Task3.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Sun Sep 30 18:21:10 2018
  5. @author: varshath
  6. """
  7. import base64
  8. import time
  9. from timeit import default_timer as timer
  10. from Crypto.Cipher import AES
  11. from Crypto.Random import get_random_bytes
  12. from base64 import b64encode,b64decode
  13. from Crypto.Util.Padding import pad,unpad
  14. def writeFile(data,fileName):
  15. with open(fileName, 'w+') as f:
  16. f.write(data)
  17. def writeFileInBytes(data,fileName):
  18. with open(fileName, 'wb+') as f:
  19. f.write(data)
  20. def generateFiles():
  21. with open('kbfile.txt', 'w+') as f:
  22. num_chars = 256
  23. f.write("abcd" * num_chars)
  24. #f.write("\n"*1)
  25. with open('mbfile.txt', 'w+') as f:
  26. num_chars = 256 * 1024
  27. f.write("abcd" * num_chars)
  28. f.write("\n"*1)
  29. def getData(fileName):
  30. x=""
  31. with open(fileName,'r') as file:
  32. x=file.read()
  33. return x
  34. def getDataInBytes(fileName):
  35. x=""
  36. with open(fileName,'rb') as file:
  37. x=file.read()
  38. return x
  39. def generateKey(size):
  40. start = timer()
  41. key = get_random_bytes(size)
  42. end= timer()
  43. print("Time Taken to generate Key is ",(end-start))
  44. return key
  45. def encryptInCTR(key,data,fileNameToEncrypt):
  46. data=bytes(data, 'utf-8')
  47. cipher = AES.new(key, AES.MODE_CTR)
  48. start = timer()
  49. ct_bytes = cipher.encrypt(data)
  50. end= timer()
  51. print("Time Taken to Encrypt is ",(end-start))
  52. print("Time Taken to Decrypt per byte is ",(end-start)/len(data))
  53. nonce = b64encode(cipher.nonce).decode('utf-8')
  54. ct = b64encode(ct_bytes).decode('utf-8')
  55. writeFileInBytes(bytes(ct,"utf-8"),fileNameToEncrypt)
  56. return nonce
  57. def decryptInCTR(key,fileNameToDecrypt,nonce,fileNameToSavePT):
  58. ct = b64decode(getDataInBytes(fileNameToDecrypt))
  59. nonce = b64decode(nonce)
  60. cipher = AES.new(key, AES.MODE_CTR, nonce=nonce)
  61. start=timer()
  62. pt = cipher.decrypt(ct)
  63. end=timer()
  64. print("Time Taken to Decrypt is ",(end-start))
  65. print("Time Taken to Decrypt per byte is ",((end-start)/len(pt)))
  66. pt=pt.decode("utf-8")
  67. writeFile(pt,fileNameToSavePT)
  68. return pt
  69. generateFiles()
  70. key=generateKey(32)
  71. kbData=getData("kbfile.txt")
  72. mbData=getData("mbfile.txt")
  73. nonce=encryptInCTR(key,kbData,"Encrypted_kbfile.txt")
  74. ptKb=decryptInCTR(key,"Encrypted_kbfile.txt",nonce,"Decypted_kbfile.txt")
  75. nonce=encryptInCTR(key,mbData,"Encrypted_mbfile.txt")
  76. ptMb=decryptInCTR(key,"Encrypted_mbfile.txt",nonce,"Decypted_mbfile.txt")
  77. if ptKb==kbData and ptMb==mbData:
  78. print("Encryption and decryption done successfully")
  79. else:
  80. print("Error in Encryption and Decryption")