added lab2 questions

This commit is contained in:
Student 2025-08-05 09:59:28 +05:30
parent 857ebf4a77
commit a1b2495e47
5 changed files with 99 additions and 11 deletions

31
IS/Lab/Lab2/3des.py Normal file
View file

@ -0,0 +1,31 @@
from Crypto.Cipher import DES3
from Crypto.Util.Padding import pad, unpad
def des3_cipher(key):
return DES3.new(key.encode('utf-8'), DES3.MODE_ECB)
def des3_en(ptext, key):
cipher = des3_cipher(key)
padded_text = pad(ptext.encode('utf-8'), DES3.block_size)
return cipher.encrypt(padded_text)
def des3_de(ctext, key):
cipher = des3_cipher(key)
decrypted = unpad(cipher.decrypt(ctext), DES3.block_size)
return decrypted.decode('utf-8')
def despad_key(key):
key = key.ljust(24)[:24]
return key
def main():
print("Welcome to 3DES (Triple DES)")
ptext = input("Enter plaintext: ")
key = input("Enter key (minimum 16 characters for 3DES): ")
key = despad_key(key)
ctext = des3_en(ptext, key)
print("Your ciphertext: ", ctext)
print("Your decrypted plaintext: ", des3_de(ctext, key))
if __name__ == '__main__':
main()