php openssl加密数据长度,PHP使用openssl解密数据(用mcrypt加密)
所以我不需要評論3DES不安全和ECB不好等等,我們知道,這就是為什么我們試圖解密,以有一個更好的加密算法。
我在下面提供了使用mcrypt進行加密的代碼,以及我們試圖使用的1行代碼(openssl)對其進行解密。它總是返回false,我們想知道為什么。
我開始懷疑問題是mcrypt庫使用了8字節的IV,而openssl說它必須是0字節。
如果您能找到使用openssl解密這些值的方法,我們將不勝感激。
提前謝謝。
代碼如下:
$sEncryptionKey = 'aaaabbbbccccddddeeeeffff';
$sDataToEncrypt = 'Foo bar';
echo "Data to be Encrypted: $sDataToEncrypt\n";
$rMcrypt = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_ECB, '');
$iIvSize = mcrypt_enc_get_iv_size($rMcrypt); //This gives 8 bytes
$sInitializationVector = mcrypt_create_iv($iIvSize, MCRYPT_RAND);
$iKeySize = mcrypt_enc_get_key_size($rMcrypt);
if ($iKeySize != strlen($sEncryptionKey)) {
throw new Exception ('Invalid key length: '.$iKeySize);
}
mcrypt_generic_init($rMcrypt, $sEncryptionKey, $sInitializationVector);
$sEncryptedString = base64_encode(mcrypt_generic($rMcrypt, $sDataToEncrypt));
echo "Data Encrypted: $sEncryptedString\n";
$sDecryptedString = trim(mdecrypt_generic($rMcrypt, base64_decode($sEncryptedString)));
echo "Data Decrypted: $sDecryptedString\n";
mcrypt_generic_deinit($rMcrypt);
mcrypt_module_close($rMcrypt);
$sDecryptedString2 = openssl_decrypt(base64_decode($sEncryptedString), 'des-ede3', $sEncryptionKey, 0, ''); //this returns false.
echo "Data Decrypted (open SSL): $sDecryptedString2\n";
$sDecryptedString2 = openssl_decrypt(base64_decode($sEncryptedString), 'des-ede3', $sEncryptionKey, 0, $sInitializationVector); //Warning: openssl_decrypt(): IV passed is 8 bytes long which is longer than the 0 expected by selected cipher, truncating
?>
Data to be Encrypted: Foo bar
Data Encrypted: 5Mraf9swmaI=
Data Decrypted: Foo bar
Data Decrypted (open SSL):
Warning: openssl_decrypt(): IV passed is 8 bytes long which is longer than the 0 expected by selected cipher, truncating in /usr/local/www/appcluster01.ezmax.ca/pub/web/test/ian/test.cmd on line 31
總結
以上是生活随笔為你收集整理的php openssl加密数据长度,PHP使用openssl解密数据(用mcrypt加密)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 45.98万买500万内最好家用SUV!
- 下一篇: java 股票 代码_Java中利用散列