2015-02-05 93 views
-1

我有這個javascript。當我試圖解密時,它沒有給出任何輸出。 加密工作正常但不解密 我在做什麼錯誤。解密與cryptojs失敗

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script> 
<script language = "javascript"> 


    var JsonFormatter = { 
     stringify: function (cipherParams) { 
      // create json object with ciphertext 
      var jsonObj = { 
       ct: cipherParams.ciphertext.toString(CryptoJS.enc.Base64) 
      }; 

      // optionally add iv and salt 
      if (cipherParams.iv) { 
       jsonObj.iv = cipherParams.iv.toString(); 
      } 
      if (cipherParams.salt) { 
       jsonObj.s = cipherParams.salt.toString(); 
      } 

      // stringify json object 
      return JSON.stringify(jsonObj); 
     }, 

     parse: function (jsonStr) { 
      // parse json string 
      var jsonObj = JSON.parse(jsonStr); 

      // extract ciphertext from json object, and create cipher params object 
      var cipherParams = CryptoJS.lib.CipherParams.create({ 
       ciphertext: CryptoJS.enc.Base64.parse(jsonObj.ct) 
      }); 

      // optionally extract iv and salt 
      if (jsonObj.iv) { 
       cipherParams.iv = CryptoJS.enc.Hex.parse(jsonObj.iv) 
      } 
      if (jsonObj.s) { 
       cipherParams.salt = CryptoJS.enc.Hex.parse(jsonObj.s) 
      } 

      return cipherParams; 
     } 
    }; 

    var AES = CryptoJS.AES; 
    var key = "B374A26A71490437AA024E4FADD5B497FDFF1A8EA6FF12F6FB65AF2720B59CCF"; 
    var iv = CryptoJS.enc.Hex.parse('7E892875A52C59A3B588306B13C31FBD'); 
    var aeskey = CryptoJS.enc.Hex.parse(key); 
    var secret = "50585"; 

    alert("attempt 1"); 
    var e1 = AES.encrypt(secret, aeskey, { iv: iv , format: JsonFormatter }); 
    var encJSON = JSON.parse(e1); 
    var encresult = encJSON['ct']; 
    var encres = encodeURIComponent(encresult);  
    alert("encrypted = " + encresult);  
    alert("e1.iv = " + e1.iv); 
    alert("encoded = " + encres); 

    alert("attempt 2"); 
    var decoderesult = decodeURIComponent(encres);  
    var encObj = CryptoJS.lib.CipherParams.create({ 
     ciphertext: CryptoJS.enc.Base64.parse(decoderesult) 
    }); 
    var decrypt = AES.decrypt(encObj, aeskey, { iv: iv , format: JsonFormatter }); 
    var decrypted = decrypted.toString(CryptoJS.enc.Utf8); 
    alert("decoded = " + decoderesult); 
    alert("decrypted = " + decrypted);  
    alert("decrypt.iv = " + decrypt.iv); 


</script> 

加密警報即將到來,但不是解密。爲什麼和在哪裏失敗

+0

錯字的問題是題外話,不利於未來的讀者。您可以刪除您的問題,也可以等到它關閉然後自動刪除。 – 2015-02-05 20:27:40

回答

-1

我想你只有一個錯字var decrypted = decrypted.toString(CryptoJS.enc.Utf8);它應該是var decrypted = decrypt.toString(CryptoJS.enc.Utf8)

之後,您將得到一個decoderesultdecrypted。但decrypt.iv仍然返回未定義。不知道這裏有什麼問題。

有關演示,請參閱下面和這裏的jsFiddle

var log = function(text) { 
 
    output.innerHTML += '<p>' + text + '</p>\n'; 
 
} 
 

 
var JsonFormatter = { 
 
     stringify: function (cipherParams) { 
 
      // create json object with ciphertext 
 
      var jsonObj = { 
 
       ct: cipherParams.ciphertext.toString(CryptoJS.enc.Base64) 
 
      }; 
 

 
      // optionally add iv and salt 
 
      if (cipherParams.iv) { 
 
       jsonObj.iv = cipherParams.iv.toString(); 
 
      } 
 
      if (cipherParams.salt) { 
 
       jsonObj.s = cipherParams.salt.toString(); 
 
      } 
 

 
      // stringify json object 
 
      return JSON.stringify(jsonObj); 
 
     }, 
 

 
     parse: function (jsonStr) { 
 
      // parse json string 
 
      var jsonObj = JSON.parse(jsonStr); 
 

 
      // extract ciphertext from json object, and create cipher params object 
 
      var cipherParams = CryptoJS.lib.CipherParams.create({ 
 
       ciphertext: CryptoJS.enc.Base64.parse(jsonObj.ct) 
 
      }); 
 

 
      // optionally extract iv and salt 
 
      if (jsonObj.iv) { 
 
       cipherParams.iv = CryptoJS.enc.Hex.parse(jsonObj.iv) 
 
      } 
 
      if (jsonObj.s) { 
 
       cipherParams.salt = CryptoJS.enc.Hex.parse(jsonObj.s) 
 
      } 
 

 
      return cipherParams; 
 
     } 
 
    }; 
 

 
    var AES = CryptoJS.AES; 
 
    var key = "B374A26A71490437AA024E4FADD5B497FDFF1A8EA6FF12F6FB65AF2720B59CCF"; 
 
    var iv = CryptoJS.enc.Hex.parse('7E892875A52C59A3B588306B13C31FBD'); 
 
    var aeskey = CryptoJS.enc.Hex.parse(key); 
 
    var secret = "50585"; 
 

 
    //alert("attempt 1"); 
 
    var e1 = AES.encrypt(secret, aeskey, { iv: iv , format: JsonFormatter }); 
 
    var encJSON = JSON.parse(e1); 
 
    var encresult = encJSON['ct']; 
 
    var encres = encodeURIComponent(encresult);  
 
    console.log('encres', encresult); 
 
    console.log('e1.iv=', e1.iv); 
 
    console.log('encoded=', encres); 
 
    log('encres= ' + encresult); 
 
    log('e1.iv=' + e1.iv); 
 
    log('encoded=' + encres); 
 
    //alert("encrypted = " + encresult);  
 
    //alert("e1.iv = " + e1.iv); 
 
    //alert("encoded = " + encres); 
 

 
    //alert("attempt 2"); 
 
    var decoderesult = decodeURIComponent(encres);  
 
    var encObj = CryptoJS.lib.CipherParams.create({ 
 
     ciphertext: CryptoJS.enc.Base64.parse(decoderesult) 
 
    }); 
 
    var decrypt = AES.decrypt(encObj, aeskey, { iv: iv , format: JsonFormatter }); 
 
    console.log(decrypt); 
 
    var decrypted = decrypt.toString(CryptoJS.enc.Utf8); //decrypted --> decrypt 
 
    console.log("decoded = " + decoderesult); 
 
    console.log("decrypted = " + decrypted);  
 
    console.log("decrypt.iv = " + decrypt.iv); 
 
    log("decoded = " + decoderesult); 
 
    log("decrypted = " + decrypted);  
 
    log("decrypt.iv = " + decrypt.iv);
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script> 
 

 
<div id="output"></div>

+0

這顯然是一個錯字。你應該把這個問題標記爲脫離主題,而不是回答它。 – 2015-02-05 20:28:42