今天繼續來學習非對稱式加密-RSA
#實作
首先先裝好cryptography
pip install cryptography
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
# Generate an RSA key pair
def generate_rsa_key_pair():
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
backend=default_backend()
)
public_key = private_key.public_key()
return private_key, public_key
# Save the RSA private key to a file
def save_private_key_to_file(private_key, filename):
with open(filename, "wb") as key_file:
key_file.write(private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption()
))
# Save the RSA public key to a file
def save_public_key_to_file(public_key, filename):
with open(filename, "wb") as key_file:
key_file.write(public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
))
# Load the RSA private key from a file
def load_private_key_from_file(filename):
with open(filename, "rb") as key_file:
private_key = serialization.load_pem_private_key(
key_file.read(),
password=None,
backend=default_backend()
)
return private_key
# Load the RSA public key from a file
def load_public_key_from_file(filename):
with open(filename, "rb") as key_file:
public_key = serialization.load_pem_public_key(
key_file.read(),
backend=default_backend()
)
return public_key
# Encrypt plaintext
def encrypt_with_rsa(public_key, plaintext):
ciphertext = public_key.encrypt(
plaintext.encode(),
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
return ciphertext
# Main function
if __name__ == "__main__":
# Generate an RSA key pair
private_key, public_key = generate_rsa_key_pair()
# Save the RSA private key and public key to files
save_private_key_to_file(private_key, "private_key.pem")
save_public_key_to_file(public_key, "public_key.pem")
# Encryption and decryption example
plaintext = "Today is my birthday!"
ciphertext = encrypt_with_rsa(public_key, plaintext)
decrypted_text = decrypt_with_rsa(private_key, ciphertext)
print("private key:",private_key)
print("public key:",public_key)
print("Original text:", plaintext)
print("Encrypted:", ciphertext.hex())
print("Decrypted:", decrypted_text)
private key: <cryptography.hazmat.backends.openssl.rsa._RSAPrivateKey object at 0x7d1051c315a0>
public key: <cryptography.hazmat.backends.openssl.rsa._RSAPublicKey object at 0x7d1051c30730>
Original text: Today is my birthday!
Encrypted: 733abcaf19d330f97e6a7593a063a59ec85b02024bf3564281a8b9c48ee4d4354c3b91673b7716fdf198b92f1028ad254a9ab34ac7da4fb70d1e6ca174044a17cd2686ad404ed9456a841927e1274056ed8e60d0ea2b85a29648d09c9ecbe442750a5c3d86763728d5931be925a386ecfd088aa77072ec91b462a70c0a33007f428e7c6369ff342d25ec840db045426e3c7e9fff2900ca592638d62b5b25495cd0a99a8dc1273deb92cf086a0f4a96f635bd34a3e51f5ef35b14ceb99516e36dafa030e6981d367a279bd099065fe877dd07117bf73e834be7a33405c2e473c63bbd20ad99dcfd414d1bfa64a287043bb7aed7d368070b4da54e572a1d3a45e9
Decrypted: Today is my birthday!
心得:今天我生日= =