Ejemplo n.º 1
0
def generate_secret_key(inverse_size=64, p=P):
    """ usage: generate_secret_key(inverse_size=64, p=P) => a, ai
    
        Returns 2 integers. 
        The size of the inverse places a limit on how many cipher multiplications may be performed. """
    ai = random_integer(inverse_size)
    return modular_inverse(ai, p), ai
Ejemplo n.º 2
0
def generate_secret_key(inverse_size=64, p=P):
    """ usage: generate_secret_key(inverse_size=64, p=P) => a, ai
    
        Returns 2 integers. 
        The size of the inverse places a limit on how many cipher multiplications may be performed. """
    ai = random_integer(inverse_size)
    return modular_inverse(ai, p), ai
Ejemplo n.º 3
0
def generate_key2(size_in_bytes, modulus=256):
    """ Generates random numbers that have a modular inverse. """
    key_material = iter(bytearray(urandom((size_in_bytes * 2) + 64)))
    key2 = bytearray()
    while len(key2) < size_in_bytes:
        try:
            key_byte = next(key_material)
        except StopIteration:
            key_material = iter(bytearray(urandom((size_in_bytes * 2) + 64)))
        try:
            modular_inverse(key_byte, modulus)
        except ValueError:
            pass
        else:
            key2.append(key_byte)    
    return key2
Ejemplo n.º 4
0
def generate_key2(size_in_bytes, modulus=256):
    """ Generates random numbers that have a modular inverse. """
    key_material = iter(bytearray(urandom((size_in_bytes * 2) + 64)))
    key2 = bytearray()
    while len(key2) < size_in_bytes:
        try:
            key_byte = next(key_material)
        except StopIteration:
            key_material = iter(bytearray(urandom((size_in_bytes * 2) + 64)))
        try:
            modular_inverse(key_byte, modulus)
        except ValueError:
            pass
        else:
            key2.append(key_byte)
    return key2
Ejemplo n.º 5
0
def decrypt(ciphertext, key, key_removal_subroutine=multiplication_subroutine, modulus=DEFAULT_MODULUS, multiplier=1):   
    message_size = len(ciphertext) / 2
    key = [pow(modular_inverse(byte | 1, DEFAULT_MODULUS), multiplier, modulus) for byte in key]    
    key_removal_subroutine(ciphertext, key[message_size:], modulus)
    ciphertext[:] = decode(ciphertext)         
    key_removal_subroutine(ciphertext, key[:message_size], modulus)
    return ciphertext                
Ejemplo n.º 6
0
def generate_backdoor_public_key(private_key, parameters=PARAMETERS):
    """ usage: generate_backdoor_public_key(private_key, parameters=PARAMETERS) => public_key : tuple
    
        Generates a public key for use as parameters in the key agreement scheme. 
        It is recommended to use backdoor_generate_keypair instead. """
    d, n, n_k = private_key
    e = modular_inverse(d, n_k)    
    return e, n
Ejemplo n.º 7
0
def generate_backdoor_public_key(private_key, parameters=PARAMETERS):
    """ usage: generate_backdoor_public_key(private_key, parameters=PARAMETERS) => public_key : tuple
    
        Generates a public key for use as parameters in the key agreement scheme. 
        It is recommended to use backdoor_generate_keypair instead. """
    d, n, n_k = private_key
    e = modular_inverse(d, n_k)
    return e, n
Ejemplo n.º 8
0
def generate_backdoor_private_key(parameters=PARAMETERS):
    """ usage: generate_backdoor_private_key(parameters=PARAMETERS) => backdoor_key : tuple
    
        Generates a private backdoor key. 
        It is recommended to use generate_backdoor_keypair instead. """
    d_size = parameters["d_size"]
    n_size = parameters["n_size"]
    k = random_integer(parameters["k_size"])     
    while True:
        d = random_integer(d_size) | (1 << (d_size * 8)) 
        n = random_integer(n_size) | (1 << (n_size * 8))             
        try:
            modular_inverse(d, n - k)
        except ValueError:
            continue
        else:
            break            
    return d, n, n - k
Ejemplo n.º 9
0
def generate_backdoor_private_key(parameters=PARAMETERS):
    """ usage: generate_backdoor_private_key(parameters=PARAMETERS) => backdoor_key : tuple
    
        Generates a private backdoor key. 
        It is recommended to use generate_backdoor_keypair instead. """
    d_size = parameters["d_size"]
    n_size = parameters["n_size"]
    k = random_integer(parameters["k_size"])
    while True:
        d = random_integer(d_size) | (1 << (d_size * 8))
        n = random_integer(n_size) | (1 << (n_size * 8))
        try:
            modular_inverse(d, n - k)
        except ValueError:
            continue
        else:
            break
    return d, n, n - k
