简体   繁体   English

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

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

I have looked online for what this exception means in relation to my program but can't seem to find a solution or the reason why its happening to my specific program. 我已经在网上查找了此异常对我的程序的含义,但似乎找不到解决方案或它在我的特定程序中发生的原因。

C#.Net code: 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();
        }
    }

I have use google CryptoJS 3.1 to encrypt password 我已经使用谷歌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>

My JavaScript code : 我的JavaScript代码:

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

Try replacing your code with following 尝试使用以下代码替换您的代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM