from Crypto.Cipher import DES
from Crypto.Random import get_random_bytes
def pad(text):
while len(text) % 8 != 0:
text += ' '
return text
def des_encrypt(key, plaintext):
des = DES.new(key, DES.MODE_ECB)
padded_text = pad(plaintext)
encrypted_text = des.encrypt(padded_text.encode('utf-8'))
return encrypted_text
def des_decrypt(key, encrypted_text):
des = DES.new(key, DES.MODE_ECB)
decrypted_text = des.decrypt(encrypted_text).decode('utf-8').rstrip()
return decrypted_text
key = get_random_bytes(8)
plaintext = "DES Algoritum"
print("Original Text:", plaintext)
encrypted = des_encrypt(key, plaintext)
print("Encrypted Text:", encrypted)
decrypted = des_decrypt(key, encrypted)
print("Original/Decriptiran Text:", decrypted)
ZnJvbSBDcnlwdG8uQ2lwaGVyIGltcG9ydCBERVMKZnJvbSBDcnlwdG8uUmFuZG9tIGltcG9ydCBnZXRfcmFuZG9tX2J5dGVzCgpkZWYgcGFkKHRleHQpOgogICAgd2hpbGUgbGVuKHRleHQpICUgOCAhPSAwOgogICAgICAgIHRleHQgKz0gJyAnCiAgICByZXR1cm4gdGV4dAoKZGVmIGRlc19lbmNyeXB0KGtleSwgcGxhaW50ZXh0KToKICAgIGRlcyA9IERFUy5uZXcoa2V5LCBERVMuTU9ERV9FQ0IpIAogICAgcGFkZGVkX3RleHQgPSBwYWQocGxhaW50ZXh0KQogICAgZW5jcnlwdGVkX3RleHQgPSBkZXMuZW5jcnlwdChwYWRkZWRfdGV4dC5lbmNvZGUoJ3V0Zi04JykpCiAgICByZXR1cm4gZW5jcnlwdGVkX3RleHQKCmRlZiBkZXNfZGVjcnlwdChrZXksIGVuY3J5cHRlZF90ZXh0KToKICAgIGRlcyA9IERFUy5uZXcoa2V5LCBERVMuTU9ERV9FQ0IpCiAgICBkZWNyeXB0ZWRfdGV4dCA9IGRlcy5kZWNyeXB0KGVuY3J5cHRlZF90ZXh0KS5kZWNvZGUoJ3V0Zi04JykucnN0cmlwKCkKICAgIHJldHVybiBkZWNyeXB0ZWRfdGV4dAoKa2V5ID0gZ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