So please print things out in hexadecimals. You can then clearly see that indeed during decrypt that the salt and IV were overlapping. In your code you were also assuming a different length salt than that was given.
from Crypto import Random
from Crypto.Cipher import AES
from base64 import b64encode, b64decode
from os import urandom
import hashlib
import binascii
def key_generation(password, salt, iterations):
assert iterations > 0
print "Salt: " + tohex(salt), '\n', "Password: " + password
key = password + salt #Combines [password] and [salt] to create a [key]
for i in range(iterations): #Hashes the [key]
key = hashlib.sha256(key).digest() #Using Sha256 it hashes the [key] based on amount of [iterations]
return key
def pad(s):
return s + b"\0" * (AES.block_size - len(s) % AES.block_size)
def encryption(message, password, salt, iterations, key_size=256):
print " === ENCRYPTION === "
key = key_generation(password, salt, iterations)
print "Key: " + tohex(key)
message = pad(message)
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CBC, iv)
print "Random IV: " + tohex(iv)
enc = salt + iv + cipher.encrypt(message)
return enc
def decryption(ciphertext, password, iterations):
print " === DECRYPTION === "
salt = ciphertext[0:10]
iv = ciphertext[10:10+AES.block_size]
print "Random IV: " + tohex(iv)
key = key_generation(password, salt, iterations)
print "Key: " + tohex(key)
cipher = AES.new(key, AES.MODE_CBC, iv)
plaintext = cipher.decrypt(ciphertext[10+AES.block_size:])
print "Plaintext: " + plaintext
return plaintext.rstrip(b"\0")
def encrypt_file(file_name, password, salt, iterations):
with open(file_name, 'rb') as fo:
plaintext = fo.read()
print "Text: " + plaintext
enc = encryption(plaintext, password, salt, iterations)
print "Encrypted Contents: " + enc
with open(file_name + ".enc", 'wb') as fo:
fo.write(enc)
def decrypt_file(file_name, password, iterations):
with open(file_name, 'rb') as fo:
ciphertext = fo.read()
print "Cipher'd Text: " + ciphertext
dec = decryption(ciphertext, password, iterations)
with open(file_name + ".dec", 'wb') as fo:
fo.write(dec)
def tohex(data):
return binascii.hexlify(data)
encrypt_file('HelloWorld.txt', 'password', 'randomsalt', 64000)
decrypt_file('HelloWorld.txt.enc', 'password', 64000)