2014-06-23 46 views
-4

第一個代碼是針對實際加密我提供的數據的jscript(cryptoJS)。我也在mcrypt中使用了常量IV和一些關鍵字。事實證明,一些結果是不同的。被調用時,IV鍵與keybase64不同。看看JavaScript中的CryptoJS和PHP中的Mcrypt的差異結果

這裏JSCRIPT(cryptoJS)

function crypto_encrypt(text) {                  //This is for JS 
var keyBase64 = CryptoJS.enc.Base64.parse("ITU2NjNhI0tOc2FmZExOTQ=="); 
var iv = CryptoJS.enc.Base64.parse('AAAAAAAAAAAAAAAAAAAAAA=='); 

var encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(text), keyBase64, 
    { 
     keySize: 128/8, 
     iv: iv, 
     mode: CryptoJS.mode.CBC, 
     padding: CryptoJS.pad.Pkcs7 
    }); 

// Returns a Base64 encoded string. 
return encrypted; 

}

然後我把它叫做這個

<!DOCTYPE html> 
<html> 
<head lang="en"> 
<meta charset="UTF-8"> 
<title></title> 
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script> 
<script type='text/javascript' src='./jquery.soap.js'></script> 
<!--<script type='text/javascript' src='./soapclient.js'></script>--> 

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script> 
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-base64-min.js"></script> 
<script type='text/javascript' src='./HCSConnect.js'></script> 
</head> 
<body> 
<script language="JavaScript" type="text/javascript"> 
var encryptedname = crypto_encrypt('Patrick'); 
alert(encryptedname); 

</script> 
</body> 
</html> 

結果是Y44Ktqd/b2VpbdUFaXR8bg==

現在,當我使用在這樣的PHP mcrypt

<?php 
$Pass = "Password"; 
$Clear = "Patrick"; 
//$sSecretKey = ("ITU2NjNhI0tOc2FmZExOTQ==");  

$crypted = fnEncrypt($Clear, $Pass); 
echo "Encrypred: ".$crypted."</br>"; 

$newClear = fnDecrypt($crypted, $Pass); 
echo "Decrypted: ".$newClear."</br>"; 



function fnEncrypt($sValue, $sSecretKey) 
{ 
return rtrim(
    base64_encode(
     mcrypt_encrypt(
      MCRYPT_RIJNDAEL_192, 
      ("ITU2NjNhI0tOc2FmZExOTQ=="),$sValue, 
      MCRYPT_MODE_CBC, 
      $iv = ('AAAAAAAAAAAAAAAAAAAAAA==') 
      ) 
    ), "\0" 
    ); 
} 

function fnDecrypt($sValue, $sSecretKey) 
{ 
return rtrim(
    mcrypt_decrypt(
     MCRYPT_RIJNDAEL_192, 
     ("ITU2NjNhI0tOc2FmZExOTQ=="), 
     base64_decode($sValue), 
     MCRYPT_MODE_CBC, 
     $iv = ('AAAAAAAAAAAAAAAAAAAAAA==') 
    ), "\0" 
); 
} 
?> 

答案是xjjhImM0ZmS6roSthDAnbUwYB1ihnve1

在哪裏我出了問題。

回答

1

你不BASE64_DECODE你的密鑰和IV:

function fnEncrypt($sValue, $sSecretKey) 
{ 
return rtrim(
    base64_encode(
     mcrypt_encrypt(
      MCRYPT_RIJNDAEL_192, 
      base64_decode("ITU2NjNhI0tOc2FmZExOTQ=="),$sValue, // added base64_decode 
      MCRYPT_MODE_CBC, 
      $iv = base64_decode('AAAAAAAAAAAAAAAAAAAAAA==') // added base64_decode 
      ) 
    ), "\0" 
    ); 
} 

同其它功能。

+0

不,先生,它不能解決問題。 – user15077