python - 如何在 pycryptodome 中使用 ECC 加密消息

标签 python encryption pycrypto

我正在使用混合加密(RSA+AES),但长度很大,现在我想使用 ECC 而不是 RSA,但在 pycryptodom 中没有实现。 这是我的 RSA 代码

def generate_keys():
    key = RSA.generate(1024)
    private_key = key.exportKey(format='PEM', pkcs=8, 
                  protection="scryptAndAES128-CBC")
    f = open("private_key.pem", "wb")
    f.write(private_key)
    public_key = key.publickey().exportKey('PEM')
    f = open("public_key.pem", "wb")
    f.write(public_key)
    f.close()

def encrypt(username, msg):
    #get the reciever's public key
    f = open("{}.pem".format(username)) # a.salama.pem
    recipient_key = RSA.import_key(f.read())
    f.close()

    # Encrypt the session key with the reciever's public RSA key
    cipher_rsa = PKCS1_OAEP.new(recipient_key)

    # Encrypt the data with the AES session key
    session_key = get_random_bytes(16)

    cipher_aes = AES.new(session_key, AES.MODE_EAX)
    ciphertext, tag = cipher_aes.encrypt_and_digest(msg.encode('utf-
                    8'))
    encrypted_data = cipher_rsa.encrypt(session_key) + 
    cipher_aes.nonce + tag +  ciphertext    
    encrypted_data = base64.b64encode(encrypted_data)
    return encrypted_data

在尝试使用 ECC+AES 之后,代码将是

from Crypto.PublicKey import ECC
from Crypto.Random import get_random_bytes
from Crypto.Cipher import AES, PKCS1_OAEP
import base64

def generate_keys():
    key = ECC.generate(curve='P-256') #3072 RSA 
    private_key = key.export_key(format='PEM')
    f = open('private_key.pem','wt')
    f.write(private_key)
    f.close()

    public_key = key.public_key().export_key(format='PEM')
    f = open('public_key.pem','wt')
    f.write(public_key)
    f.close()

def encrypt(username, msg):
    #get the reciever's public key
    f = open("{}.pem".format(username), 'rt') # a.salama.pem
    recipient_key = ECC.import_key(f.read())
    f.close()

    # Encrypt the session key with the reciever's public RSA key
    cipher_rsa = PKCS1_OAEP.new(recipient_key)

    # Encrypt the data with the AES session key
    session_key = get_random_bytes(16)
    #we use the EAX mode to allow detection of unauthorized 
    modifications.  
    cipher_aes = AES.new(session_key, AES.MODE_EAX)
    ciphertext, tag = cipher_aes.encrypt_and_digest(msg.encode('utf-
                      8'))
    encrypted_data = cipher_rsa.encrypt(session_key) + 
    cipher_aes.nonce + tag +  ciphertext    
    encrypted_data = base64.b64encode(encrypted_data)

    return encrypted_data.decode()

这让我在这一行出错

cipher_rsa = PKCS1_OAEP.new(recipient_key)

但我想用公钥加密 session key ,如何使用 pycryptodome 或任何其他方式做到这一点

最佳答案

Pycryptodome不支持基于椭圆曲线的加密(ECC 加密)。

使用 ECIES 算法,例如这个 Python 库:https://github.com/kigawas/eciespy

ECIES(椭圆曲线集成加密方案)是混合加密方案,它结合了ECC 公钥密码学 对 session 进行非对称加密 key ,稍后用于使用对称密码(例如使用 AES-GCM)对输入数据进行加密。

关于python - 如何在 pycryptodome 中使用 ECC 加密消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50538729/

相关文章:

python - 将输入文件的一行中的每个字符添加到列表中,并将每个列表添加到每行之后的另一个列表中

python - multiprocessing.cpu_count 返回错误的核心数

c# - 如何使用带有 RSA 私钥的 RS256 对 JWT 进行签名

c++ - block 密码

encryption - .png 和 .jpg 文件的解密

Python : What is the difference between matplotlib. 图像和 scipy.misc?

python - 类在类属性中引用自身的最佳方式是什么?

google-app-engine - 在 Google App Engine 的 dev_appserver.py 中导入 pycrypto 会出现 IOError

python - 在Python中存储和读取文件中的加密字符串

python pycrypto安装错误