日期:2014-05-18  浏览次数:20960 次

C#加密文件后怎样解密
C# code

             try
            {
                byte[] Key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 
0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };

                byte[] IV = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 
0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };

                RijndaelManaged RMCrypto = new RijndaelManaged();

                FileStream file = new FileStream(@"f:\a.txt",FileMode.Open);

                CryptoStream crypto = new CryptoStream(file,RMCrypto.CreateEncryptor(Key, IV), CryptoStreamMode.Read);

                StreamReader SReader = new StreamReader(crypto);

                SReader.Close();

                file.Close();

            }
            catch 
            {
                Console.WriteLine("The Listener Failed.");
            }



用CryptoStream和RijndaelManaged算法把txt加密成功了,打开乱码。 但是我想解密,把文件复原。都按照msdn的做了没效果,也没异常。

搞不懂

------解决方案--------------------
那就要看文件的编码格式了。中间使用错误的编码将导致乱码,最后的结果当然也就不可能对了。

看看下面的代码,可以正常工作的:
用法:
a. 调用TestEnDencrypt.Encrypt(@"f:\a.xml") 加密获得"f:\a.xml_"文件,我测试用的xml是Unicode编码的,所以代码中用Encoding.Unicode.GetString获取的,你要对照文件更改对应的编码格式,这个是一个关键点;
b. 调用TestEnDencrypt.Dencrypt(@"f:\a.xml_")文件就可以成功得到解密后的字符串内容了。

C# code

    class TestEnDencrypt
    {

        static byte[] Key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 
0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };

        static byte[] IV = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 
0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };


        static void Encrypt(string fileName)
        {

            try
            {
                RijndaelManaged RMCrypto = new RijndaelManaged();

                FileStream file = new FileStream(fileName, FileMode.Open);
                byte[] data = new byte[file.Length];

                file.Read(data, 0, (int)file.Length);

                file.Close();

                MemoryStream ms = new MemoryStream();

                CryptoStream crypto = new CryptoStream(ms, RMCrypto.CreateEncryptor(Key, IV), CryptoStreamMode.Write);

                using (StreamWriter sw = new StreamWriter(crypto))
                {
                    string text = Encoding.Unicode.GetString(data);
                    sw.Write(text);
                }

                File.WriteAllBytes(fileName + "_", ms.ToArray());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        static string Dencrypt(string fileName)
        {
            string ret = null;

            try
            {
                RijndaelManaged RMCrypto = new RijndaelManaged();
                FileStream file = new FileStream(fileName, FileMode.Open);

                CryptoStream crypto = new CryptoStream(file, RMCrypto.CreateDecryptor(Key, IV), CryptoStreamMode.Read);

                StreamReader SReader = new StreamReader(crypto);

                ret = SReader.ReadToEnd();

                SReader.Close();

                file.Close();

            }
            catch
            {
                Console.WriteLine("The Listener Failed.");
            }

            return ret;
        }
    }