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,33 @@
|
||||
from Crypto.Cipher import AES
|
||||
from Crypto.Util.Padding import pad
|
||||
from Crypto.Random import get_random_bytes
|
||||
|
||||
|
||||
# Generate new secret key
|
||||
with open('key', 'wb') as f:
|
||||
k1 = get_random_bytes(16)
|
||||
f.write(k1)
|
||||
|
||||
plaintext = input("Please input the message: ")
|
||||
|
||||
# Pad plaintext
|
||||
plaintext = plaintext.encode()
|
||||
|
||||
plaintext = pad(plaintext, 16)
|
||||
|
||||
# Read Key
|
||||
with open('key', 'rb') as f:
|
||||
aes_key = f.read()
|
||||
|
||||
assert k1 == aes_key
|
||||
|
||||
# Encrypt and send (write) message
|
||||
cipher = AES.new(aes_key, AES.MODE_CBC, iv=b'0'*16)
|
||||
|
||||
ciphertext = cipher.encrypt(plaintext)
|
||||
|
||||
with open('ctext', 'wb') as f:
|
||||
f.write(ciphertext)
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
from Crypto.Cipher import AES
|
||||
from Crypto.Util.Padding import unpad
|
||||
|
||||
# Read Key
|
||||
with open('key', 'rb') as f:
|
||||
aes_key = f.read()
|
||||
|
||||
cipher = AES.new(aes_key, AES.MODE_CBC, iv=b'0'*16)
|
||||
|
||||
# Read message and decrypt
|
||||
|
||||
with open('ctext', 'rb') as f:
|
||||
ciphertext = f.read()
|
||||
|
||||
plaintext = unpad(cipher.decrypt(ciphertext), 16).decode()
|
||||
|
||||
print(f'Plaintext: {plaintext}')
|
||||
|
||||
Reference in new issue
Block a user