コード例 #1
0
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)
コード例 #2
0
def main():
    key = "YELLOW SUBMARINE"
    INPUT = "L77na/nrFsKvynd6HzOoG7GHTLXsTVu9qvY/2syLXzhPweyyMTJULu/6/kXX0KSvoOLSFQ=="
    nonce = 0

    data = base64.b64decode(INPUT)

    print(ctr(data, key, nonce))
コード例 #3
0
def f2(ciphertext):
    global key
    global nonce
    plaintext = ctr(ciphertext,key,nonce)
    if ";admin=true;" in str(plaintext):
        return True
    else:
        return False
コード例 #4
0
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
    ]
コード例 #5
0
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]
コード例 #6
0
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)
コード例 #7
0
ファイル: chal19.py プロジェクト: zimolzak/crypto-challenges
#     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)
コード例 #8
0
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")
コード例 #9
0
ファイル: chal18.py プロジェクト: zimolzak/crypto-challenges
#!/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__ + ")")
コード例 #10
0
def ctr_cheat(ciphertext):
    return cryptopals.ctr(ciphertext, ctr_key, ctr_nonce, "little")
コード例 #11
0
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
コード例 #12
0
def ctr_cheat(ciphertext):
    return cryptopals.ctr(ciphertext, ctr_key, ctr_nonce, "little")
コード例 #13
0
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")
コード例 #14
0

#### 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):
コード例 #15
0
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")
コード例 #16
0
    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):
コード例 #17
0
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")
コード例 #18
0
#!/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__ + ")")
コード例 #19
0
ファイル: chal20.py プロジェクト: zimolzak/crypto-challenges
#     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:
コード例 #20
0
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