예제 #1
0
파일: p26.py 프로젝트: Saad2012/cryptopals
def p26():
    user_data = 'A' * 16
    comment1 = 'comment1=cooking%20MCs;userdata='
    comment2 = ';comment2=%20like%20a%20pound%20of%20bacon'
    ptxt = comment1 + user_data.replace(';', '').replace('=', '') + comment2
    key = urandom(16)

    ctxt = aes_ctr(ptxt, key)
    ctxtmod = _bitflip(ctxt, user_data)
    ptxtmod = aes_ctr(ctxtmod, key)
    return _parse(ptxtmod)
예제 #2
0
def p25() -> bytes:
    with open('Data/25.txt', 'rb') as f:
        data = b64decode(f.read().replace(b'\n', b''))

    key = b'YELLOW SUBMARINE'
    ptxt = aes_ecb_decrypt(data, key)

    key = urandom(16)
    ctxt = aes_ctr(ptxt, key)

    newtext = b'A' * len(ctxt)
    edited = _edit_ctxt(ctxt, key, 0, newtext)
    return xor(xor(edited, ctxt), newtext)
예제 #3
0
def p20() -> bytes:
    key = urandom(16)
    ctxts = []

    with open('Data/20.txt', 'rb') as f:
        for line in f.readlines():
            ptxt = b64decode(line)
            ctxts.append(aes_ctr(ptxt, key, 0))

    keystream = b''
    for i in range(min(map(len, ctxts))):
        keystream += get_key([c[i] for c in ctxts])

    ptxt = []
    for ctxt in ctxts:
        raw = [c ^ k for (c, k) in zip(ctxt, keystream)]
        ptxt.append(bytes(raw))

    return b'\n'.join(ptxt)
예제 #4
0
파일: p20.py 프로젝트: Saad2012/cryptopals
def p20():
    key = urandom(16)
    ctxts = []

    with open('Data/20.txt') as f:
        for line in f.readlines():
            ptxt = b64decode(line)
            ctxts.append(aes_ctr(ptxt, key, 0))

    keystream = ''
    for i in range(max(map(len, ctxts))):
        keystream += get_key([(c[i] if i < len(c) else '') for c in ctxts])

    ptxt = ''
    for ctxt in ctxts:
        raw = [chr(ord(c) ^ ord(k)) for (c, k) in zip(ctxt, keystream)]
        ptxt += ''.join(raw) + '\n'

    return ptxt[:-1]
예제 #5
0
def p19() -> bytes:
    key = urandom(16)
    ctxts = []

    with open('Data/19.txt', 'rb') as f:
        for line in f.readlines():
            ptxt = b64decode(line)
            ctxts.append(aes_ctr(ptxt, key, 0))

    keystream = b''
    for i in range(max(map(len, ctxts))):
        index_bytes = [(c[i] if i < len(c) else None) for c in ctxts]
        keystream += get_key(index_bytes)

    ptxt = []
    for ctxt in ctxts:
        raw = [c ^ k for (c, k) in zip(ctxt, keystream)]
        ptxt.append(bytes(raw))

    return b'\n'.join(ptxt)
예제 #6
0
def _edit_ctxt(ctxt: bytes, key: bytes, offset: int, newtext: bytes) -> bytes:
    new_ctxt = aes_ctr(newtext, key)
    return ctxt[:offset] + new_ctxt + ctxt[offset + len(new_ctxt):]