added lab2 questions
This commit is contained in:
parent
857ebf4a77
commit
a1b2495e47
5 changed files with 99 additions and 11 deletions
|
@ -1,23 +1,18 @@
|
|||
from Crypto.Cipher import DES
|
||||
from Crypto.Util.Padding import pad, unpad
|
||||
|
||||
def des_cipher(key):
|
||||
return DES.new(key.encode('utf-8'), DES.MODE_ECB)
|
||||
|
||||
def despad_ptext(text):
|
||||
n = len(text) % 8
|
||||
if n != 0:
|
||||
return text + (' ' * (8 - n))
|
||||
else:
|
||||
return text
|
||||
|
||||
def des_en(ptext, key):
|
||||
cipher = des_cipher(key)
|
||||
ptext = despad_ptext(ptext).encode('utf-8')
|
||||
return cipher.encrypt(ptext)
|
||||
padded_text = pad(ptext.encode('utf-8'), DES.block_size)
|
||||
return cipher.encrypt(padded_text)
|
||||
|
||||
def des_de(ctext, key):
|
||||
cipher = des_cipher(key)
|
||||
return cipher.decrypt(ctext).decode('utf-8').rstrip()
|
||||
decrypted = unpad(cipher.decrypt(ctext), DES.block_size)
|
||||
return decrypted.decode('utf-8')
|
||||
|
||||
def despad_key(key):
|
||||
return key.ljust(8)[:8]
|
||||
|
@ -25,7 +20,6 @@ def despad_key(key):
|
|||
def main():
|
||||
print("Welcome to DES (Original)")
|
||||
ptext = input("Enter plaintext: ")
|
||||
despad_ptext(ptext)
|
||||
key = input("Enter key: ")
|
||||
key = despad_key(key)
|
||||
ctext = des_en(ptext, key)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue