在c#中加密文件(Rijndael)

时间:2017-05-20 12:56:26

标签: c# encryption rsa md5 rijndael

我有一些代码,可以用c#

中的代码解密文件

原始文件大小约为9KB,我将解密文件加载到文本框

我有按钮,我想将文本框加密成文件并保存在驱动器c:\中,但我的加密代码不起作用,加密文件大小约为19KB?怎么可能??

示例文件:Enc.zip

有人可以帮忙吗?

我的解密功能

byte[] buffer8 = new byte[32];

        buffer8[0] = 203;
        buffer8[1] = 96;
        buffer8[2] = 34;
        buffer8[3] = 58;
        buffer8[4] = 206;
        buffer8[5] = 154;
        buffer8[6] = 134;
        buffer8[7] = 23;
        buffer8[8] = 55;
        buffer8[9] = 152;
        buffer8[10] = 150;
        buffer8[11] = 91;
        buffer8[12] = 247;
        buffer8[13] = 146;
        buffer8[14] = 191;
        buffer8[15] = 222;
        buffer8[16] = 112;
        buffer8[17] = 106;
        buffer8[18] = 101;
        buffer8[19] = 172;
        buffer8[20] = 252;
        buffer8[21] = 13;
        buffer8[22] = 178;
        buffer8[23] = 43;
        buffer8[24] = 14;
        buffer8[25] = 34;
        buffer8[26] = 20;
        buffer8[27] = 203;
        buffer8[28] = 6;
        buffer8[29] = 56;
        buffer8[30] = 137;
        buffer8[31] = 232;
        byte_1 = buffer8;
        byte[] buffer9 = new byte[16];
        buffer9[0] = 208;
        buffer9[1] = 236;
        buffer9[2] = 106;
        buffer9[3] = 239;
        buffer9[4] = 230;
        buffer9[5] = 15;
        buffer9[6] = 28;
        buffer9[7] = 116;
        buffer9[8] = 65;
        buffer9[9] = 164;
        buffer9[10] = 249;;
        buffer9[11] = 74;
        buffer9[12] = 155;
        buffer9[13] = 146;
        buffer9[14] = 162;
        buffer9[15] = 107;
        byte_2 = buffer9;

        byte[] array, array2, array31;
        FileStream fileStream = new FileStream(@"C:\Enc.MFI", FileMode.Open, FileAccess.Read);
        fileStream.Position = 0L;
        array2 = new byte[fileStream.Length];
        fileStream.Read(array2, 0, array2.Length);
        fileStream.Close();
        array31 = array2;
        SymmetricAlgorithm symmetricAlgorithm12 = new RijndaelManaged();
        symmetricAlgorithm12.Mode = CipherMode.CBC;
        ICryptoTransform transform12 = symmetricAlgorithm12.CreateDecryptor(byte_1, byte_2);
        MemoryStream memoryStream16 = new MemoryStream();
        CryptoStream cryptoStream12 = new CryptoStream(memoryStream16, transform12, CryptoStreamMode.Write);
        cryptoStream12.Write(array2, 0, array2.Length);
        cryptoStream12.FlushFinalBlock();
        array = memoryStream16.ToArray();
        memoryStream16.Close();
        cryptoStream12.Close();

        MemoryStream memoryStream = new MemoryStream(array);
        BinaryReader binaryReader = new BinaryReader(memoryStream);
        memoryStream.Position = 0L;
        byte[] array3 = binaryReader.ReadBytes(binaryReader.ReadInt32());
        byte[] array4 = binaryReader.ReadBytes((int)(memoryStream.Length - 4L - (long)array3.Length));
        binaryReader.Close();

        string dec = Encoding.Unicode.GetString(array4, 0, array4.Length);
        textBox1.Text = dec;

这是我的加密功能

byte[] bytes = Encoding.Unicode.GetBytes(textBox1.Text);
        SymmetricAlgorithm symmetricAlgorithm12 = new RijndaelManaged();
        symmetricAlgorithm12.Mode = CipherMode.CBC;
        ICryptoTransform transform12 = symmetricAlgorithm12.CreateEncryptor(byte_1, byte_2);
        FileStream ur = new FileStream(@"c:\Enc.MFI", FileMode.Create, FileAccess.Write);
        MemoryStream memoryStream16 = new MemoryStream();
        CryptoStream cryptoStream12 = new CryptoStream(ur, transform12, CryptoStreamMode.Write);
        cryptoStream12.Write(bytes, 0, bytes.Length);
        cryptoStream12.Write(bytes, 0, bytes.Length);
        cryptoStream12.FlushFinalBlock();
        memoryStream16.Close();
        cryptoStream12.Close();
        ur.Dispose();
        MessageBox.Show("File Created");

1 个答案:

答案 0 :(得分:0)

我不知道它是否有帮助,但我已经制作了AES 256加密技术。 C#中的解密系统。链接:Here :D

要加密:

    Crypt0rV3 c = new Crypt0rV3(); // Generates random Key & IV
    string[] rk = Registry.GetValueNames(); // Check for registry
    if(!rk.Contains("GITGUD") // Default registry key
    {
        c.SetInRegistry();
    }
    else
    {
        c.Key = (string)rk.GetValue("Key");
        c.IV = (string)rk.GetValue("IV");
    }
    string path = @"C:\Users\" + Environment.UserName + @"\Desktop";
    byte[] buffer = File.ReadAllBytes(path);
    File.WriteAllBytes(path, c.EncodeBytes(buffer));

解码:

    byte[] buffer = File.ReadAllBytes(path);
    File.WriteAllBytes(path, c.DecodeBytes(buffer));

摧毁并永不解密:

    c.Destroy(); // Remove from registry