def isValid(CipherText, Key, IV): cipher = AES.AESCipher(Key, AES.MODE_CBC, IV) plainText = cipher.decrypt(CipherText) if unPad(plainText, 16) != "ERROR": return True return False
iv_A = os.urandom(16) iv_B = os.urandom(16) msg_A = "Hello World" cipher = AES.AESCipher(key_A, AES.MODE_CBC, iv_A) send_A = cipher.encrypt("".join(pad(msg_A,16))) + iv_A print "A -> B" print "Send AES-CBC(SHA1(s)[0:16],iv=random(16),msg)" received_B = send_A iv_A_B = received_B[len(received_B) - 16:] encrypted_A_B = received_B[:len(received_B) - 16] cipher = AES.AESCipher(key_B, AES.MODE_CBC, iv_A_B) msg_B = unPad(cipher.decrypt(encrypted_A_B),16) cipher = AES.AESCipher(key_B, AES.MODE_CBC, iv_B) send_B = cipher.encrypt("".join(pad(msg_B,16))) + iv_B print "B -> A" print "Send AES-CBC(SHA1(s)[0:16],iv=random(16),A's msg)" received_A = send_B iv_B_A = received_A[len(received_A) -16:] encrypted_B_A = received_A[:len(received_A)-16] cipher = AES.AESCipher(key_A, AES.MODE_CBC, iv_B_A) msg = unPad(cipher.decrypt(encrypted_B_A),16) print msg == msg_A == msg_B