finished hw
This commit is contained in:
1 parent
4797a57f1e
commit
850c34e526
6 files changed
+167
-5
No files matched your search
@@ -0,0 +1,20 @@
|
||||
from Crypto.PublicKey import RSA
|
||||
from Crypto.Cipher import PKCS1_OAEP
|
||||
|
||||
# Read Bob's public key
|
||||
with open("bob_public.pem", "rb") as f:
|
||||
public_key = RSA.import_key(f.read())
|
||||
|
||||
# Get the message from the command line
|
||||
message = input("Enter a message: ").encode()
|
||||
|
||||
# Create a cipher object using Bob's public key
|
||||
cipher = PKCS1_OAEP.new(public_key)
|
||||
|
||||
# Encrypt the message
|
||||
ciphertext = cipher.encrypt(message)
|
||||
|
||||
# Write the ciphertext
|
||||
with open("ctext", "wb") as f:
|
||||
f.write(ciphertext)
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
from Crypto.PublicKey import RSA
|
||||
from Crypto.Cipher import PKCS1_OAEP
|
||||
import os
|
||||
|
||||
# Check if keys already exist
|
||||
if not os.path.exists("bob_private.pem"):
|
||||
key = RSA.generate(2048)
|
||||
|
||||
private_key = key.export_key()
|
||||
public_key = key.publickey().export_key()
|
||||
|
||||
# Save the private key
|
||||
with open("bob_private.pem", "wb") as f:
|
||||
f.write(private_key)
|
||||
|
||||
# Save the public key
|
||||
with open("bob_public.pem", "wb") as f:
|
||||
f.write(public_key)
|
||||
|
||||
print("Generated and saved Bob's public and private keys.")
|
||||
|
||||
if not os.path.exists('ctext'):
|
||||
print('No ctext found. Run alice.py')
|
||||
exit(0)
|
||||
|
||||
# Read the private key
|
||||
with open("bob_private.pem", "rb") as f:
|
||||
private_key = RSA.import_key(f.read())
|
||||
|
||||
# Read the ciphertext
|
||||
with open("ctext", "rb") as f:
|
||||
ciphertext = f.read()
|
||||
|
||||
cipher = PKCS1_OAEP.new(private_key)
|
||||
|
||||
# Decrypt
|
||||
message = cipher.decrypt(ciphertext)
|
||||
print("Decrypted message:", message.decode())
|
||||
Reference in new issue
Block a user