Task2_8.py 959 B

123456789101112131415161718192021222324252627282930
  1. def decryptInCTR(key,fileNameToDecrypt,nonce,fileNameToSavePT):
  2. ct = b64decode(getDataInBytes(fileNameToDecrypt))
  3. nonce = b64decode(nonce)
  4. cipher = AES.new(key, AES.MODE_CTR, nonce=nonce)
  5. start=timer()
  6. pt = cipher.decrypt(ct)
  7. end=timer()
  8. print("Time Taken to Decrypt is ",(end-start))
  9. print("Time Taken to Decrypt per byte is ",((end-start)/len(pt)))
  10. pt=pt.decode("utf-8")
  11. writeFile(pt,fileNameToSavePT)
  12. return pt
  13. generateFiles()
  14. key=generateKey(16)
  15. kbData=getData("kbfile.txt")
  16. mbData=getData("mbfile.txt")
  17. nonce=encryptInCTR(key,kbData,"Encrypted_kbfile.txt")
  18. ptKb=decryptInCTR(key,"Encrypted_kbfile.txt",nonce,"Decypted_kbfile.txt")
  19. nonce=encryptInCTR(key,mbData,"Encrypted_mbfile.txt")
  20. ptMb=decryptInCTR(key,"Encrypted_mbfile.txt",nonce,"Decypted_mbfile.txt")
  21. if ptKb==kbData and ptMb==mbData:
  22. print("Encryption and decryption done successfully")
  23. else:
  24. print("Error in Encryption and Decryption")