def main(): ptexts = loader("20.txt", "base64") ctexts = [aes_ctr(ptext, KEY, NONCE) for ptext in ptexts] keystream = guess_keystream(ctexts) print("Cracked plaintexts:") print() correct, total = 0, 0 for ptext, ctext in zip(ptexts, ctexts): guess = repeating_key_xor(ctext, keystream) print(guess.decode(errors="replace")) nchars = len(ptext) correct += sum(guess[i] == ptext[i] for i in range(nchars)) total += nchars # With more input, accuracy is more consistent at ~98%. # Unfortunately, the first keystream byte is always wrong D: accuracy = correct / total print() print(f"Accuracy: {correct}/{total} ({accuracy*100:.2f}%)")
def main(): ptext, high_score = b"", 0 for ctext in loader("4.txt", "hexstring"): candidate = single_byte_xor(ctext) score = englishness(candidate) if score > high_score: ptext, high_score = candidate, score print(ptext.decode())
def main(): key = b"YELLOW SUBMARINE" ctext = loader("10.txt", "base64", split=False) ptext = aes_cbc_decrypt(ctext, key) print("Decrypted a ciphertext AES-CBC.") print("Used a null IV and the following key:", key) print() print(ptext.decode()) print()
def main(): # NOTE BEFORE STARTING: Do not be fooled! Signatures are created using # private keys, notwithstanding the (perhaps intentionally misleading?) # wording in this and the previous challenge that might imply otherwise. lines = loader("44.txt", lambda l: l.rstrip("\n").split(": ")) msgs = [] for i in range(0, len(lines), 4): block = lines[i:i + 4] # After the rstrip() and split() above, a block looks like: # # [["msg", "Listen for me, you better listen for me now. "], # ["s", "1267396447369736888040262262183731677867615804316"], # ["r", "1105520928110492191417703162650245113664610474875"], # ["m", "a4db3de27e2db3e5ef085ced2bced91b82e0df19"]] # # We have a message, the components of a DSA signature, and the SHA1 # hash of the message. Everything here's a `str` at the moment, so # we'll transform the values. # # There's an error in "m" for one of the blocks in the challenge data, # but we can just calculate the hash ourselves. block[0][1] = block[0][1].encode() block[1][1] = int(block[1][1]) block[2][1] = int(block[2][1]) block[3][1] = from_bytes(SHA1(block[0][1]).digest()) msgs.append({data[0]: data[1] for data in block}) print(f"Loaded {len(msgs)} DSA-signed messages.") print("Recovering private key from the first repeated nonce we detect.") for msg1, msg2 in combinations(msgs, 2): if msg1["r"] != msg2["r"]: continue m1, m2 = msg1["m"], msg2["m"] s1, s2 = msg1["s"], msg2["s"] k = (((m1 - m2) % Q) * invmod(s1 - s2, Q)) % Q privkey = recover_dsa_privkey(k, msg1["r"], s1, m1) digest = SHA1(to_hexstring(to_bytes(privkey))).hexdigest() assert digest == "ca8f6f7c66fa362d40760d135b763eb8527d3d52" print() print("Recovered key:", privkey) break else: print("Failed to recover key!")
def main(): ctext = loader("6.txt", "base64", split=False) ptext, key, high_score = b"", b"", 0 for size in likely_key_sizes(ctext): blocks = [ctext[i:i + size] for i in range(0, len(ctext), size)] transposed = zip_longest(*blocks, fillvalue=0) likely_key = b"".join( single_byte_xor(tblock, key=True) for tblock in transposed) candidate = repeating_key_xor(ctext, likely_key) score = englishness(candidate) if score > high_score: ptext, key, high_score = candidate, likely_key, score print(f"Key: '{key.decode()}'") print() print(ptext.decode())
def main(): ecb_key, key = b"YELLOW SUBMARINE", make_aes_key() ptext = aes_ecb_decrypt(loader("25.txt", "base64", split=False), ecb_key) ctext = aes_ctr(ptext, key) print(f"Cracking ciphertext using {CPUS} cores, please wait.") # This signal handler business allows Ctrl-C to work more gracefully # with multiprocessing. (Tested on Linux only.) orig_handler = signal.signal(signal.SIGINT, signal.SIG_IGN) with Pool(CPUS) as pool: signal.signal(signal.SIGINT, orig_handler) mp_data = ((ctext, key, offset) for offset in range(len(ctext))) try: recovered_ptext = bytes(pool.imap(mp_find_ptext_byte, mp_data)) except KeyboardInterrupt: raise else: print() print(recovered_ptext.decode()) finally: print()
def main(): cdata = loader("8.txt", "base64") aes_ecb_lines = [ n for n, ctext in enumerate(cdata, 1) if is_aes_ecb(ctext) ] print("The following lines were encrypted with AES-ECB:", aes_ecb_lines)
import tensorflow as tf from keras.backend.tensorflow_backend import set_session config = tf.ConfigProto() config.gpu_options.allow_growth = True # dynamically grow the memory used on the GPU config.log_device_placement = True # to log device placement (on which device the operation ran) # (nothing gets printed in Jupyter, only if you run it standalone) sess = tf.Session(config=config) set_session(sess) # set this TensorFlow session as the default session for Keras experiment = 'experiment-3' model=create_model() model.load_weights("./GaitRecognizer/gait_recognizer_model_weight.h5") input_image, source_image =loader('./input/'+experiment) create_gif('./input/'+experiment+'/source-silh','./input/'+experiment+'/gif','source.gif') input_image=input_image.astype('float32') source_image=source_image.astype('float32') input_image/=255 source_image/=255 model_input_layer = model.layers[0].input model_output_layer = model.layers[-1].output max_change_above = input_image + 0.05 max_change_below = input_image - 0.05 origin=np.argmax(model.predict(input_image)) gait_to_fake = np.argmax(model.predict(source_image))
def main(): key = b"YELLOW SUBMARINE" ctext = loader("7.txt", "base64", split=False) ptext = aes_ecb_decrypt(ctext, key) print(ptext.decode())