Tuesday, December 14, 2010

Decrypting SSL traffic with Wireshark: "ssl_load_key: can't import pem data"


One reason for the "ssl_load_key: can't import pem data" can be:
Open your PEM Key file.
It should read
-----BEGIN RSA PRIVATE KEY-----
Base64 encoded key
-----END RSA PRIVATE KEY-----
Occasionally you will end up with a key file in the following format, hence the "ssl_load_key: can't import pem data" error,
-----BEGIN PRIVATE KEY-----
Bse64 encoded key
-----END PRIVATE KEY-----
To convert it to the correct format that can be read by Wireshark you will need OpenSSL,
Enter the following commands into a console window:
> openssl pkcs8 -topk8 -in key.pem -out temp.pem
Enter a temporary password when prompted
And then to RSA format:
>openssl rsa -in temp.pem -out rsa.pem
Enter the temporary password from previous step when prompted.
Use rsa.pem in Wireshark


Wednesday, December 1, 2010

Prime Art Generators

Go here to see how to generate a set of Primes.


This is a simple method that traverses each row in the image and puts a black pixel at every point that is a Prime number.
private Bitmap CreateNormalPrimeArt(HashSet<int> primes, int width, int height)
{
    Bitmap bmp = new Bitmap(width, height);
    using (Graphics gfx = Graphics.FromImage(bmp))
    {
        using (Brush brush = new SolidBrush(Color.FromArgb(25, 25, 76)))
        {
            gfx.FillRectangle(brush, new Rectangle(0, 0, width, height));
        }
    }
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            if (primes.Contains((y * height) + x))
                bmp.SetPixel(x, y, Color.White);
        }
    }
    using (Graphics gfx = Graphics.FromImage(bmp))
    {
        using (Brush brush = new SolidBrush(Color.White))
        {
            gfx.DrawString("Prime Rows", new System.Drawing.Font("Calibri", 10), brush, new PointF(20, height - 20));
        }
    }
    return bmp;
}



The second is based on the Ulam Spiral.
private Bitmap CreateUlamSpiralPrimeArt(HashSet<int> primes, int width, int height)
{
    if (width != height)
    {
        if (width > height)
            width = height;
        else
            height = width;
    }
    if (width % 2 == 0)
    {
        width--;
        height--;
    }
    int index = 1;
    int sideLength = 2;
    int thisSide = 2;
    int turns = 0;
    Bitmap bmp = new Bitmap(width, height);
    using (Graphics gfx = Graphics.FromImage(bmp))
    {
        using (Brush brush = new SolidBrush(Color.FromArgb(25, 25, 76)))
        {
            gfx.FillRectangle(brush, new Rectangle(0, 0, width, height));
        }
    }
    // set p to center
    Point p = new Point(width / 2, height / 2);
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            if (p.Y <= 5 || p.Y >= height - 5 || p.X <= 5 || p.X >= width - 5)
                break;
            if (primes.Contains(index))
                bmp.SetPixel(p.X, p.Y, Color.White);
            index++;
            thisSide--;
            int dir = turns % 4;
            if (thisSide == 0)
            {
                thisSide = sideLength;
                turns++;
                if (dir == 1 || dir == 3)
                    sideLength += 1;
            }
            if (dir == 0)
            {
                p.X = p.X + 1;
            }
            else if (dir == 1)
            {
                p.Y = p.Y - 1;
            }
            else if (dir == 2)
            {
                p.X = p.X - 1;
            }
            else if (dir == 3)
            {
                p.Y = p.Y + 1;
            }
        }
    }
    using (Graphics gfx = Graphics.FromImage(bmp))
    {
        using (Brush brush = new SolidBrush(Color.White))
        {
            gfx.DrawString("Ulam Spiral", new System.Drawing.Font("Calibri", 10), brush, new PointF(20, height - 20));
        }
    }
    return bmp;
}



Third method creates nice spirals based on the Prime distribution. Enjoy.
private Bitmap CreateDSCPrimeArt(HashSet<int> primes, int width, int height)
{
    int index = 1;
    Bitmap bmp = new Bitmap(width, height);
    using (Graphics gfx = Graphics.FromImage(bmp))
    {
        using (Brush brush = new SolidBrush(Color.FromArgb(25, 25, 76)))
        {
            gfx.FillRectangle(brush, new Rectangle(0, 0, width, height));
        }
    }
    // set p to center
    int max = primes.Max();
    Point p = new Point(width / 2, height / 2);
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            if (primes.Contains(index))
            {
                // calculate location of the next pixel from the center of the image
                // distance to the center of the image is based on the square root of the prime
                PointF pixel = new Point() { X = p.X, Y = p.Y };
                pixel.X += (float)(Math.Sin(index) * Math.Sqrt(index));
                pixel.Y += (float)(Math.Cos(index) * Math.Sqrt(index));

                if (!(pixel.Y <= 5 || pixel.Y >= height - 5 || pixel.X <= 5 || pixel.X >= width - 5))
                {
                    bmp.SetPixel((int)pixel.X, (int)pixel.Y, Color.White);
                }
            }
            index++;


            if (index > max)
                break;
        }
    }

    using (Graphics gfx = Graphics.FromImage(bmp))
    {
        using (Brush brush = new SolidBrush(Color.White))
        {
            gfx.DrawString("Prime Spiral", new System.Drawing.Font("Calibri", 10), brush, new PointF(20, height - 20));
        }
    }
    return bmp;
}

