from AES_128 import AES_128_CTR, random_data, XOR from base64 import b64decode, b64encode random_key = random_data(16) def single_byte_xor_key(cipher): #returns the key f = [] for i in xrange(0, 256): f.append(0) decrypted = '' for c in cipher: decrypted += chr(i ^ ord(c)) for c in decrypted: if ( (ord(c) >= 65 and ord(c) <= 90) or (ord(c) >= 97 and ord(c) <= 122) or ord(c) == 32 ): #Calculating the no of alphabets in string decrypted with key i f[i] += 1 max_f = 0 key = 0 for i in xrange(0, 256): if (max_f < f[i]): max_f = f[i] key = i return chr(key)
from random import randint from base64 import b64decode,b64encode from AES_128 import AES_128_CBC_decrypt,AES_128_CBC_encrypt,pkcs_7_unpad,XOR,random_data IV = random_data(16) def check_high_ASCII(data): for c in data: if ord(c) > 127 : raise ValueError(data) def encryption_oracle(data): return AES_128_CBC_encrypt(data,IV) def decryption_oracle(data): txt = AES_128_CBC_decrypt(data,IV) check_high_ASCII(txt) return txt def crack(): cipher = encryption_oracle('A'*80) attack = cipher[0:16]+'\x00'*16+cipher[0:16]+cipher[-32:] recovered_pt = '' try: recovered_pt = decryption_oracle(attack) except ValueError as e: recovered_pt = str(e) key = XOR(recovered_pt[0:16],recovered_pt[32:48]) if key == IV : print '[+] Cracked key : %s' % repr(key)
from AES_128 import AES_128_ECB_decrypt,AES_128_CTR,random_data,XOR from base64 import b64decode from random import randint random_key = random_data(16) def edit(ciphertext, offset, newtext): pt = AES_128_CTR(txt, random_key) pt = pt[:offset] + newtext + pt[offset+len(newtext):] return AES_128_CTR(pt, random_key) def crack(cipher, oracle): attack = 'A'*len(cipher) keystream = XOR (attack, oracle(cipher, 0, attack)) pt = XOR(cipher, keystream) return pt if __name__ == '__main__': cipher = ''.join(line.strip() for line in open('25.txt','r')) txt = AES_128_ECB_decrypt(b64decode(cipher), 'YELLOW SUBMARINE', True) cipher = AES_128_CTR(txt, random_key) newtext = random_data(randint(10,50)) offset = randint(0,len(cipher)-1) if AES_128_CTR(edit(cipher, offset, newtext), random_key)[offset:offset+len(newtext)] == newtext : print '[+] Edit Test passed' else : print '[-] Failed'