@ Fred-ii-你絕對沒錯,謝謝!我無法相信我沒有看到...我很樂意給你答案!
按照要求:
這似乎是你使用AES-256-XTS
方法。例如,如果將其更改爲aes128
或AES-128-CBC
,則會看到結果。請查閱文檔http://php.net/manual/en/function.openssl-encrypt.php - 手冊中沒有列出AES-256-XTS
。
編輯:我從之前的編輯中刪除了錯誤報告,我將進一步調查這個問題,爲什麼最終會產生一個空的結果。由於(密碼)方法(實際上)在運行var_dump(openssl_get_cipher_methods());
,時確實存在,所以它也可能依賴於openssl;從錯誤報告中留下的評論中拉取/取出。
我會更新我的答案如果我希望得到結果/解釋爲什麼OP和我自己的測試成爲空白並從OP獲得更多信息。
編輯:
做進一步的研究之後,我偶然發現this link和代碼中使用 「AES-256-XTS」 加密方法奏效「開箱的」 (注意;請參閱結尾處源腳本中的註釋行註釋)。
在查看該代碼並將其與OP進行比較時,我注意到這是需要加密的數據/消息。
- 該消息被散列爲「作爲鍵」,從而使字符串變空。只是散列密鑰而不是密鑰和數據/消息,因爲您試圖使用消息和密鑰「作爲密鑰」加密現有哈希方法。
得到的腳本出來是以下幾點:
旁註:註釋行的正下方,也有各自的部分工作;只是不要同時使用兩者。
$plaintext = 'The secret message in plain text';
$password = '3sc3RLrpd17';
$key = substr(hash('sha256', $password, true), 0, 32);
$iv = openssl_random_pseudo_bytes(16);
$method = "aes-256-xts";
$userName = "JOHN";
$encrypted = base64_encode(openssl_encrypt($plaintext, $method, $key, OPENSSL_RAW_DATA, $iv));
// $encrypted = openssl_encrypt($plaintext, $method, $key, OPENSSL_RAW_DATA, $iv);
echo "<br>";
var_dump($iv);
echo "<br>";
var_dump($userName);
echo "<br>";
var_dump($encrypted);
echo "<hr>";
$decrypted = openssl_decrypt(base64_decode($encrypted), $method, $key, OPENSSL_RAW_DATA, $iv);
// $decrypted = openssl_decrypt($encrypted, $method, $key, OPENSSL_RAW_DATA, $iv);
echo 'decrypted to: ' . $decrypted . "\n\n";
特別說明:我在採摘Jay Blanchard's大腦和測試方面也有一些幫助;兩個頭常常比一個好,所以Jay也值得讚揚。
注意:多個源指示它(「AES-256-XTS」)用於文件系統/磁盤加密。您可以說真正用於文件系統加密的「AES-256-XTS和AES-128-XTS」方法,因此不適用於文本。
源代碼this link拉:
<?php
$plaintext = 'My secret message 1234';
$password = '3sc3RLrpd17';
$method = 'aes-256-cbc'; // I replaced aes-256-cbc with aes-256-xts during testing
// Must be exact 32 chars (256 bit)
$password = substr(hash('sha256', $password, true), 0, 32);
echo "Password:" . $password . "\n";
// IV must be exact 16 chars (128 bit)
$iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0);
// av3DYGLkwBsErphcyYp+imUW4QKs19hUnFyyYcXwURU=
$encrypted = base64_encode(openssl_encrypt($plaintext, $method, $password, OPENSSL_RAW_DATA, $iv));
// My secret message 1234
$decrypted = openssl_decrypt(base64_decode($encrypted), $method, $password, OPENSSL_RAW_DATA, $iv);
echo 'plaintext=' . $plaintext . "\n";
echo 'cipher=' . $method . "\n";
echo 'encrypted to: ' . $encrypted . "\n";
echo 'decrypted to: ' . $decrypted . "\n\n";
這似乎是你使用'AES-256-XTS'的方法。例如,如果將其更改爲「aes128」或「AES-128-CBC」,則會看到結果。請查閱文檔http://php.net/manual/en/function.openssl-encrypt.php - 手冊中沒有列出「AES-256-XTS」。 –
@ Fred-ii-你說的沒錯,謝謝!我無法相信我沒有看到...我很樂意給你答案! –
亞當,*歡呼* –