1 | secrets |
2 | |
3 | curve = registry.get_curve('brainpoolP256r1') |
4 | |
5 | def compress_point(point): |
6 | return hex(point.x) + hex(point.y % 2)[2:] |
7 | |
8 | def ecc_calc_encryption_keys(pubKey): |
9 | ciphertextPrivKey = secrets.randbelow(curve.field.n) |
10 | ciphertextPubKey = ciphertextPrivKey * curve.g |
11 | sharedECCKey = pubKey * ciphertextPrivKey |
12 | return (sharedECCKey, ciphertextPubKey) |
13 | |
14 | def ecc_calc_decryption_key(privKey, ciphertextPubKey): |
15 | sharedECCKey = ciphertextPubKey * privKey |
16 | return sharedECCKey |
17 | |
18 | privKey = secrets.randbelow(curve.field.n) |
19 | pubKey = privKey * curve.g |
20 | print("private key:", hex(privKey)) |
21 | print("public key:", compress_point(pubKey)) |
22 | |
23 | (encryptKey, ciphertextPubKey) = ecc_calc_encryption_keys(pubKey) |
24 | print("ciphertext pubKey:", compress_point(ciphertextPubKey)) |
25 | print("encryption key:", compress_point(encryptKey)) |
26 | |
27 | decryptKey = ecc_calc_decryption_key(privKey, ciphertextPubKey) |
28 | print("decryption key:", compress_point(decryptKey)) |
29 | Run the above code example: |