我有一個 PHP 庫,它是 Openssl 的包裝器,其目標是讓新開發(fā)人員默認情況下更簡單、更安全地加密和解密數(shù)據(jù)。該庫成功處理許多不同的密碼和模式,但我無法讓 OCB 模式正常工作。我似乎能夠毫無問題地加密數(shù)據(jù),但當我嘗試解密時,它openssl_decrypt()會返回false。當我檢查openssl_error_string()任何錯誤消息時,沒有任何錯誤消息。AES-128-CBC下面是一個 MVCE,演示了使用和執(zhí)行相同的代碼AES-128-OCB。該AES-128-CBC示例按預期工作。沒有AES-128-OCB任何跡象表明失敗的原因。(我在示例中特意使用了 16 個字符的純文本字符串,以從方程中刪除空填充)。$ciphers = [ 'AES-128-CBC', 'AES-128-OCB'];$key = 'secretkey';$plainText = 'Testing testing!';foreach ($ciphers as $cipher) { printf('Cipher: %s%s', $cipher, PHP_EOL); $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($cipher)); printf('IV: %s%s', $iv, PHP_EOL); printf('Text: %s%s', $plainText, PHP_EOL); $encryptedText = openssl_encrypt($plainText, $cipher, $key, OPENSSL_RAW_DATA, $iv); printf('Encrypted Text: %s%s', $encryptedText, PHP_EOL); $encodedText = base64_encode($encryptedText); printf('Encoded Text: %s%s', $encodedText, PHP_EOL); $decodedText = base64_decode($encodedText); printf('Decoded Text: %s%s', $decodedText, PHP_EOL); $decryptedText = openssl_decrypt($decodedText, $cipher, $key, OPENSSL_RAW_DATA, $iv); printf('Decrypted Text: '); var_dump($decryptedText); while ($msg = openssl_error_string()) { printf('Openssl Error: %s%s', $msg, PHP_EOL); } printf('%s%s', str_repeat('-', 60), PHP_EOL);}輸出:Cipher: AES-128-CBCIV: ?K?K?l4.?4;yText: Testing testing!Encrypted Text: vg??~6?D??R?????xd?^?,?[??p"~Encoded Text: dgUIZ5itfjazRLTiUvzIxsjNeGScXqksjFsaq7pwIn4=Decoded Text: vg??~6?D??R?????xd?^?,?[??p"~Decrypted Text: string(16) "Testing testing!"------------------------------------------------------------Cipher: AES-128-OCBIV: ??)?????XText: Testing testing!Encrypted Text: m??i??B[?d?Encoded Text: BW2IkWmo5kJbFgYf8YdkpQ==Decoded Text: m??i??B[?d?Decrypted Text: bool(false)------------------------------------------------------------我不是加密方面的專家,但在從事這項工作時學到了很多知識。我在 google 上搜索了 OCB 模式,并讓它與 PHP 和 Openssl 一起工作,但幾乎沒有關于這個主題的信息。
使用 OCB 模式時 Openssl 解密失敗
函數(shù)式編程
2023-10-15 15:07:40