Exemplo n.º 1
0
def encrypt(plaintext: str) -> str:
    plaintext = plaintext.replace(b';', b'%3B')
    plaintext = plaintext.replace(b'=', b'%3D')
    plaintext = b'comment1=cooking%20MCs;userdata=' + plaintext +\
     b';comment2=%20like%20a%20pound%20of%20bacon'

    cipher = aesCTR(KEY)
    return cipher.encrypt(plaintext)
Exemplo n.º 2
0
def decrypt(ciphertext: str) -> str:
    cipher = aesCTR(KEY)
    print(plaintext)

    if b';admin=true;' in plaintext:
        print('You did it!')
    else:
        print('Nope. Try again!')
def edit(ciphertext: str, offset: int, newtext: str) -> str:
    cipher = aesCTR(KEY)

    ciphertext = ciphertext[:offset] + newtext + ciphertext[offset + 1:]
    return cipher.encrypt(ciphertext)
    return output


KEY = keygen()


def edit(ciphertext: str, offset: int, newtext: str) -> str:
    cipher = aesCTR(KEY)

    ciphertext = ciphertext[:offset] + newtext + ciphertext[offset + 1:]
    return cipher.encrypt(ciphertext)


if __name__ == '__main__':
    plaintext = b''
    with open('25.txt', 'r') as file:
        plaintext = b64decode(file.read())

    print(plaintext)
    cipher = aesCTR(KEY)
    ciphertext = cipher.encrypt(plaintext)

    key = b''
    for i in range(len(ciphertext)):
        modified_ciphertext = edit(ciphertext, i, b'\x00')
        key += bytes([modified_ciphertext[i]])
    decrypted_plaintext = xor(ciphertext, key)
    print(decrypted_plaintext)
    print('Are they same -', plaintext == decrypted_plaintext)
        if error_seen <= min_error:
            outputs = [output]
            keys = [key * len(input)]
            min_error = error_seen

    return outputs[0]


if __name__ == '__main__':
    plaintexts = []
    with open('20.txt', 'r') as file:
        plaintexts = [b64decode(x) for x in file.read().splitlines()]
    print(*plaintexts, sep='\n')

    cipher = aesCTR('YELLOW SUBMARINE')
    ciphertexts = [cipher.encrypt(plaintext) for plaintext in plaintexts]
    #print(ciphertexts)

    min_cipher_text_length = min(
        [len(ciphertext) for ciphertext in ciphertexts])

    ciphertexts = [ciphertext[:min_cipher_text_length] for ciphertext in\
     ciphertexts]
    #print(ciphertexts)

    blocks = [b'' for _ in range(min_cipher_text_length)]
    for i in range(min_cipher_text_length):
        for ciphertext in ciphertexts:
            blocks[i] += bytes([ciphertext[i]])
    #print(blocks)