Ejemplo n.º 10
0
def decrypt(ciphertexts, key, rounds=1, modulus=256, blocksize=16):
    output = bytearray()    
    key = [modular_inverse(item | 1, modulus) for item in key]
    for index, block in enumerate(slide(ciphertexts, 2)):
        block[1] = (block[1] * key[1]) % modulus
        block[0] = (block[0] * key[0]) % modulus
        block[0] = modular_subtraction(block[0], block[1], modulus)        
        output.append(block[0])
    return output
Ejemplo n.º 11
0
def decrypt(ciphertexts, key, rounds=5, modulus=DEFAULT_MODULUS):  
    """ usage: decrypt(ciphertexts, key, 
                       rounds=5, modulus=DEFAULT_MODULUS) => plaintext
                       
        Given ciphertexts and key, return plaintexts. """        
    output = [] 
    for ciphertext in slide(ciphertexts, 2 ** rounds):        
        multiplication_subroutine(ciphertext, [modular_inverse(item | 1, modulus) for item in key], modulus)            
        for round in range(rounds):    
            ciphertext[:] = decode(ciphertext, modulus)          
        output.append(ciphertext[0])   
    return output
Ejemplo n.º 12
0
def decrypt(ciphertext,
            key,
            key_removal_subroutine=multiplication_subroutine,
            modulus=DEFAULT_MODULUS,
            multiplier=1):
    message_size = len(ciphertext) / 2
    key = [
        pow(modular_inverse(byte | 1, DEFAULT_MODULUS), multiplier, modulus)
        for byte in key
    ]
    key_removal_subroutine(ciphertext, key[message_size:], modulus)
    ciphertext[:] = decode(ciphertext)
    key_removal_subroutine(ciphertext, key[:message_size], modulus)
    return ciphertext
Ejemplo n.º 13
0
def decrypt(ciphertexts, key, rounds=5, modulus=DEFAULT_MODULUS):
    """ usage: decrypt(ciphertexts, key, 
                       rounds=5, modulus=DEFAULT_MODULUS) => plaintext
                       
        Given ciphertexts and key, return plaintexts. """
    output = []
    for ciphertext in slide(ciphertexts, 2**rounds):
        multiplication_subroutine(
            ciphertext, [modular_inverse(item | 1, modulus) for item in key],
            modulus)
        for round in range(rounds):
            ciphertext[:] = decode(ciphertext, modulus)
        output.append(ciphertext[0])
    return output
Ejemplo n.º 14
0
def decrypt(data, key, rounds=1, modulus=256, multiplier=None):
    size = len(data)     
    inverse_key2 = [modular_inverse(item, modulus) for item in key[-size:]]
    key1 = convert_key(key[:size])    
    for round in range(rounds):
        key_index = sum(range(2, size))        
        multiplication_subroutine(data, inverse_key2, modulus)
        for index1 in reversed(range(size)):
            for index2 in range(index1 + 1, size):                
                word1, word2 = data[index1], data[index2]                                
                word1, word2 = invert_psuedohadamard_transform(word1, word2, modulus)           
                data[index1], data[index2] = choice_swap(key1[key_index % len(key1)], word1, word2)            
                key_index -= 1                                    
    
    multiplication_subroutine(data, inverse_key2, modulus)      
    return data
Ejemplo n.º 15
0
def decrypt(data, key, rounds=1, modulus=256, multiplier=None):
    size = len(data)
    inverse_key2 = [modular_inverse(item, modulus) for item in key[-size:]]
    key1 = convert_key(key[:size])
    for round in range(rounds):
        key_index = sum(range(2, size))
        multiplication_subroutine(data, inverse_key2, modulus)
        for index1 in reversed(range(size)):
            for index2 in range(index1 + 1, size):
                word1, word2 = data[index1], data[index2]
                word1, word2 = invert_psuedohadamard_transform(
                    word1, word2, modulus)
                data[index1], data[index2] = choice_swap(
                    key1[key_index % len(key1)], word1, word2)
                key_index -= 1

    multiplication_subroutine(data, inverse_key2, modulus)
    return data