我无法使用c#中的Rijndael方法正确解密文件中的加密数据?

时间:2016-09-10 12:49:13

标签: c# encryption cryptography rijndaelmanaged

我有两种使用Encrypt和Decrypt的方法,我所做的是读取加密的文件数据并用加密数据修改文件,这个操作正确完成但是当我去解密加密数据时没有返回给我如果我返回奇怪的字符,则值不是原始的 这些是功能:

@media (min-width: 992px) and (max-width: 1199px) {
    .pull-md-left {
        float: left;
    }
    .pull-md-right {
        float: right;
    }
}

任何建议......

1 个答案:

答案 0 :(得分:0)

@Artjom B.他意识到我在解密函数中犯了一个错误:

 public string DecryptFile(string inputFile)
        {

            const string password = @"*PROJECT-CONFIG-FILE-ENCRYPTED-2016*"; // Your Key Here

            var cryptFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            var fsEncrypt = new FileStream(inputFile, FileMode.Create);

            var rmCrypto = new RijndaelManaged();

            Rfc2898DeriveBytes pwdGen = new Rfc2898DeriveBytes(password, 10000);
            rmCrypto.BlockSize = 256;

            byte[] key = pwdGen.GetBytes(rmCrypto.KeySize / 8);   //This will generate a 256 bits key
            byte[] iv = pwdGen.GetBytes(rmCrypto.BlockSize / 8);  //This will generate a 256 bits IV

// The mistake is that he was calling the CreateEncryptor function ()
                var cs = new CryptoStream(fsEncrypt, rmCrypto.CreateDecryptor(key, iv), CryptoStreamMode.Read);

            StreamReader streamReader = new StreamReader(cs);
            string conexion = streamReader.ReadLine();


            streamReader.Close();
            cs.Close();
            fsEncrypt.Close();

            return conexion;
        }