Definition of Heap Tree
Representation
Heap Tree - Construction
Heap Tree - Insertion
Heap Tree - Take Out
Introduction of Hash
Example code of Hash
Here is an example of RSA encryption and decryption in Python:
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import binascii
# Generate RSA key pair
keyPair = RSA.generate(3072)
# Get public key
pubKey = keyPair.publickey()
print(f"Public key: (n={hex(pubKey.n)}, e={hex(pubKey.e)})")
pubKeyPEM = pubKey.exportKey()
print(pubKeyPEM.decode('ascii'))
# Get private key
privKeyPEM = keyPair.exportKey()
print(f"Private key: (n={hex(pubKey.n)}, d={hex(keyPair.d)})")
print(privKeyPEM.decode('ascii'))
# Encrypt message
msg = b'A message for encryption'
encryptor = PKCS1_OAEP.new(pubKey)
encrypted = encryptor.encrypt(msg)
print("Encrypted:", binascii.hexlify(encrypted))
# Decrypt message
decryptor = PKCS1_OAEP.new(keyPair)
decrypted = decryptor.decrypt(encrypted)
print('Decrypted:', decrypted)
This code uses the pycryptodome library to generate RSA keys, encrypt a message using the public key, and decrypt the message using the private key[1].
The key generation process is as follows:
p and q are selected[4].n is calculated as n = p * q[4].φ(n) is calculated as φ(n) = (p-1) * (q-1)[4].e is chosen such that 1 < e < φ(n) and gcd(e, φ(n)) = 1[4].d is calculated such that d * e ≡ 1 (mod φ(n))[4].The public key is (e, n) and the private key is (d, n)[4].
To encrypt a message M, it is converted to a number less than n. The ciphertext C is calculated as C = M^e (mod n)[4].
To decrypt, the ciphertext C is raised to the power of d modulo n to recover the original message M = C^d (mod n)[4].