日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php openssl加密数据长度,PHP使用openssl解密数据(用mcrypt加密)

發布時間:2023/12/10 php 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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加密)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。