Advertisement
SooO

Untitled

Nov 8th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1. from Crypto.Cipher import AES
  2. from Crypto.Util.strxor import strxor
  3. from binascii import hexlify
  4. from Crypto.Random import get_random_bytes
  5.  
  6. # Random generation
  7. K = get_random_bytes(AES.block_size)
  8. cipher = AES.new(K, AES.MODE_CBC)
  9.  
  10. # Original Message
  11. M1 = K #bytes(K,encoding="utf-8")
  12. M2 = K #bytes(K,encoding="utf-8")
  13.  
  14. Cm0 = cipher.encrypt(('\0' * AES.block_size).encode("utf-8"))
  15. Cm1 = cipher.encrypt(strxor(Cm0, M1))
  16.  
  17. Tm = Cm2 = cipher.encrypt(strxor(Cm1, M2))
  18. N1 = get_random_bytes(AES.block_size)#'qwertyuiop'.encode("utf-8")
  19.  
  20. # Inject second message after the first message
  21. Cx0 = cipher.encrypt(('\0' * AES.block_size).encode("utf-8"))
  22. Cx1 = cipher.encrypt(strxor(Cx0, M1))
  23. Cx2 = cipher.encrypt(strxor(Cx1, N1))
  24.  
  25. # X needs to *encrypt* to the same value as Cm1
  26. #X = strxor(cipher.decrypt(Cx1), Cx2)
  27. X = strxor(strxor(Cx0, M1), Cx2)
  28. Cx3 = cipher.encrypt(strxor(Cx2, X))
  29. Tx = Cx4 = cipher.encrypt(strxor(Cx3, M2))
  30.  
  31. # MAC should be same here
  32. print("Tm = '%s'" % hexlify(Tm))
  33. print("Tx = '%s'" % hexlify(Tx))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement