Как зашифровать сообщения с ECC в pycryptodom - PullRequest
0 голосов
/ 26 мая 2018

Я использую гибридное шифрование (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)

, но я хочу зашифровать ключ сеансас открытым ключом, как это сделать с pycryptodome или любым другим способом

1 Ответ

0 голосов
/ 29 октября 2018

Pycryptodome не поддерживает шифрование на основе эллиптических кривых (ECC-шифрование).

Вместо этого используйте алгоритм ECIES , например, этот Pythonбиблиотека: https://github.com/kigawas/eciespy

ECIES (схема интегрированного шифрования по эллиптической кривой) - это гибридная схема шифрования , которая объединяет криптографию с открытым ключом ECC для асимметричного шифрования сеансового ключа, используется позже для шифрования входных данных с помощью симметричного шифра (например, с помощью AES-GCM).

...