0

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);
    
5
  • 2
    Isn't this what the classes in System.Security.Cryptography are for? Also, the approach to encrypt only the pixel values is slow because GetPixel and SetPixel are slow. You're better off looking into using LockBits to get the raw image bytes. Commented Feb 17, 2014 at 14:04
  • What do you mean with efficient, is it the speed or the strength of the encryption? Commented Feb 17, 2014 at 14:12
  • @Petoj balance between both will be good. Commented Feb 17, 2014 at 14:23
  • @pk1772: Have you heard of AES? It's quite a common cipher... Commented Feb 17, 2014 at 17:04
  • @NiklasB.- too many rounds! But I think the modified version will do. Commented Feb 20, 2014 at 8:53

1 Answer 1

3

Have you considered using the C# integrated cryptography functions? They are found in System.Security.Cryptography. There is a CryptoStream that seems to allow Stream based cryptographic transformations.

http://msdn.microsoft.com/en-us/library/system.security.cryptography.cryptostream(v=vs.110).aspx

Those classes go back to the OS integraed cryptography packet which IIRC is pretty good.

Sign up to request clarification or add additional context in comments.

2 Comments

Actually only the classes that end in "CryptoServiceProvider" go back to the OS integrated cryptography packet, the ones that end in "Managed" use a fully .NET based implementation. That is why they have abstract base classes that both implementations inherit from, you can use that without worrying which is used.
Also CryptoStream has no implementation itself, it is just a (very useful) wrapper around a ICryptoTransform, this lets you use any class derived from SymmetricAlgorithm as the actual algorithm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.