The generation of encryption keys can be guessed if the values are non randomised. In this case we will create a set of random numbers which are determistic and not pseudo-random:
AES Crack |
Code
The following is an outline of the code:
import numpy as np import hashlib import sys import binascii import Padding from Crypto.Cipher import AES from Crypto import Random i=4 msg="elephant poo" def encrypt(word,key, mode): plaintext = Padding.appendPadding(word,blocksize=Padding.AES_blocksize,mode='CMS') encobj = AES.new(key,mode) return(encobj.encrypt(plaintext.encode())) def decrypt(ciphertext,key, mode): encobj = AES.new(key,mode) return(encobj.decrypt(ciphertext)) def isprintable(s, codec='utf8'): try: s.decode(codec) except UnicodeDecodeError: return False else: return True rng = np.random.RandomState(0) val=rng.randn(52) print('Value selected: ',val[i]) key= hashlib.sha256(str(val[i]).encode()).digest() print('Key=',hashlib.sha256(str(val[i]).encode()).hexdigest()) cipher = encrypt(msg,key,AES.MODE_ECB) print(binascii.hexlify(cipher)) for x in val: key= hashlib.sha256(str(x).encode()).digest() decipher = decrypt(cipher,key,AES.MODE_ECB) if isprintable(decipher): print(decipher)