Created
November 19, 2014 11:18
-
-
Save stevedoyle/702381bc4a324711f472 to your computer and use it in GitHub Desktop.
Demonstrate pseudo-random IV generation for AES-CBC using a counter as IV input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from Crypto.Cipher import AES | |
from Crypto.Cipher import DES3 | |
def add_padding(message): | |
pad_len = 16 - (len(message) % 16) | |
message += (' ' * pad_len) | |
return message | |
# AES test | |
key='\x63\x5b\x76\xd1\x71\x5c\xdf\x2a\x69\x99\xc9\x0f\x3b\x23\x13\x02' | |
pkt_ctr_iv='\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01' | |
dummy_iv='\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' | |
# Using a dummy IV to encrypt a counter IV to create a pseudo random IV from the first block of ciphertext | |
# Allows the use of a simple counter as IV input to CBC mode. | |
obj_enc = AES.new(key, AES.MODE_CBC, dummy_iv) | |
new_iv = obj_enc.encrypt(pkt_ctr_iv) | |
message = "This is a sample message." | |
message = add_padding(message) | |
enc = obj_enc.encrypt(message) | |
obj_dec = AES.new(key, AES.MODE_CBC, new_iv) | |
dec = obj_dec.decrypt(enc) | |
if(dec != message): | |
print("Error decrypting message") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment