For my recent project, so far I have been using SDES for encrypting any binary file. Undoubtedly, the algorithm is simple to implement; but at the same time it is not suitable for encrypting large files as it takes plaintext of size 1 byte as input i.e Encrypts 1 byte at a time. Any suggestions on more efficient encryption algorithm is appreciated.
Below is the simple implementation of SDES on image files:
This segment of code encrypts the entire data bytes including the header. You can save the data bytes in form of ASCII or UTF-8 in a file and transmit over some insecure channel. Upon decryption and reconverting to an image file you can use magic number programming.
Conversion.Convert cvt = new Conversion.Convert(); Console.WriteLine("Please enter the RGB/GRAY/BINARY image path: "); string path = Console.ReadLine(); byte []arrayPT = Conversion.Convert.ImageToBinary(path); // Get the binary data from the image byte []arrayCT = new byte[arrayPT.Length]; int i = 0; foreach (byte element in arrayPT) { arrayCT[i] = ob.Encrypt(element); Console.Write("{0}", arrayCT[i]); //Convert the contents of arrayCT[] into char and save into a file i++; }Use this approach to encrypt only the color information i.e. pixel values.
SDES ob = new SDES(key); Bitmap img = new Bitmap(imgpath); Color pixelColor, pixels; //byte[] buff = new byte[3 * img.Height * img.Width]; for (int i = 0; i < img.Width; i++) { for (int j = 0; j < img.Height; j++) { pixels = img.GetPixel(i, j); pixelColor = Color.FromArgb(ob.Encrypt(pixels.R), ob.Encrypt(pixels.G), ob.Encrypt(pixels.B)); img.SetPixel(i, j, pixelColor); } } img.Save(newEncryptedImg);
GetPixelandSetPixelare slow. You're better off looking into using LockBits to get the raw image bytes.