2013-08-20 52 views
0

我在網上查找這個異常意味着什麼與我的程序有關,但似乎無法找到解決方案或其原因爲什麼發生到我的具體程序。填充是無效的,不能使用CryptoJS 3.1加密和服務器端AesCryptoServiceProvider解密

.NET C#代碼

/// 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> 

我的代碼:

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

回答

-1

嘗試用以下

更換你的代碼
var encrypted = CryptoJS.AES.encrypt(newPassword, oldPassword); 
cipherText1 = Convert.ToBase64String(encrypted); 
console.log(cipherText1); 
+2

Convert.ToBase64String(encrypted);方法來自.net C#lib。不能在Javascript中使用 – Kunal

相關問題