fork download
  1. import Crypto.Cipher.AES as AES
  2.  
  3.  
  4. ### Data
  5. cbckey1 = '140b41b22a29beb4061bda66b6747e14'
  6. cbcct1 = '4ca00ff4c898d61e1edbf1800618fb2828a226d160dad07883d04e008a7897ee2e4b7465d5290d0c0e6c6822236e1daafb94ffe0c5da05d9476be028ad7c1d81'
  7.  
  8. cbckey2 = '140b41b22a29beb4061bda66b6747e14'
  9. cbcct2 = '5b68629feb8606f9a6667670b75b38a5b4832d0f26e1ab7da33249de7d4afc48e713ac646ace36e872ad5fb8a512428a6e21364b0c374df45503473c5242a253'
  10.  
  11. ctrkey1 = '36f18357be4dbd77f050515c73fcf9f2'
  12. ctrct1 = '69dda8455c7dd4254bf353b773304eec0ec7702330098ce7f7520d1cbbb20fc388d1b0adb5054dbd7370849dbf0b88d393f252e764f1f5f7ad97ef79d59ce29f5f51eeca32eabedd9afa9329'
  13.  
  14. ctrkey2 = '36f18357be4dbd77f050515c73fcf9f2'
  15. ctrct2 = '770b80259ec33beb2561358a9f2dc617e46218c0a53cbeca695ae45faa8952aa0e311bde9d4e01726d3184c34451'
  16.  
  17. ### Homework:
  18.  
  19. ckey1 = cbckey1.decode('hex')
  20. ct1 = cbcct1.decode('hex')
  21.  
  22. ckey2 = cbckey2.decode('hex')
  23. ct2 = cbcct2.decode('hex')
  24.  
  25. tk1 = ctrkey1.decode('hex')
  26. tct1 = ctrct1.decode('hex')
  27.  
  28. tk2 = ctrkey2.decode('hex')
  29. tct2 = ctrct2.decode('hex')
  30.  
  31. def strxor(s1, s2):
  32. return ''.join([ chr(ord(c1) ^ ord(c2)) for (c1, c2) in zip(s1, s2)])
  33.  
  34.  
  35. class BlockAES:
  36. def __init__(self, key):
  37. self.key = key
  38. self.bs = 16
  39. self.aes = AES.new(key)
  40.  
  41. def split_msg(self, msg):
  42. ms = [ msg[ self.bs*i : self.bs*(i+1) ] for i in range(len(msg)/self.bs + 1) ]
  43. while ms.count(''): ms.remove('')
  44. return ms
  45.  
  46. class CBC(BlockAES):
  47. def decrypt(self, msg):
  48. ms = self.split_msg(msg)
  49. pt = ''
  50. for i in xrange(len(ms) - 1, 0, -1):
  51. pt = strxor(ms[i - 1], self.aes.decrypt(ms[i])) + pt
  52. return pt
  53.  
  54. class CTR(BlockAES):
  55. def decrypt(self, msg):
  56. ms = self.split_msg(msg)
  57. pt = ''
  58. iv = ms[0]
  59. for b in ms[1:]:
  60. pt += strxor(self.aes.encrypt(iv), b)
  61. iv = iv[:-1] + chr(1 + ord(iv[-1]))
  62. return pt
  63.  
  64. if __name__ == '__main__':
  65. print CBC(ckey1).decrypt(ct1)
  66. print CBC(ckey2).decrypt(ct2)
  67. print CTR(tk1).decrypt(tct1)
  68. print CTR(tk2).decrypt(tct2)
Success #stdin #stdout 0.02s 7356KB
stdin
cbckey1 = '140b41b22a29beb4061bda66b6747e14'
cbcct1 = '4ca00ff4c898d61e1edbf1800618fb2828a226d160dad07883d04e008a7897ee2e4b7465d5290d0c0e6c6822236e1daafb94ffe0c5da05d9476be028ad7c1d81'
 
cbckey2 = '140b41b22a29beb4061bda66b6747e14'
cbcct2 = '5b68629feb8606f9a6667670b75b38a5b4832d0f26e1ab7da33249de7d4afc48e713ac646ace36e872ad5fb8a512428a6e21364b0c374df45503473c5242a253'
 
ctrkey1 = '36f18357be4dbd77f050515c73fcf9f2'
ctrct1 = '69dda8455c7dd4254bf353b773304eec0ec7702330098ce7f7520d1cbbb20fc388d1b0adb5054dbd7370849dbf0b88d393f252e764f1f5f7ad97ef79d59ce29f5f51eeca32eabedd9afa9329'
 
ctrkey2 = '36f18357be4dbd77f050515c73fcf9f2'
ctrct2 = '770b80259ec33beb2561358a9f2dc617e46218c0a53cbeca695ae45faa8952aa0e311bde9d4e01726d3184c34451'
stdout
Basic CBC mode encryption needs padding.
Our implementation uses rand. IV
CTR mode lets you build a stream cipher from a block cipher.
Always avoid the two time pad!