Ejemplo n.º 1
0
def encrypt(binary_plaintext, modulus, public_key):
    """Generate binary ciphertext from binary plaintext with RSA."""
    padded_plaintext = pad_plaintext(binary_plaintext, plaintext_block_size())
    return ''.join(left_pad(rsa_exponentiation(block, modulus, public_key),
                            MODULUS_BITS)
                   for block in block_split(padded_plaintext,
                                            plaintext_block_size()))
Ejemplo n.º 2
0
def sha_1(binary_message):
    """SHA-1 cryptographic hash function. Take a binary string of any
    length and output an obfuscated 160-bit binary hash."""
    padded_message = pad_plaintext(binary_message, BLOCKSIZE)
    sub_registers = [hex_to_binary(initial_register)
                     for initial_register
                     in ['67452301', 'EFCDAB89', '98BADCFE',
                         '10325476', 'C3D2E1F0']]
    for block in block_split(padded_message, BLOCKSIZE):
        sub_blocks = sha_1_expansion(block)
        sub_registers = sha_1_compression(sub_registers, sub_blocks)
    return ''.join(sub_registers)
Ejemplo n.º 3
0
def sha_1_expansion(block):
    """Take a 512 bit binary message and convert it into a series of
    32 bit blocks.
    """
    sub_blocks = block_split(block, SUB_BLOCKSIZE)
    for interval in xrange(len(sub_blocks), SHA_1_INTERVALS):
        new_sub_block = bitwise_xor(sub_blocks[interval - 3],
                                    sub_blocks[interval - 8],
                                    sub_blocks[interval - 14],
                                    sub_blocks[interval - 16])
        sub_blocks.append(wrap_bits_left(new_sub_block, 1))
    return sub_blocks
Ejemplo n.º 4
0
def sha_1(binary_message):
    """SHA-1 cryptographic hash function. Take a binary string of any
    length and output an obfuscated 160-bit binary hash."""
    padded_message = pad_plaintext(binary_message, BLOCKSIZE)
    sub_registers = [
        hex_to_binary(initial_register) for initial_register in
        ['67452301', 'EFCDAB89', '98BADCFE', '10325476', 'C3D2E1F0']
    ]
    for block in block_split(padded_message, BLOCKSIZE):
        sub_blocks = sha_1_expansion(block)
        sub_registers = sha_1_compression(sub_registers, sub_blocks)
    return ''.join(sub_registers)
Ejemplo n.º 5
0
def sha_1_expansion(block):
    """Take a 512 bit binary message and convert it into a series of
    32 bit blocks.
    """
    sub_blocks = block_split(block, SUB_BLOCKSIZE)
    for interval in xrange(len(sub_blocks), SHA_1_INTERVALS):
        new_sub_block = bitwise_xor(sub_blocks[interval - 3],
                                    sub_blocks[interval - 8],
                                    sub_blocks[interval - 14],
                                    sub_blocks[interval - 16])
        sub_blocks.append(wrap_bits_left(new_sub_block, 1))
    return sub_blocks
Ejemplo n.º 6
0
def feistel_scheme(binary_text, subkeys):
    """Run the DES algorithm on a each block of the string input
    text. The subkeys parameter should be a list of sixteen 48-bit
    binary strings.
    """
    final_blocks = []
    for block in block_split(binary_text):
        block = permute(block, INITIAL_PERMUTATION)
        left, right = half_string(block)
        for key in subkeys:
            right, left = bitwise_xor(left, feistel_function(right, key)), right
        block = permute(right + left, FINAL_PERMUTATION)
        final_blocks.append(block)
    return ''.join(final_blocks)
Ejemplo n.º 7
0
def feistel_scheme(binary_text, subkeys):
    """Run the DES algorithm on a each block of the string input
    text. The subkeys parameter should be a list of sixteen 48-bit
    binary strings.
    """
    final_blocks = []
    for block in block_split(binary_text):
        block = permute(block, INITIAL_PERMUTATION)
        left, right = half_string(block)
        for key in subkeys:
            right, left = bitwise_xor(left, feistel_function(right,
                                                             key)), right
        block = permute(right + left, FINAL_PERMUTATION)
        final_blocks.append(block)
    return ''.join(final_blocks)