Update 2017-01-30
Fixed broken pictures, changed code to a cleaner format without line numbering.


Tuesday, November 30, 2010

Generate HashSet of Prime numbers, check if Prime. Basic

public static HashSet<int> GetPrimes(int max)
{
  HashSet<int> primes = new HashSet<int>();
  for (int i = 2; i < max; i++)
   if (IsPrime(i))
    primes.Add(i);
  return primes;
}

private static bool IsPrime(int candidate)
{
  bool prime = true;
  double sqr = Math.Sqrt(candidate);
  for (int i = 2; i <= sqr; i++)
  {
   if (candidate % i == 0)
   {
    prime = false;
    break;
   }
  }
  return prime;
}

Thursday, November 18, 2010

How to block advertisements in Windows Live Messenger

A friend asked me how I managed to get rid of the advertisements in Windows Live Messenger so I thought that I'd share it here.
I found this little trick some years ago when I got annoyed by the mouse over adverts that expanded outside the initial box that they appear in. I honestly do not remember where I found it and when I try to Google for the fix now it only finds some downloadable applications that should 'fix the ads'. They seem a little suspicious thou so I'll just share my fix here.

It seems that Windows Live Messenger gets all its advertisements from various feeds from msn.com, msads.net and live.com. Lucky us the feeds originate from subdomains to these addresses so we don't have to block the whole domains.

1. Open your "hosts" file that is located in one of the system folders.
  XP : C:\WINDOWS\system32\drivers\etc

Note that the file may be read-only so you should open it with administrative privileges. Right click and select "Run as Administrator".

2. Add the following lines to the end of the hosts file below the reference to localhost.
0.0.0.0 rad.msn.com
0.0.0.0 global.msads.net
0.0.0.0 rss.video.msn.com
0.0.0.0 ads1.msn.com
0.0.0.0 rad.live.com
0.0.0.0 specials.uk.msn.com

3. Save the file.

Done.

windows_live_messenger_after_no_advertisements_fixThe advertisements may still show for a little while after the fix has been applied. The next time Windows Live Messenger tries to download new advertisements it will only show a non clickable logo as seen in the picture to the right.
To speed up the process just restart Windows Live Messenger and the fix should be applied immediately.

Friday, October 15, 2010

Client Certificate: FormatException Invalid Hexadecimal string format


Ran into this exception while trying to add a client certificate to a SOAP client.
After installing the certificate through the mmc->Certificate snap-in I tried to copy the Thumbprint of the said certificate so that my client application would be able to use it.
The snap-in dispays the thumbprint as follows:
34 f7 83 fc 40 f6 1d d7 1c 92 43 c9 a5 af aa 42 ba 5e 27 db
So I just copied it and inserted it into my app.config.
First exception was from the fact that the thumbprint needs to be a string without spaces, converted the string to:
34f783fc40f61dd71c9243c9a5afaa42ba5e27db
This time I got the exception FormatException: Invalid Hexadecimal string format. I copied the string from the snap-in again and this time I pasted it into Notepad++ to see if there were anything else hidden in the string. As it turned out there was.
?34 f7 83 fc 40 f6 1d d7 1c 92 43 c9 a5 af aa 42 ba 5e 27 db
So pasting back into the app.config and this time removing the starting character and the starting quote and manually writing them back and the just to be safe doing the same thing with the ending character and the ending quote it worked. Don’t forget to remove the spaces as well.
Hope this helps somebody out there as it really slowed me down last night. : )


Tuesday, October 12, 2010

WPF Dispatcher

guiControl.Dispatcher.Invoke(
    System.Windows.Threading.DispatcherPriority.Normal,
    Action(() =>
    {
        // do stuff
    }));
Ensure that GUI control manipulation is done in the GUI thread.

ThreadStart snippet


using System.Threading;
Thread thread = new Thread(
 new ThreadStart(() =>
  {
   // do stuff
  }));
thread.Start();
Snippet for creating separate thread inline and starting it.