【问题标题】:when i change the encryption key i get error in line "while ((data = cs.ReadByte()) != -1)"当我更改加密密钥时,我在“while ((data = cs.ReadByte()) != -1)”行中出现错误
【发布时间】:2017-11-20 03:38:12
【问题描述】:

我制作了一个桌面应用程序 wpf,我需要加密一些文件(图像、音频),我找到了这个解决方案,但它只适用于提供的加密密钥。所以当我尝试更改加密密钥时,加密效果很好,但是在解密时我得到了这个错误“le remplissage n'est pas valide et ne peut pas supprimé”

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

【问题讨论】:

  • 那么 english 中的错误信息是什么?
  • 英文错误:填充无效,无法删除,选择“while ((data = fsInput.ReadByte()) != -1)”行
  • 你做了什么发现错误?
  • 我更改了加密密钥
  • 您是否更改了两种方法中的加密密钥?

标签: c# security encryption cryptography aes


【解决方案1】:

那个英文错误是“padding is invalid and cannot be removed”。它表示密钥或(密文的结尾)已更改。当加密密钥与解密密钥不同(或者,在您的情况下,如果密码、盐或迭代计数不正确),这正是您应该期待的错误。

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

【讨论】:

    猜你喜欢
    • 2018-08-30
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 2012-06-11
    • 1970-01-01
    • 2017-06-06
    • 2017-10-01
    相关资源
    最近更新 更多