Ejemplo n.º 8
0
def decrypt(binary_ciphertext, binary_key):
    """Reveal binary plaintext from binary ciphertext with AES."""
    subkeys = list(reversed(key_schedule(binary_key)))
    final_blocks = []
    for block in block_split(binary_ciphertext, 128):
        block_matrix = binary_to_matrix(block)
        block_matrix = add_round_key(block_matrix, subkeys[0])
        block_matrix = inverse_shift_rows(block_matrix)
        block_matrix = byte_sub(block_matrix, INVERSE_S_BOX)
        for round in xrange(1, NUMBER_OF_ROUNDS - 1):
            block_matrix = add_round_key(block_matrix, subkeys[round])
            block_matrix = mix_columns(block_matrix, INVERSE_COLUMN_MIX)
            block_matrix = inverse_shift_rows(block_matrix)
            block_matrix = byte_sub(block_matrix, INVERSE_S_BOX)
        block_matrix = add_round_key(block_matrix, subkeys[-1])
        final_blocks.append(matrix_to_binary(block_matrix))
    return unpad_plaintext(''.join(final_blocks))
Ejemplo n.º 9
0
def decrypt(binary_ciphertext, binary_key):
    """Reveal binary plaintext from binary ciphertext with AES."""
    subkeys = list(reversed(key_schedule(binary_key)))
    final_blocks = []
    for block in block_split(binary_ciphertext, 128):
        block_matrix = binary_to_matrix(block)
        block_matrix = add_round_key(block_matrix, subkeys[0])
        block_matrix = inverse_shift_rows(block_matrix)
        block_matrix = byte_sub(block_matrix, INVERSE_S_BOX)
        for round in xrange(1, NUMBER_OF_ROUNDS - 1):
            block_matrix = add_round_key(block_matrix, subkeys[round])
            block_matrix = mix_columns(block_matrix, INVERSE_COLUMN_MIX)
            block_matrix = inverse_shift_rows(block_matrix)
            block_matrix = byte_sub(block_matrix, INVERSE_S_BOX)
        block_matrix = add_round_key(block_matrix, subkeys[-1])
        final_blocks.append(matrix_to_binary(block_matrix))
    return unpad_plaintext(''.join(final_blocks))
Ejemplo n.º 10
0
def encrypt(binary_plaintext, binary_key):
    """Generate binary ciphertext from binary plaintext with AES."""
    padded_plaintext = pad_plaintext(binary_plaintext, 128)
    subkeys = key_schedule(binary_key)
    final_blocks = []
    for block in block_split(padded_plaintext, 128):
        block_matrix = binary_to_matrix(block)
        block_matrix = add_round_key(block_matrix, subkeys[0])
        for round in xrange(1, 10):
            block_matrix = byte_sub(block_matrix)
            block_matrix = shift_rows(block_matrix)
            block_matrix = mix_columns(block_matrix, COLUMN_MIX)
            block_matrix = add_round_key(block_matrix, subkeys[round])
        block_matrix = byte_sub(block_matrix)
        block_matrix = shift_rows(block_matrix)
        block_matrix = add_round_key(block_matrix, subkeys[-1])
        final_blocks.append(matrix_to_binary(block_matrix))
    return ''.join(final_blocks)
Ejemplo n.º 11
0
def encrypt(binary_plaintext, binary_key):
    """Generate binary ciphertext from binary plaintext with AES."""
    padded_plaintext = pad_plaintext(binary_plaintext, 128)
    subkeys = key_schedule(binary_key)
    final_blocks = []
    for block in block_split(padded_plaintext, 128):
        block_matrix = binary_to_matrix(block)
        block_matrix = add_round_key(block_matrix, subkeys[0])
        for round in xrange(1, 10):
            block_matrix = byte_sub(block_matrix)
            block_matrix = shift_rows(block_matrix)
            block_matrix = mix_columns(block_matrix, COLUMN_MIX)
            block_matrix = add_round_key(block_matrix, subkeys[round])
        block_matrix = byte_sub(block_matrix)
        block_matrix = shift_rows(block_matrix)
        block_matrix = add_round_key(block_matrix, subkeys[-1])
        final_blocks.append(matrix_to_binary(block_matrix))
    return ''.join(final_blocks)
Ejemplo n.º 12
0
def decrypt(binary_ciphertext, modulus, private_key):
    """Reveal binary plaintext from binary ciphertext with RSA."""
    plaintext = ''.join(left_pad(rsa_exponentiation(block, modulus, private_key),
                                 plaintext_block_size())
                        for block in block_split(binary_ciphertext, MODULUS_BITS))
    return unpad_plaintext(plaintext)