fork download
  1. from Crypto.Cipher import DES
  2. from Crypto.Random import get_random_bytes
  3.  
  4. def pad(text):
  5. while len(text) % 8 != 0:
  6. text += ' '
  7. return text
  8.  
  9. def des_encrypt(key, plaintext):
  10. des = DES.new(key, DES.MODE_ECB)
  11. padded_text = pad(plaintext)
  12. encrypted_text = des.encrypt(padded_text.encode('utf-8'))
  13. return encrypted_text
  14.  
  15. def des_decrypt(key, encrypted_text):
  16. des = DES.new(key, DES.MODE_ECB)
  17. decrypted_text = des.decrypt(encrypted_text).decode('utf-8').rstrip()
  18. return decrypted_text
  19.  
  20. key = get_random_bytes(8)
  21. plaintext = "DES Algoritum"
  22.  
  23. print("Original Text:", plaintext)
  24. encrypted = des_encrypt(key, plaintext)
  25. print("Encrypted Text:", encrypted)
  26.  
  27. decrypted = des_decrypt(key, encrypted)
  28. print("Original/Decriptiran Text:", decrypted)
  29.  
Success #stdin #stdout 0.02s 11380KB
stdin
Standard input is empty
stdout
('Original Text:', 'DES Algoritum')
('Encrypted Text:', "\xaa8K\xb6]\x08H\xbat@u\xcc'h\xf5\xe6")
('Original/Decriptiran Text:', u'DES Algoritum')