Task5_3.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. def rsa(message,size):
  2. start = timer()
  3. key = RSA.generate(size)
  4. key_gen_time= timer() - start
  5. cipher = PKCS1_OAEP.new(key.publickey())
  6. buffer=""
  7. chunks=[]
  8. size=214
  9. parts=int(len(message)/size)
  10. x=0
  11. for i in range(parts):
  12. chunks.append(message[size*i:size*(i+1)])
  13. x=i
  14. chunks.append(message[(x+1)*size:])
  15. ciphers=[]
  16. start = timer()
  17. for chunk in chunks:
  18. chunk_bytes=chunk.encode("utf-8")
  19. ciphertext = cipher.encrypt(chunk_bytes)
  20. ciphers.append(ciphertext)
  21. encrypt_time=timer() - start
  22. #decryption
  23. cipher = PKCS1_OAEP.new(key)
  24. start = timer()
  25. for ciphertext in ciphers:
  26. pt = cipher.decrypt(ciphertext)
  27. buffer+=pt.decode("utf-8")
  28. decrypt_time = timer() - start
  29. if buffer==message:
  30. print("successfull encryption and decryption")
  31. else:
  32. print("Incorrect encryption/decryption")
  33. print("time taken for key gen ",(key_gen_time))
  34. print("time taken to encrypt ",(encrypt_time))
  35. print("Time Taken to Encrypt per byte is ",((encrypt_time)/len(message)))
  36. print("time taken to Decrypt ",(decrypt_time))
  37. print("Time Taken to Decrypt per byte is ",((decrypt_time)/len(message)))
  38. generateFiles()
  39. kbData=getData("kbfile.txt")
  40. mbData=getData("mbfile.txt")
  41. rsa(kbData,2048)
  42. rsa(mbData,2048)