填充无效,无法使用CryptoJS 3.1加密和服务器端AesCryptoServiceProvider解密来删除

Padding is invalid and cannot be removed using CryptoJS 3.1 to encrypt and server side AesCryptoServiceProvider to decrypt

本文关键字:服务器端 加密 AesCryptoServiceProvider 解密 删除 无效 CryptoJS 填充      更新时间:2023-09-26

我在网上查找了这个异常对我的程序意味着什么,但似乎找不到解决方案,也找不到它发生在我的特定程序中的原因。

C#.Net代码:

    /// Decrypts a BASE64 encoded string of encrypted data, returns a plain string
    /// </summary>
    /// <param name="base64StringToDecrypt">an Aes encrypted AND base64 encoded string</param>
    /// <param name="passphrase">The passphrase.</param>
    /// <returns>returns a plain string</returns>
    public static string AESDecrypt(string base64StringToDecrypt, string passphrase)
    {
        //Set up the encryption objects
        using (AesCryptoServiceProvider acsp = GetProvider(Encoding.Default.GetBytes(passphrase)))
        {
            byte[] RawBytes = Convert.FromBase64String(base64StringToDecrypt);
            ICryptoTransform ictD = acsp.CreateDecryptor();
            //RawBytes now contains original byte array, still in Encrypted state
            //Decrypt into stream
            MemoryStream msD = new MemoryStream(RawBytes, 0, RawBytes.Length);
            CryptoStream csD = new CryptoStream(msD, ictD, CryptoStreamMode.Read);
            //csD now contains original byte array, fully decrypted
            //return the content of msD as a regular string
            return (new StreamReader(csD)).ReadToEnd();
        }
    }

我已经使用谷歌CryptoJS 3.1加密密码

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
<script>
var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase");
var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase");
</script>

我的JavaScript代码:

var encrypted = CryptoJS.AES.encrypt(newPassword, oldPassword);
cipherText1 = encrypted.ciphertext.toString(CryptoJS.enc.Base64);
console.log(cipherText1);

尝试用以下替换您的代码

var encrypted = CryptoJS.AES.encrypt(newPassword, oldPassword);
cipherText1 = Convert.ToBase64String(encrypted); 
console.log(cipherText1);