堆積樹定義

Definition of Heap Tree

表示法

Representation

堆積樹-建置

Heap Tree - Construction

堆積樹-插入

Heap Tree - Insertion

堆積樹-取用

Heap Tree - Take Out

Heap sort 堆積排序法

雜湊定義和應用介紹

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:

  1. Two distinct, large prime numbers p and q are selected[4].
  2. n is calculated as n = p * q[4].
  3. The totient φ(n) is calculated as φ(n) = (p-1) * (q-1)[4].
  4. An encryption exponent e is chosen such that 1 < e < φ(n) and gcd(e, φ(n)) = 1[4].
  5. The private exponent 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].