title: PyCrypto - How does the Initialization Vector work?
body: <p>I'm trying to understand how PyCrypto works to use in a project but I'm not fully understanding the significance of the Initialization Vector (IV).  I've found that I can use the wrong IV when decoding a string and I still seem to get the message back except for the first 16 bytes (the block size).  Am simply using it wrong or not understanding something?</p>
  
  <p>Here's a sample code to demonstrate:</p>
  
  <pre><code>import Crypto
  import Crypto.Random
  from Crypto.Cipher import AES
  
  def pad_data(data):
      if len(data) % 16 == 0:
          return data
      databytes = bytearray(data)
      padding_required = 15 - (len(databytes) % 16)
      databytes.extend(b'\x80')
      databytes.extend(b'\x00' * padding_required)
      return bytes(databytes)
  
  def unpad_data(data):
      if not data:
          return data
  
      data = data.rstrip(b'\x00')
      if data[-1] == 128: # b'\x80'[0]:
          return data[:-1]
      else:
          return data
  
  
  def generate_aes_key():
      rnd = Crypto.Random.OSRNG.posix.new().read(AES.block_size)
      return rnd
  
  def encrypt(key, iv, data):
      aes = AES.new(key, AES.MODE_CBC, iv)
      data = pad_data(data)
      return aes.encrypt(data)
  
  def decrypt(key, iv, data):
      aes = AES.new(key, AES.MODE_CBC, iv)
      data = aes.decrypt(data)
      return unpad_data(data)
  
  def test_crypto ():
      key = generate_aes_key()
      iv = generate_aes_key() # get some random value for IV
      msg = b"This is some super secret message.  Please don't tell anyone about it or I'll have to shoot you."
      code = encrypt(key, iv, msg)
  
      iv = generate_aes_key() # change the IV to something random
  
      decoded = decrypt(key, iv, code)
  
      print(decoded)
  
  if __name__ == '__main__':
      test_crypto()
  </code></pre>
  
  <p>I'm using Python 3.3.</p>
  
  <p>Output will vary on execution, but I get something like this: <code>b"1^,Kp}Vl\x85\x8426M\xd2b\x1aer secret message.  Please don't tell anyone about it or I'll have to shoot you."</code></p>