def f1(plaintext): global key global nonce sanitised = plaintext.replace(";","';'").replace("=","'='") data = "comment1=cooking%20MCs;userdata="+sanitised+";comment2=%20like%20a%20pound%20of%20bacon" return ctr(bytearray(data,"ascii"),key,nonce)
def main(): key = "YELLOW SUBMARINE" INPUT = "L77na/nrFsKvynd6HzOoG7GHTLXsTVu9qvY/2syLXzhPweyyMTJULu/6/kXX0KSvoOLSFQ==" nonce = 0 data = base64.b64decode(INPUT) print(ctr(data, key, nonce))
def f2(ciphertext): global key global nonce plaintext = ctr(ciphertext,key,nonce) if ";admin=true;" in str(plaintext): return True else: return False
def site_profile_token(input_str): good_nonce = "" for i in range(8): good_nonce += chr(random.randint(0, 255)) input_str = input_str.replace(';', '.') input_str = input_str.replace('=', '.') plaintext = ('comment1=cooking%20MCs;userdata=' + input_str + ';comment2=%20like%20a%20pound%20of%20bacon;') return [ cryptopals.ctr(plaintext, ctr_key, good_nonce, "little"), good_nonce ]
def site_profile_token(input_str): good_nonce = "" for i in range(8): good_nonce += chr(random.randint(0,255)) input_str = input_str.replace(';', '.') input_str = input_str.replace('=', '.') plaintext = ('comment1=cooking%20MCs;userdata=' + input_str + ';comment2=%20like%20a%20pound%20of%20bacon;') return [cryptopals.ctr(plaintext, ctr_key, good_nonce, "little") , good_nonce]
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)
# chal19.py - Fixed-nonce CTR via substitutions # # Copyright (C) 2015 Andrew J. Zimolzak <*****@*****.**>, # and licensed under GNU GPL version 3. Full notice is found in # the file 'LICENSE' in the same directory as this file. import base64 from cryptopals import warn, ctr, xor_str key = open('unknown_key.txt', 'r').read().splitlines()[0] nonce = "\x00\x00\x00\x00\x00\x00\x00\x00" ciphertexts = [] for b64 in open('19.txt', 'r').read().splitlines(): ciphertexts = ciphertexts + [ ctr(base64.b64decode(b64), key, nonce, "little") ] guesses = [''] * 256 for i in range(len(ciphertexts)): # This loop is for manual use only. Serves no purpose in the final # decryption step. Manually adjust the [i][15] number, and examine # the printout for strings that look like slices of English text # (mainly lowercase and spaces, few punctuation marks, no ASCII > # 127). First char of each string is the J'th byte in the # keystream. for c in range(256): if i == 0: guesses[c] = guesses[c] + chr(c)
def edit(ciphertext, key, nonce, offset, newtext): plaintext = cryptopals.ctr(ciphertext, key, nonce, "little") nchars = len(newtext) plaintext = plaintext[0:offset] + newtext + plaintext[offset+nchars:] return cryptopals.ctr(plaintext, key, nonce, "little")
#!/usr/bin/env python # chal18.py - Implement CTR # # Copyright (C) 2015 Andrew J. Zimolzak <*****@*****.**>, # and licensed under GNU GPL version 3. Full notice is found in # the file 'LICENSE' in the same directory as this file. import base64 from cryptopals import warn, ctr ciphertext = base64.b64decode( "L77na/nrFsKvynd6HzOoG7GHTLXsTVu9qvY/2syLXzhPweyyMTJULu/6/kXX0KSvoOLSFQ==") key = "YELLOW SUBMARINE" nonce = "\x00\x00\x00\x00\x00\x00\x00\x00" # 8 byte nonce plaintext = ctr(ciphertext, key, nonce, "little") print plaintext #### tests #### assert plaintext[-5:] == "baby " warn("Passed assertions (" + __file__ + ")")
def ctr_cheat(ciphertext): return cryptopals.ctr(ciphertext, ctr_key, ctr_nonce, "little")
def profile_is_admin(cipher_nonce_list): token = cipher_nonce_list[0] nonce = cipher_nonce_list[1] plaintext = cryptopals.ctr(token, ctr_key, nonce, "little") return ';admin=true;' in plaintext
def profile_token_cheat(cipher_nonce_list): ciphertext = cipher_nonce_list[0] nonce = cipher_nonce_list[1] return cryptopals.ctr(ciphertext, ctr_key, nonce, "little")
#### CTR ecb_encrypted = base64.b64decode(''.join( open('25.txt', 'r').read().splitlines())) plain = AES.new("YELLOW SUBMARINE", AES.MODE_ECB).decrypt(ecb_encrypted) ctr_key = open('unknown_key.txt', 'r').read().splitlines()[0] ctr_nonce = "" for i in range(8): # I believe this sets it upon import of this file, not for each # encrypt/decrypt. Not the most secure, but in any case I don't # use a nonce attack to "cheat" at CTR challenges. ctr_nonce += chr(random.randint(0, 255)) ctr_ciphertext = cryptopals.ctr(plain, ctr_key, ctr_nonce, "little") def edit(ciphertext, key, nonce, offset, newtext): plaintext = cryptopals.ctr(ciphertext, key, nonce, "little") nchars = len(newtext) plaintext = plaintext[0:offset] + newtext + plaintext[offset + nchars:] return cryptopals.ctr(plaintext, key, nonce, "little") def edit_public(ciphertext, offset, newtext): # closure return edit(ciphertext, ctr_key, ctr_nonce, offset, newtext) def ctr_cheat(ciphertext):
def edit(ciphertext, key, nonce, offset, newtext): plaintext = cryptopals.ctr(ciphertext, key, nonce, "little") nchars = len(newtext) plaintext = plaintext[0:offset] + newtext + plaintext[offset + nchars:] return cryptopals.ctr(plaintext, key, nonce, "little")
return cipher.encrypt(message) #### CTR ecb_encrypted = base64.b64decode(''.join(open('25.txt', 'r'). read().splitlines())) plain = AES.new("YELLOW SUBMARINE", AES.MODE_ECB).decrypt(ecb_encrypted) ctr_key = open('unknown_key.txt', 'r').read().splitlines()[0] ctr_nonce = "" for i in range(8): # I believe this sets it upon import of this file, not for each # encrypt/decrypt. Not the most secure, but in any case I don't # use a nonce attack to "cheat" at CTR challenges. ctr_nonce += chr(random.randint(0,255)) ctr_ciphertext = cryptopals.ctr(plain, ctr_key, ctr_nonce, "little") def edit(ciphertext, key, nonce, offset, newtext): plaintext = cryptopals.ctr(ciphertext, key, nonce, "little") nchars = len(newtext) plaintext = plaintext[0:offset] + newtext + plaintext[offset+nchars:] return cryptopals.ctr(plaintext, key, nonce, "little") def edit_public(ciphertext, offset, newtext): # closure return edit(ciphertext, ctr_key, ctr_nonce, offset, newtext) def ctr_cheat(ciphertext): return cryptopals.ctr(ciphertext, ctr_key, ctr_nonce, "little") def profile_token_cheat(cipher_nonce_list):
# chal20.py - Fixed-nonce CTR via statistics # # Copyright (C) 2015 Andrew J. Zimolzak <*****@*****.**>, # and licensed under GNU GPL version 3. Full notice is found in # the file 'LICENSE' in the same directory as this file. import base64 from cryptopals import warn, ctr, xor_str, xor_uneq from rkxor import break_cipher_given_keysize, xor_char_str key = open('unknown_key.txt', 'r').read().splitlines()[0] nonce = "\x00\x00\x00\x00\x00\x00\x00\x00" ciphertexts = [] for b64 in open('20.txt', 'r').read().splitlines(): ciphertexts = ciphertexts + [ctr(base64.b64decode(b64), key, nonce, "little")] #### Decrypt the hard way (statistics) #### maximum = 0 for i in range(len(ciphertexts)): if len(ciphertexts[i]) > maximum: maximum = len(ciphertexts[i]) keystream = "" for k in range(maximum): concat = "" for c in ciphertexts: try: concat += c[k] except IndexError: