From 0f450672b19c2bd4c501ce1e3c9105919c6d9ba7 Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Tue, 7 Nov 2023 15:07:54 -0700 Subject: [PATCH] Pull out a main function, remove global state. Makes it easier to see what inputs I need. --- temp/ecc-hybrid-encryption-example.py | 38 +++++++++++++++------------ 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/temp/ecc-hybrid-encryption-example.py b/temp/ecc-hybrid-encryption-example.py index a78b6cb..b444a1b 100644 --- a/temp/ecc-hybrid-encryption-example.py +++ b/temp/ecc-hybrid-encryption-example.py @@ -17,9 +17,8 @@ def ecc_point_to_256_bit_key(point): sha.update(int.to_bytes(point.y, 32, 'big')) return sha.digest() -curve = registry.get_curve('brainpoolP256r1') -def encrypt_ECC(msg, pubKey): +def encrypt_ECC(curve, msg, pubKey): ciphertextPrivKey = secrets.randbelow(curve.field.n) sharedECCKey = ciphertextPrivKey * pubKey secretKey = ecc_point_to_256_bit_key(sharedECCKey) @@ -34,20 +33,25 @@ def decrypt_ECC(encryptedMsg, privKey): plaintext = decrypt_AES_GCM(ciphertext, nonce, authTag, secretKey) return plaintext -msg = b'Text to be encrypted by ECC public key and ' \ - b'decrypted by its corresponding ECC private key' -print("original msg:", msg) -privKey = secrets.randbelow(curve.field.n) -pubKey = privKey * curve.g +def main(): + curve = registry.get_curve('brainpoolP256r1') + msg = b'Text to be encrypted by ECC public key and ' \ + b'decrypted by its corresponding ECC private key' + print("original msg:", msg) + privKey = secrets.randbelow(curve.field.n) + pubKey = privKey * curve.g -encryptedMsg = encrypt_ECC(msg, pubKey) -encryptedMsgObj = { - 'ciphertext': binascii.hexlify(encryptedMsg[0]), - 'nonce': binascii.hexlify(encryptedMsg[1]), - 'authTag': binascii.hexlify(encryptedMsg[2]), - 'ciphertextPubKey': hex(encryptedMsg[3].x) + hex(encryptedMsg[3].y % 2)[2:] -} -print("encrypted msg:", encryptedMsgObj) + encryptedMsg = encrypt_ECC(curve, msg, pubKey) + encryptedMsgObj = { + 'ciphertext': binascii.hexlify(encryptedMsg[0]), + 'nonce': binascii.hexlify(encryptedMsg[1]), + 'authTag': binascii.hexlify(encryptedMsg[2]), + 'ciphertextPubKey': hex(encryptedMsg[3].x) + hex(encryptedMsg[3].y % 2)[2:] + } + print("encrypted msg:", encryptedMsgObj) -decryptedMsg = decrypt_ECC(encryptedMsg, privKey) -print("decrypted msg:", decryptedMsg) + decryptedMsg = decrypt_ECC(encryptedMsg, privKey) + print("decrypted msg:", decryptedMsg) + +if __name__ == "__main__": + main()