我需要使用Javascript(pidCrypt AES CBC)加密一个短字符串,然后使用PHP(phpseclib Crypt_AES CBC)对其进行解密。
各种库之间存在很多不兼容性(尝试了很多库,有些我甚至因为缺乏文档而无法开始使用)我试图理解并克服它们(现在的“标准”) “只是不介绍调试,并且只是使用不正确的用户将返回错误或仅返回false
。”
pidCrypt AES CBC表示它正在使用OpenSSL兼容编码。
到目前为止,我无法理解为什么pidCrypt的输出不会在phpseclib中解密:
pidCrypt
var aes=new pidCrypt.AES.CBC();
aes.encryptText("abcdefg", "secret", {nBits: 128});
console.log(aes.pidcrypt.getParams());
/**
A0_PAD: true
UTF8: true
blockSize: 16
clear: true
dataIn: "abcdefg"
dataOut: "U2FsdGVkX19p8dJctoZSgamat+UBHMWM/Zx64fBDYJY="
decryptIn: ""
decryptOut: ""
encryptIn: Array[16]
encryptOut: "©·åÅýzáðC`"
iv: "e314f8ebbcc5a1e0ae6c27033dd6725f"
key: "49f9bd99b7952b68b3bfa47404455fa7"
nBits: 128
salt: "69f1d25cb6865281"
*/
phpseclib
function hex2raw($strHexString)
{
$strRawBytes="";
$arrChunks=str_split($strHexString, 2);
for($i=0; $i<count($arrChunks); $i++)
$strRawBytes.=chr(hexdec($arrChunks[$i]));
return $strRawBytes;
}
require_once("Crypt/AES.php");
$aes=new Crypt_AES(CRYPT_AES_MODE_CBC);
$aes->setPassword("secret");
$aes->setIV(hex2raw("e314f8ebbcc5a1e0ae6c27033dd6725f"));
var_export($aes->decrypt(base64_decode("U2FsdGVkX19p8dJctoZSgamat+UBHMWM/Zx64fBDYJY=")));
/**
false
*/
我也尝试过phpseclib AES CBC模式“Key Derivation:None”示例(setKey和setIV而不是setPassword),结果相同。
如何让phpseclib解码上述pidCrypt AES CBC输出?
或
有没有替代phpseclib可以解密上面的pidCrypt输出(它有一个代码API;不允许使用OpenSSL用法示例)?
请不要讲授Javascript安全性和网络安全性。这并不意味着防止互联网/网络嗅探。
答案 0 :(得分:2)
在comment on php.net中找到一个使用OpenSSL进行解密的解决方案,删除了OpenSSL部分并将其替换为phpseclib(后者又使用了php的mcrypt或其自己的软件实现)。
require_once("Crypt/AES.php");
//https://www.pidder.com/pidcrypt/?page=demo_aes-cbc
//"U2FsdGVkX18KqDRV4bSqJCauxP7004rQmPu3HD1FUrgbXEGpjJAKAfgYvxtWJ6iJ"
//Used the password: "password".
var_export(pidCryptAESCBCDecrypt("U2FsdGVkX18KqDRV4bSqJCauxP7004rQmPu3HD1FUrgbXEGpjJAKAfgYvxtWJ6iJ", "password"));
//Outputs: "Some text to encrypt..."
function pidCryptAESCBCDecrypt($strBase64OpenSSLCompatibleCipher, $strPassword)
{
$data=base64_decode($strBase64OpenSSLCompatibleCipher);
$salt=substr($data, 8, 8);
$ct=substr($data, 16);
/**
* From https://github.com/mdp/gibberish-aes
*
* Number of rounds depends on the size of the AES in use
* 3 rounds for 256
* 2 rounds for the key, 1 for the IV
* 2 rounds for 128
* 1 round for the key, 1 round for the IV
* 3 rounds for 192 since it's not evenly divided by 128 bits
*/
$rounds = 3;
$data00 = $strPassword.$salt;
$md5_hash = array();
$md5_hash[0] = md5($data00, true);
$result = $md5_hash[0];
for ($i = 1; $i < $rounds; $i++) {
$md5_hash[$i] = md5($md5_hash[$i - 1].$data00, true);
$result .= $md5_hash[$i];
}
$key = substr($result, 0, 32);
$iv = substr($result, 32,16);
$aes=new Crypt_AES(CRYPT_AES_MODE_CBC);
$aes->setKey($key);
$aes->setIV($iv);
echo $aes->decrypt($ct);
}
更新:更简单(不相信我没看到),发送到phpseclib pidCryptUtil.encodeBase64(aes.pidcrypt.getParams().encryptOut)
,aes.pidcrypt.getParams().iv
和aes.pidcrypt.getParams().key
。
答案 1 :(得分:2)
OKAY 所以在很多的头部刮伤和撞击墙壁之后,我想我终于设法找到了一种既有效又有效的解决方案,但我还是遇到了一个奇怪的异象,想知道我实际上做错了什么。我一直在努力实现的是AES 256加密PHP中的字符串的能力(我想用MCrypt而不是唉),然后使用PidCrypt(JQuery)解密相同的字符串。在我发现这个帖子之前,找到满足我需求的解决方案几乎是不可能的。
但即使在成功实施 DECRYPT 功能(使用Crypt_AES PHP库以及仅使用openssl_decrypt / encrypt)后,我仍然无法正确解密消息在PidCrypt中用PHP加密。
这就是我所拥有的(请注意我在https://sourceforge.net/projects/pidcrypt/使用AES演示页面通过PidCrypt加密/解密消息 - 256 AES)。
这是适用于我的PHP代码:
//for those who can't use openssl_encrypt because they don't have PHP >= 5.3.X
//the only solution I've found is to install phpseclib.sourceforge.net/crypt
require_once("Crypt/AES.php");
echo decrypt('TEST', 'U2FsdGVkX1/yPcPIrEGSGvkII+qMeyrOY+l3t09Gylc1td80g0VFtzRw1MbjYJZ3I1JJOW9G6wMlUxpu2gjHq68fwxo3Bds+PlpCnjgDBwL+VZe8QSOwS/CMPh9A+cOkyGQ+fCNp/KnKwgkqTziCpSWjgbFSlcOHtMWPoy1ecMM+CjhzyUZQ6SfJs+K8x9tj');
echo '<br />';
echo encrypt('TEST', 'TESTING RANDOM DATA PLEASE EXPLAIN WHY I HAVE TO PUT AN ADDITIONAL BYTE ON THE END OF THE DATA TO DECRYPT THE ORIGINAL STRING?');
function decrypt($password, $edata) {
$data = base64_decode($edata);
$salt = substr($data, 8, 8);
$ct = substr($data, 16);
$rounds = 3;
$data00 = $password . $salt;
$md5_hash = array();
$md5_hash[0] = md5($data00, true);
$result = $md5_hash[0];
for($i = 1; $i < $rounds; $i++) {
$md5_hash[$i] = md5($md5_hash[$i - 1] . $data00, true);
$result .= $md5_hash[$i];
}
$key = substr($result, 0, 32);
$iv = substr($result, 32, 16);
//phpseclib specifics
$aes = new Crypt_AES(CRYPT_AES_MODE_CBC);
$aes->setKey($key);
$aes->setIV($iv);
return $aes->decrypt($ct);
//instead of using phpseclib with openssl_decrypt you would do the following
//return openssl_decrypt($ct, 'aes-256-cbc', $key, true, $iv);
}
function encrypt($password, $data) {
//my randomly generated 8 byte salt
$salt = substr(time(), -8);
//alternatively with openssl you would use
//$salt = openssl_random_pseudo_bytes(8);
$salted = '';
$dx = '';
while(strlen($salted) < 48) {
$dx = md5($dx . $password . $salt, true);
$salted .= $dx;
}
$key = substr($salted, 0, 32);
$iv = substr($salted, 32, 16);
//phpseclib specifics
$aes = new Crypt_AES(CRYPT_AES_MODE_CBC);
$aes->setKey($key);
$aes->setIV($iv);
$encrypted_data = $aes->encrypt($data . "\n");
//alternatively with openssl you would do the following
//$encrypted_data = openssl_encrypt($data . "\n", 'aes-256-cbc', $key, true, $iv);
return base64_encode('Salted__' . $salt . $encrypted_data);
}
现在会有人请告诉我为什么我要包含一个额外的角色,无论它是什么,只需添加到我的最后$ data要加密,否则当我去解密PidCrypt网站上的字符串时,如果我没有包含附加字符(这是我的情况是换行符)那么它将切断加密的最后一个字符字符串?
只要我没有做错事,我就可以添加额外的角色。如果这是PHP和PidCrypt之间的错误,那么只要它有效,我就会不管它......