用于加密大字节数组的高效加密算法

时间:2014-02-17 13:57:14

标签: c# algorithm encryption

对于我最近的项目,到目前为止,我一直在使用SDES加密任何二进制文件。毫无疑问,该算法易于实现;但同时它不适合加密大文件,因为它需要1字节大小的明文作为输入,即一次加密1个字节。任何关于更有效的加密算法的建议都表示赞赏


以下是图像文件上SDES的简单实现:

  • 此段代码会加密包括标头在内的整个数据字节。您可以将ASCII或UTF-8格式的数据字节保存在文件中,并通过某些不安全的通道进行传输。解密并重新转换为图像文件后,您可以使用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++;
        }
    
  • 使用此方法仅加密颜色信息,即像素值。

        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);
    

1 个答案:

答案 0 :(得分:3)

您是否考虑过使用C#集成加密功能?它们位于System.Security.Cryptography。有CryptoStream似乎允许基于Stream的加密转换。

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

这些类可以追溯到操作系统集成的加密数据包,IIRC非常好。

相关问题