亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用AES-256-CCM通過python在php中解密加密文本

如何使用AES-256-CCM通過python在php中解密加密文本

PHP
米琪卡哇伊 2023-03-11 16:48:51
AES-256-CCM我正在嘗試解密使用Python加密的 PHP 密文,cryptography.hazmat我在 Python 代碼中所做的是:from cryptography.hazmat.primitives.ciphers.aead import AESCCMfrom os import urandomimport base64#Text To Encryptplaintext = bytes("message from python", encoding='utf-8')#AES 256 Key Genratorkey = AESCCM.generate_key(256)#Genrate Noncenonce= urandom(12)#chipher cipher = AESCCM(key, tag_length=8)#Encryptionciphertext = cipher.encrypt(nonce, plaintext, None)key然后我將,nonce和轉換ciphertext為 base64key_b64 = base64.standard_b64encode(key)ciphertext_b64 = base64.standard_b64encode(ciphertext)nonce_b64 = base64.standard_b64encode(nonce)在我的例子中我得到了這個結果key = b'\xcb\x14\x96{,0(\x15\x86 \xda\xf8\x1b"i|M\xbd\xc5d\xe7\xa6I\xdf\x7f\xe11\xae\xe8\x8a\xb3j'key_b64 = b'yxSWeywwKBWGINr4GyJpfE29xWTnpknff+ExruiKs2o='nonce = b'\xc7f\xdc\xe3\xe4\x03>M\x9by\x92\x9dnonce_b64 = b'x2bc4+QDPk2beZKd'ciphertext = b'R\x9f\xe6D\\_\xdexC\x82\xf8\x8e\x9b;\x91\xc7OLo\xc2\t/\x8fV>G='ciphertext_b64 = b'Up/mRFxf3nhDgviOmzuRx09Mb8IJL49WPkc9'我在我的 PHP 代碼中使用 base64 結果<?php$key_from_python = base64_decode('yxSWeywwKBWGINr4GyJpfE29xWTnpknff+ExruiKs2o=');$ciphertext_from_python = base64_decode('ooGUzo0YiwKPs9+2wXySYEpdBNfSpyLUHm1M');$nonce_from_python = base64_decode('Up/x2bc4+QDPk2beZKd');$cipher = "aes-256-ccm";if (in_array($cipher, openssl_get_cipher_methods())){$ivlen = openssl_cipher_iv_length($cipher);$iv = openssl_random_pseudo_bytes($ivlen);$decrypted_mesage_from_pythom = openssl_decrypt($encrypted_from_python_,$cipher,$key_from_python,$options=0 , $iv, $tag);echo $decrypted_mesage_from_pythom;}它基于我在這里找到的一個例子http://php.babo.ist/#/en/function.openssl-encrypt.html我找不到另一個例子,解密過程沒有返回任何東西,真正讓我困惑的是:我們沒有使用 IV 在 python 代碼中加密,但是 PHP 需要非 NULL IV,如何解決?PHP 代碼中的什么$tag以及 PHP 和 python 中的 $tag_lenght(cipher = AESCCM(key, tag_length=8)) ?如果解密需要nonce如何在我的 PHP 代碼中使用它?如何獲得這份工作?從 python 加密并在 PHP 中解密相同的密文注意:我必須使用 python 進行加密,使用 php 進行解密,我必須使用 AES-CCM,python 代碼是固定的,謝謝您的理解
查看完整描述

1 回答

?
PIPIONE

TA貢獻1829條經驗 獲得超9個贊

AES-CCM 的 PHP 實現中似乎存在一個字節的隨機數長度12(由 OP 使用)的錯誤,這會導致錯誤的密文/標簽。但是,此錯誤被 OP 的 PHP 代碼中的許多缺陷所隱藏。因此,必須首先修復這些缺陷:

  • Python 和 PHP 實現的不同之處在于,在 Python 代碼中,密文和標簽按此順序連接,而在 PHP 代碼中,密文和標簽是分開處理的。

  • Python 代碼中的 nonce 對應于 PHP 代碼中的 IV。

  • 在 PHP 代碼中,OPENSSL_RAW_DATA如果密文作為原始數據傳遞而不是 Base64 編碼,則必須設置標志。

  • nonce 和 ciphertext (+ tag) 的值在兩個代碼中不同。然而,由于選擇的 12 字節隨機數與 12 字節隨機數的 PHP 錯誤相結合,更正毫無意義,請參見下文。即,成功測試的先決條件是隨機數大小不等于 12 字節。

考慮到這些要點的 PHP 實現是例如

<?php

// Data from Python code

$key_from_python = base64_decode('<Base64 encoded key from Python>');

$ciphertext_from_python = base64_decode('<Base64 encoded (ciphertext + tag) from Python>');

$nonce_from_python = base64_decode('<Base64 encoded nonce from Python>');

$cipher = 'aes-256-ccm';


// Separate ciphertext and tag

$tagLength = 8;

$ciphertext = substr($ciphertext_from_python, 0, -$tagLength);

$tag = substr($ciphertext_from_python, -$tagLength);


// Decrypt

if (in_array($cipher, openssl_get_cipher_methods())){

? ? $decrypted_mesage_from_pythom = openssl_decrypt($ciphertext, $cipher, $key_from_python, OPENSSL_RAW_DATA, $nonce_from_python, $tag);

? ? echo $decrypted_mesage_from_pythom;

}

?>

使用此 PHP 代碼,只要 nonce 的長度不等于12bytes,就可以從 Python 代碼解密數據。


Python 和 PHP 實現允許長度為7to13字節(包括兩者),s 的隨機數。這里是 Python。關于字節 nonce 的問題12,結果如下:如果在 PHP 代碼中通過刪除最后一個字節12將字節 nonce 截斷為字節,則會創建相同的密文/標簽。以下 PHP 代碼針對和字節的標記長度說明了這一點(PHP 版本 7.4.4):75816


<?php

function printCiphertextTag($plaintext, $key, $iv, $taglength){

? ? $encrypted = openssl_encrypt($plaintext, "aes-256-ccm", $key, OPENSSL_RAW_DATA, $iv, $tag, NULL, $taglength);

? ? echo sprintf("tag size: %2s, IV size: %2s, IV (hex): %-' 24s, ciphertext (hex): %s, tag (hex): %s\n", $taglength, strlen($iv), bin2hex($iv), bin2hex($encrypted), bin2hex($tag));

}


$plaintext = 'message from python';?

$key = '01234567890123456789012345678901';

$nonce12 = openssl_random_pseudo_bytes(12);

$nonce7 = substr($nonce12, 0, 7);


printCiphertextTag($plaintext, $key, $iv = $nonce12, $taglength = 8);

printCiphertextTag($plaintext, $key, $iv = $nonce7, $taglength = 8);


printCiphertextTag($plaintext, $key, $iv = $nonce12, $taglength = 16);

printCiphertextTag($plaintext, $key, $iv = $nonce7, $taglength = 16);

?>?

此結果表明 PHP 實現中存在錯誤。


與 PHP 代碼相比,Python 代碼為12字節隨機數生成不同的密文/標簽(這就是為什么(更正的)OP 的使用字節12隨機數的 PHP 代碼失敗的原因)。使用具有相同參數的 Java/BC 進行的檢查會為字節隨機數生成與 Python 代碼相同的密文/標簽,從而驗證 Python 代碼的值并再次指示 PHP 實現中的錯誤。12



查看完整回答
反對 回復 2023-03-11
  • 1 回答
  • 0 關注
  • 255 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號