C# 当我更改加密密钥时,第行出现错误;而((data=cs.ReadByte())!=-1)";

C# 当我更改加密密钥时,第行出现错误;而((data=cs.ReadByte())!=-1)";,c#,security,encryption,cryptography,aes,C#,Security,Encryption,Cryptography,Aes,我制作了一个桌面应用程序wpf,我需要加密一些文件(图像、音频),我找到了这个解决方案,但它仅适用于提供的加密密钥。因此,当我尝试更改加密密钥时,加密工作正常,但在解密时,我得到了这个错误“le Remplicage n'est pas valide et ne peut pas supplime” 英文中的错误是“填充无效,无法删除”。它表示密钥或(密文结尾)已更改。当加密密钥与解密密钥不同时(或者,在您的情况下,如果密码、salt或迭代计数不正确),这正是您应该预期的错误 CBC加密/解密的

我制作了一个桌面应用程序wpf,我需要加密一些文件(图像、音频),我找到了这个解决方案,但它仅适用于提供的加密密钥。因此,当我尝试更改加密密钥时,加密工作正常,但在解密时,我得到了这个错误“le Remplicage n'est pas valide et ne peut pas supplime”

英文中的错误是“填充无效,无法删除”。它表示密钥或(密文结尾)已更改。当加密密钥与解密密钥不同时(或者,在您的情况下,如果密码、salt或迭代计数不正确),这正是您应该预期的错误


CBC加密/解密的坏处在于,您可能不会得到此错误-只是错误的明文。您可以使用经过身份验证的加密(如GCM模式加密)来避免这种情况。

那么英文错误消息是什么?英文错误:填充无效,无法删除,它选择了行“while((data=fsInput.ReadByte())!=-1)”你做了什么来发现错误?我更改了加密密钥你在两种方法中都更改了加密密钥吗?
    public void Encrypt(string inputFilePath, string outputfilePath)
    {
        string EncryptionKey = "MAKV2SPBNI99212";
        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);
            using (FileStream fsOutput = new FileStream(outputfilePath, FileMode.Create))
            {
                using (CryptoStream cs = new CryptoStream(fsOutput, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    using (FileStream fsInput = new FileStream(inputFilePath, FileMode.Open))
                    {
                        int data;
                        while ((data = fsInput.ReadByte()) != -1)
                        {
                            cs.WriteByte((byte)data);
                        }
                        cs.Close();
                        fsInput.Close();
                    }
                    fsOutput.Close();
                }
            }
        }
    }    

    public void Decrypt(string inputFilePath, string outputfilePath)
    {
        string EncryptionKey = "MAKV2SPBNI99212";
        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);
            using (FileStream fsInput = new FileStream(inputFilePath, FileMode.Open))
            {
                using (CryptoStream cs = new CryptoStream(fsInput, encryptor.CreateDecryptor(), CryptoStreamMode.Read))
                {
                    using (FileStream fsOutput = new FileStream(outputfilePath, FileMode.Create))
                    {
                        int data;
                        while ((data = cs.ReadByte()) != -1)
                        {
                            fsOutput.WriteByte((byte)data);
                        }
                    }
                }
            }
        }
    }