def encrypt(message, receiver_public_key): sender_private_key = ec.generate_private_key(ec.SECP256K1(), backend) shared_key = sender_private_key.exchange(ec.ECDH(), receiver_public_key) sender_public_key = sender_private_key.public_key() point = sender_public_key.public_numbers().encode_point() iv = '000000000000' xkdf = x963kdf.X963KDF(algorithm=hashes.SHA256(), length=32, sharedinfo='', backend=backend) key = xkdf.derive(shared_key) encryptor = Cipher(algorithms.AES(key), modes.GCM(iv), backend=backend).encryptor() ciphertext = encryptor.update(message) + encryptor.finalize() return point + encryptor.tag + ciphertext
def encrypt(message, receiver_public_key): sender_private_key = ec.generate_private_key(ec.SECP256K1(), backend) shared_key = sender_private_key.exchange(ec.ECDH(), receiver_public_key) sender_public_key = sender_private_key.public_key() point = sender_public_key.public_bytes( encoding=serialization.Encoding.X962, format=serialization.PublicFormat.UncompressedPoint) iv = '000000000000'.encode() xkdf = x963kdf.X963KDF(algorithm=hashes.SHA256(), length=32, sharedinfo=''.encode(), backend=backend) key = xkdf.derive(shared_key) encryptor = Cipher(algorithms.AES(key), modes.GCM(iv), backend=backend).encryptor() ciphertext = encryptor.update(message.encode()) + encryptor.finalize() return point + encryptor.tag + ciphertext
def decrypt(message, receiver_private_key): point = message[0:65] tag = message[65:81] ciphertext = message[81:] sender_public_key = ec.EllipticCurvePublicKey.from_encoded_point( ec.SECP256K1(), point) shared_key = receiver_private_key.exchange(ec.ECDH(), sender_public_key) iv = '000000000000'.encode() xkdf = x963kdf.X963KDF(algorithm=hashes.SHA256(), length=32, sharedinfo=''.encode(), backend=backend) key = xkdf.derive(shared_key) decryptor = Cipher(algorithms.AES(key), modes.GCM(iv, tag), backend=backend).decryptor() message = decryptor.update(ciphertext) + decryptor.finalize() return message
def encrypt( message: object, private_key: str, receiver_public_key: str) -> str: """ encrypts message @param message: (object) message to encrypt @param private_key: (str) private key to encrypt message with @param receiver_public_key: (str) public key of person able to decrypt message """ iv = '000000000000'.encode('utf-8') private_key_bytes = serialization.load_pem_private_key( private_key.encode('utf-8'), password=None, backend=default_backend() ) receiver_public_key = serialization.load_pem_public_key( receiver_public_key.encode('utf-8'), backend=default_backend() ) shared_key = private_key_bytes.exchange(ec.ECDH(), receiver_public_key) point = private_key_bytes.public_key().public_numbers().encode_point() xkdf = x963kdf.X963KDF( algorithm=hashes.SHA256(), length=32, sharedinfo=''.encode('utf-8'), backend=default_backend() ) key = xkdf.derive(shared_key) encryptor = Cipher( algorithms.AES(key), modes.GCM(iv), backend=default_backend() ).encryptor() padded_message = Crypt._pad_data(json.dumps(message)) ciphertext = encryptor.update(padded_message) + encryptor.finalize() complete_ciphertext = point + encryptor.tag + ciphertext return Bytes.encode_bytes(complete_ciphertext).decode('utf-8')
def decrypt(message: str, receiver_private_key: str) -> str: """ decrypts message @param message: (str) message to decrypt @param receiver_private_key: (str) private key to decrypt with """ message = Bytes.decode_bytes(message.encode('utf-8')) point = message[0:65] tag = message[65:81] ciphertext = message[81:] receiver_private_key_bytes = serialization.load_pem_private_key( receiver_private_key.encode('utf-8'), password=None, backend=default_backend() ) sender_public_numbers = ec.EllipticCurvePublicNumbers.from_encoded_point( ec.SECP256K1(), point) sender_public_key = sender_public_numbers.public_key(default_backend()) shared_key = receiver_private_key_bytes.exchange( ec.ECDH(), sender_public_key ) iv = '000000000000'.encode('utf-8') xkdf = x963kdf.X963KDF( algorithm=hashes.SHA256(), length=32, sharedinfo=''.encode('utf-8'), backend=default_backend() ) key = xkdf.derive(shared_key) decryptor = Cipher( algorithms.AES(key), modes.GCM(iv, tag), backend=default_backend() ).decryptor() dt = decryptor.update(ciphertext) + decryptor.finalize() return Crypt._unpad_data(dt).decode('utf-8')