Encryption 填充无效,无法通过Rijndael解密删除

Encryption 填充无效,无法通过Rijndael解密删除,encryption,rijndael,Encryption,Rijndael,当我调用下面的方法从windows应用程序中解密字符串时,我看到了“填充无效且无法删除”错误。字符串是从asp.net应用程序加密的。两个应用程序引用相同的程序集。我可以在asp.net应用程序中进行加密和解密,而不会出现任何问题。这是我进行加密和解密的主要代码 private static byte[] EncryptHelper(byte[] arrData, string Password, bool Encrypt) { //Create the Syme

当我调用下面的方法从windows应用程序中解密字符串时,我看到了“填充无效且无法删除”错误。字符串是从asp.net应用程序加密的。两个应用程序引用相同的程序集。我可以在asp.net应用程序中进行加密和解密,而不会出现任何问题。这是我进行加密和解密的主要代码

    private static byte[] EncryptHelper(byte[] arrData, string Password, bool Encrypt)
    {
        //Create the SymetricAlgorithem object
        SymmetricAlgorithm myAlg = new RijndaelManaged();

        //define a salt value to derive the key.
        byte[] salt = System.Text.Encoding.ASCII.GetBytes("hjkhj877ffasah");

        //Instantiate Rfc2898DeriveBytes with the password and salt.
        Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(Password, salt);


        myAlg.Key = key.GetBytes(myAlg.KeySize / 8); 
        myAlg.IV = key.GetBytes(myAlg.BlockSize / 8); 
        myAlg.Padding = PaddingMode.PKCS7;
        //Create the ICryptoTransform Object
        ICryptoTransform encrytptor = Encrypt ? myAlg.CreateEncryptor() : myAlg.CreateDecryptor();

        //Create Memorystream to write the encrypted data
        using (MemoryStream aStream = new MemoryStream())
        {

            //Create the CryptoStream Ojbect using the aStream object
            using (CryptoStream encryptStream = new CryptoStream(aStream, encrytptor, CryptoStreamMode.Write))
            {
                //Write the contents to crypto stream
                encryptStream.Write(arrData, 0, arrData.Length);

                //Flush the cryptostream
                encryptStream.FlushFinalBlock();

                //Reposition the memorystream to write the contents to an array.
                aStream.Position = 0;

            }
            aStream.Flush();
            //Convert to an array and return
            return aStream.ToArray();

        }
    }
这是我用来将纯文本从/转换为字节数组的方法

    private static byte[] GetBytes(string str)
    {
        byte[] bytes = new byte[str.Length * sizeof(char)];
        System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
        return bytes;
    }

    private static string GetString(byte[] bytes)
    {
        char[] chars = new char[bytes.Length / sizeof(char)];
        System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
        return new string(chars);
    }

为了将密码文本持久化到数据库,我使用Convert.ToBase64String()和Convert.FromBase64String。问题是我使用Rfc2898DeriveBytes类的方式有问题吗?

好吧,我认为从安全性的角度来看,对于每个具有相同密码的消息,您将使用相同的IV,而可预测的IV是一个真正的大禁忌

在那之后,我不想再多看一看到底出了什么问题,stackoverflow上有很多非常糟糕的剪切粘贴C#加密,它们只是坐在那里,没有更新机制,没有人再看它们,除非人们发现它们要再次剪切粘贴


我会尽量让它保持最新并进行审阅。

如果您有两个代码库,显然需要同时包含这两个代码库。在这里读一些答案。关闭,信息不足,否则“太本地化”。