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

本文关键字:文件 解密 数据 加密 方法 Rijndael 中的 | 更新日期: 2023-09-27 18:06:24

我有两个使用加密和解密的方法,我所做的是读取加密的文件数据,并用加密的数据修改文件,这个操作正确完成,但是当我去解密加密的数据时,如果我返回奇怪的字符,不会返回我不是原始的值:以下是函数:

public void EncryptFile(string[] conexion)
        {
            var mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

            var inputFile = mydocpath + @"'ProjectCONFIG.sigc";
            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
            var cs = new CryptoStream(fsEncrypt, rmCrypto.CreateEncryptor(key, iv), CryptoStreamMode.Write);
            StreamWriter streamWriter = new StreamWriter(cs);
            streamWriter.WriteLine(conexion);
            streamWriter.Close();
            cs.Close();
            fsEncrypt.Close();
        }
        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
            var cs = new CryptoStream(fsEncrypt, rmCrypto.CreateEncryptor(key, iv), CryptoStreamMode.Read);
            StreamReader streamReader = new StreamReader(cs);
            string conexion = streamReader.ReadLine();

            streamReader.Close();
            cs.Close();
            fsEncrypt.Close();
            return conexion;
        }

有什么建议......

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

@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;
        }