def main(): global key #put it here for the edit_api() function global nonce #put it here for the edit_api() function INPUT = recover_input() key = random_aes_key() nonce = 0 ciphertext = ctr(INPUT, key, nonce) #print ctr(ciphertext,key,nonce)[:255] junk = bytearray("A" * len(ciphertext), "ascii") mask = edit_api(ciphertext, 0, junk) keystream = xor(junk, mask) plaintext = xor(ciphertext, keystream) print(plaintext)
def main(): blocksize = 16 global key global IV #key = random_aes_key(blocksize) key = "YELLOW SUBMARINE" IV = bytearray(key, "ascii") INPUT = bytearray("A" * 32, "ascii") ciphertext = f1(INPUT) temp = blockfy(ciphertext) x = temp[0] x.extend(bytearray(16)) x.extend(temp[0]) #x = temp[0] + "\x00"*16 + temp[0] r = f2(x) if r: error = r else: print("Bad luck!") exit() p = blockfy(error) k = xor(p[0], p[2]) print(k)
def cbc_decrypt(key, cipher, iv): block_size = 16 aes = AES.new(key, AES.MODE_ECB) cipher_text = [] data = iv cipher = pkcs7(cipher, block_size) for block in grouper(cipher, block_size): plain = aes.decrypt(bytes(block)) cipher_text.append(xor(plain, data)) data = bytes(block) return b''.join(cipher_text)
def cbc_encrypt(key, plain, iv): block_size = 16 aes = AES.new(key, AES.MODE_ECB) cipher_text = [] data = iv plain = pkcs7(plain, block_size) for block in grouper(plain, block_size): data = xor(data, bytes(block)) data = aes.encrypt(data) cipher_text.append(data) return b''.join(cipher_text)
def recover_key(known,ciphertext): fake = bytearray(len(ciphertext)-len(known)) fake.extend(known) temp = xor(fake,ciphertext) x = [temp[i:i+4] for i in range(0,len(temp),4)] a = struct.Struct('<I').unpack(x[-2])[0] #getting the penultimate block for K in range(0x10000): seed_mt(K) for i in range(0,len(ciphertext)-4,4): w = extract_number() if w == a: return K
def hamming_distance(str1, str2): temp = cryptopals.xor(str1, str2) return sum(bitstring.Bits(temp))
#!/usr/bin/python3.5 from base64 import b64encode, b64decode from binascii import hexlify,unhexlify from string import ascii_letters import cryptopals import os string1="""Burning 'em, if you ain't quick and nimble I go crazy when I hear a cymbal""" key='ICE' xor1='0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f' def genKeyString(key,length): keyString='' for i in range(length): keyString+=key[i%len(key)] return keyString keyString=genKeyString(key,len(string1.encode())) result=cryptopals.xor(string1.encode(),keyString.encode()).encode() assert(hexlify(result)==xor1.encode())
#!/usr/bin/python3.5 from base64 import b64encode, b64decode from binascii import hexlify, unhexlify from string import ascii_letters import cryptopals import os string1 = """Burning 'em, if you ain't quick and nimble I go crazy when I hear a cymbal""" key = 'ICE' xor1 = '0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f' def genKeyString(key, length): keyString = '' for i in range(length): keyString += key[i % len(key)] return keyString keyString = genKeyString(key, len(string1.encode())) result = cryptopals.xor(string1.encode(), keyString.encode()).encode() assert (hexlify(result) == xor1.encode())
def genBlock(string, keysize): blocks = [] for i in range(int(len(string) / keySize[0])): blocks.append(string[keySize[0] * i:keySize[0] * (i + 1)]) return blocks try: with open('6.txt', 'rb') as file: string = b'' for line in file: string += line[:-1] string = b64decode(string) keySizeList = genKeySizeList(2, 40) for keySize in keySizeList: bestKey = [] key = '' allBlocks = genBlock(string, keySize) for i in range(0, keySize[0]): bytesString = genByteBlock(allBlocks, i) result = cryptopals.bruteForce(hexlify(bytesString)) bestKey = cryptopals.findBestKey(result) key += bestKey[0][0] keyString = cryptopals.genKeyString(key, len(string)) print('la clé est =>{}'.format(key)) print(cryptopals.xor(string, keyString.encode())) except OSError as error: raise error
def testCase(self): a = "1c0111001f010100061a024b53535009181c" b = "686974207468652062756c6c277320657965" expected = bytes.fromhex("746865206b696420646f6e277420706c6179") actual = cryptopals.xor(bytes.fromhex(a), bytes.fromhex(b)) self.assertEqual(expected, actual)
for block in blocks: byteString+=block[value:value+1] return byteString def genBlock(string,keysize): blocks=[] for i in range(int(len(string)/keySize[0])): blocks.append(string[keySize[0]*i:keySize[0]*(i+1)]) return blocks try: with open('6.txt','rb') as file: string=b'' for line in file: string+=line[:-1] string=b64decode(string) keySizeList=genKeySizeList(2,40) for keySize in keySizeList: bestKey=[] key='' allBlocks=genBlock(string,keySize) for i in range(0,keySize[0]): bytesString=genByteBlock(allBlocks,i) result=cryptopals.bruteForce(hexlify(bytesString)) bestKey=cryptopals.findBestKey(result) key+=bestKey[0][0] keyString=cryptopals.genKeyString(key,len(string)) print('la clé est =>{}'.format(key)) print(cryptopals.xor(string,keyString.encode())) except OSError as error: raise error