【问题标题】:how to decrypt ECC in python (CC-Based Hybrid Encryption)如何在python中解密ECC(基于CC的混合加密)
【发布时间】:2021-11-10 18:05:32
【问题描述】:

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

参考:https://cryptobook.nakov.com/asymmetric-key-ciphers/ecc-encryption-decryption#ecc-based-hybrid-encryption-decryption-example-in-python

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

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

运行时结果正常。 enter image description here

我还添加了必要的信息(authTag、nonce、ciphertextPubKey)像这样的文件结尾

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

另外,当我打印类型时,密文 PubKey 是 ec.Point Object

这是图书馆的链接: 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

【问题讨论】:

  • 所以这是公钥加密的基本原则。您可以将公钥发布到某个地方,以便任何人都可以获取它,然后验证您是否使用您的私钥加密了某些内容。他们还可以使用您的私钥加密某些内容,然后只有您可以解密它。这就是为什么只有你应该拥有私钥,但他们拥有公钥。

标签: python encryption cryptography encryption-asymmetric


【解决方案1】:

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

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

所以我做了什么

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

它转换自

b'3de2718f718b23135f5a40fe1e737b61'

b'=\xe2q\x8fq\x8b#\x13_Z@\xfe\x1es{a'

【讨论】:

    猜你喜欢
    • 2018-03-13
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    • 2015-04-21
    • 2021-06-21
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多