Task1.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Sun Sep 30 17:48: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. #Method to Write To A file
  15. def writeFile(data,fileName):
  16. with open(fileName, 'w+') as f:
  17. f.write(data)
  18. #Method to Write To A file in Bytes
  19. def writeFileInBytes(data,fileName):
  20. with open(fileName, 'wb+') as f:
  21. f.write(data)
  22. #Method to generate 1KB and 1MB Files
  23. def generateFiles():
  24. with open('kbfile.txt', 'w+') as f:
  25. num_chars = 256
  26. f.write("abcd" * num_chars)
  27. #f.write("\n"*1)
  28. with open('mbfile.txt', 'w+') as f:
  29. num_chars = 256 * 1024
  30. f.write("abcd" * num_chars)
  31. f.write("\n"*1)
  32. #Method to read a file
  33. def getData(fileName):
  34. x=""
  35. with open(fileName,'r') as file:
  36. x=file.read()
  37. return x
  38. def getDataInBytes(fileName):
  39. x=""
  40. with open(fileName,'rb') as file:
  41. x=file.read()
  42. return x
  43. def generateKey(size):
  44. start = timer()
  45. key = get_random_bytes(size)
  46. end= timer()
  47. print("Time Taken to generate Key is ",(end-start))
  48. return key
  49. def encryptInCBC(key,data,fileNameToEncrypt):
  50. data=bytes(data, 'utf-8')
  51. cipher = AES.new(key, AES.MODE_CBC)
  52. start = timer()
  53. ct_bytes = cipher.encrypt(pad(data, AES.block_size))
  54. end= timer()
  55. print("Time Taken to Encrypt is "+str(end-start))
  56. print("Time Taken to Encrypt per byte is ",(end-start)/len(data))
  57. iv = b64encode(cipher.iv).decode('utf-8')
  58. ct = b64encode(ct_bytes).decode('utf-8')
  59. writeFileInBytes(bytes(ct,"utf-8"),fileNameToEncrypt)
  60. return iv
  61. def decryptInCBC(key,fileNameToDecrypt,iv,fileNameToSavePT):
  62. iv = b64decode(iv)
  63. ct = b64decode(getDataInBytes(fileNameToDecrypt))
  64. cipher = AES.new(key, AES.MODE_CBC, iv)
  65. start=timer()
  66. pt = unpad(cipher.decrypt(ct), AES.block_size)
  67. end=timer()
  68. print("Time Taken to Decrypt is ",end-start)
  69. print("Time Taken to Decrypt per byte is ",(end-start)/len(pt))
  70. pt=pt.decode("utf-8")
  71. writeFile(pt,fileNameToSavePT)
  72. return pt
  73. generateFiles()
  74. key=generateKey(16)
  75. kbData=getData("kbfile.txt")
  76. mbData=getData("mbfile.txt")
  77. iv=encryptInCBC(key,kbData,"Encrypted_kbfile.txt")
  78. ptKb=decryptInCBC(key,"Encrypted_kbfile.txt",iv,"Decypted_kbfile.txt")
  79. iv=encryptInCBC(key,mbData,"Encrypted_mbfile.txt")
  80. ptMb=decryptInCBC(key,"Encrypted_mbfile.txt",iv,"Decypted_mbfile.txt")
  81. #BASIC CHECK TO SEE IF ENCRYPTION AND DECRYPTION WAS DONE CORRECTLY
  82. if ptKb==kbData and ptMb==mbData:
  83. print("Encryption and decryption done successfully")
  84. else:
  85. print("Error in Encryption and Decryption")