如何在python中解密ECC(基于cc的混合加密)



我尝试遵循这篇关于ECC混合加密的文章,我做了所有事情,但我未能将公钥发送到另一方来解密图像。

裁判:https://cryptobook.nakov.com/asymmetric-key-ciphers/ecc-encryption-decryption ecc-based-hybrid-encryption-decryption-example-in-python

当我尝试使用ciphertextPubKey时,我应该做些什么来解密图像作为库。

当我在内存中打印ciphertextPubKey时,它是这样的

的结果在运行时工作正常。输入图片描述

我还添加了必要的信息(authTag,nonce,ciphertextPubKey)结束的文件,如

D��d�nonce:c1b340952d625c29b733c18d747da1a1authTag:7d64322d11887ff7a660e7cac15182ddciphertextPubKey:0x73783af49ec67734e390f4e
(53416237437808318183671035859997439596368087928543682406945105437476683600844, 32944848609008475717230473932056561595117227191583483133382911397576230043540) on "brainpoolP256r1" => y^2 = x^3 + 56698187605326110043627228396178346077120614539475214109386828188763884139993x + 17577232497321838841075697789794520262950426058923084567046852300633325438902 (mod 76884956397045344220809746629001649093037950200943055203735601445031516197751)

同样,密文PubKey是ec。点对象当我打印。

这是库的链接:https://github.com/alexmgr/tinyec

这是用于解密

的函数
def decrypt_ECC(encryptedMsg, privKey):
(ciphertext, nonce, authTag, ciphertextPubKey) = encryptedMsg
sharedECCKey = privKey * ciphertextPubKey
secretKey = ecc_point_to_256_bit_key(sharedECCKey)
plaintext = decrypt_AES_GCM(ciphertext, nonce, authTag, secretKey)
return plaintext

主要问题是我如何导出这些密钥并将它们发送给接收者解密?

我试图反向获取密钥,我得到了相同的密钥,但有一个错误

privKey = 73825439940174196720472396443747486663228376337080507389815193133315618892552
pubKey = privKey * curve.g
ciphertextPrivKey = secrets.randbelow(curve.field.n)
ciphertextPubKeyY = ciphertextPrivKey * curve.g
ciphertextPubKey : b'0x6f31f88920caca9ba1d6507c58e0529e57c4d132fdbfc59f3f83f703f2881a4e1'

ciphertextPubKey : b'0x6f31f88920caca9ba1d6507c58e0529e57c4d132fdbfc59f3f83f703f2881a4e1'

错误:

Traceback (most recent call last):
File "testEncryption.py", line 146, in <module>
decryptedMsg = decrypt_ECC(encryptedMsg, privKey)
File "testEncryption.py", line 59, in decrypt_ECC
plaintext = decrypt_AES_GCM(ciphertext, nonce, authTag, secretKey)
File "testEncryption.py", line 37, in decrypt_AES_GCM
plaintext = aesCipher.decrypt_and_verify(ciphertext, authTag)
File "/home/yashazem/.local/lib/python3.7/site-packages/Crypto/Cipher/_mode_gcm.py", line 567, in decrypt_and_verify
self.verify(received_mac_tag)
File "/home/yashazem/.local/lib/python3.7/site-packages/Crypto/Cipher/_mode_gcm.py", line 508, in verify
raise ValueError("MAC check failed")
ValueError: MAC check failed

解决方案是我忘记将十六进制字节转换为字节,因为这个函数。

'nonce': binascii.hexlify(encryptedMsg[1]),

我所做的就是

nonceValue = nonce.decode("utf8").split(":")[1].encode()
authTagValue = authTag.decode("utf8").split(":")[1].encode()

转换而来
b'3de2718f718b23135f5a40fe1e737b61'

b'=xe2qx8fqx8b#x13_Z@xfex1es{a'

最新更新