def compare_multiple_keys(amount_of_test_values=100, QR_depth_index=0, sorted:bool = False):
    # Setup
    stat_nonce = '0' * 128
    message = '0'
    key = '1' + '0' * 127
    assert len(key) == len(stat_nonce) == 128

    # Get QR from key.
    sal = Salsa20(mode='test', static_nonce=stat_nonce)
    ciphertext = sal.encrypt(message, key)
    QR_0 = sal.prg.QR_x[QR_depth_index]
    QR_0 = to_ints(QR_0)
    
    # Make key guesses.
    key_guesses = []
    for i in range(amount_of_test_values):
        key_guesses.append(get_random_binary(len(key)))
    key_guesses.sort()

    # Get QR from each key guess, and compare with original QR.
    key_comparisons = []
    for i in range(len(key_guesses)):
        sal = Salsa20(mode='test', static_nonce=stat_nonce)
        ciphertext = sal.encrypt(message, key_guesses[i])
        QR_i = sal.prg.QR_x[QR_depth_index]
        QR_i = to_ints(QR_i)
        similarity = Pearson_correlation_coefficient(QR_0, QR_i)
        key_comparisons.append(similarity)
    
    if sorted:
        key_comparisons.sort()
    
    # Plot
    bar_chart(key_comparisons)
    return key_comparisons
def test_init():
    sal1 = Salsa20()

    stat_nonce = '11011011'*16
    sal2 = Salsa20(mode='test', static_nonce=stat_nonce)

    assert type(sal1) is type(sal2) is Salsa20
    assert type(sal1.prg) is type(sal2.prg) is PRG

    assert sal1.static_nonce == None
    assert sal2.static_nonce == stat_nonce
def test_decrypt():
    sal = Salsa20()
    plaintext = 'Hello World!'
    key = '10010110'*16
    ciphertext, nonce = sal.encrypt(plaintext, key)
    decrypted = sal.decrypt(ciphertext, key, nonce)
    assert len(ciphertext) == 64
def run():
    sa = Salsa20(static_nonce=get_nonce())
    key = get_key()

    pe = Pair_exporter()

    lines = 1000
    files = 2
    for i in range(files):
        #print(pe.status_scan())
        #new_num = pe.status_scan() + lines

        output = ''
        last_num = pe.status_scan()
        new_num = last_num + lines
        for j in range(lines):
            current_pair = new_num + j  #i*lines + j + last_num
            pt = gen_random_string(128)
            ct, nonce = sa.encrypt(pt, key)
            assert len(pt) == len(ct) == 128
            #print(len(pt), len(ct))
            #print(len(to_binary(pt)), len(to_binary(ct)))
            #print()
            #output += str(current_pair) + ',\t' + to_binary(pt) + ',\t' + to_binary(ct) + '\n'
            output += str(current_pair) + ',\t' + to_binary(
                pt) + ',\t' + to_binary(ct) + '\n'

        pe.store(output, new_num)
def test_encrypt():
    # Setup
    sal = Salsa20()
    plaintext = 'Hello World!'
    key = '10010110'*16

    # Test 1
    ciphertext_1, nonce_1 = sal.encrypt(plaintext, key)
    assert type(ciphertext_1) is str
    assert len(ciphertext_1) == 64
    assert type(nonce_1) is str
    assert len(nonce_1) == 64

    # Test 2
    input_nonce_2 = sal.generate_nonce()

    ciphertext_2, nonce_2 = sal.encrypt(plaintext, key, input_nonce_2)
    assert type(ciphertext_1) is str
    assert len(ciphertext_1) == 64
    assert type(nonce_1) is str
    assert len(nonce_1) == 64
    assert nonce_2 == input_nonce_2

    # Combined test
    assert ciphertext_1 != ciphertext_2
    assert nonce_1 != nonce_2
def next_element_comparison(sorted:bool = False):
    # Setup
    stat_nonce = '0' * 128 #'11011011'*16
    message = '0' #'Hello World!'
    key = '1' * 128 #'10010010'*16

    sal = Salsa20(mode='test', static_nonce=stat_nonce)

    # Encrypt.
    ciphertext = sal.encrypt(message, key)


    # Get and print the values internal QR values.
    QR_x = sal.prg.QR_x
    QR_y = sal.prg.QR_y
    DRF_in = sal.prg.DRF_in
    assert len(QR_x) == len(QR_y) == 80
    assert len(DRF_in) == 10

    #print_list_of_lists(QR_x)
    QR_x_int = to_ints(QR_x)
    pearson_results = Pearson_on_list_of_lists(QR_x_int)
    for i in range(len(pearson_results)):
        print(i, '-', i+1, '\t', pearson_results[i])
    
    if sorted:
        pearson_results.sort()
    
    # Plot
    bar_chart(pearson_results)
    return pearson_results
def test_mode():
    data = 'Hello World!'
    stat_nonce = '11011011'*16
    key = '10010110'*16

    sal1 = Salsa20()
    sal2 = Salsa20(static_nonce=stat_nonce, mode='test')

    assert sal1.mode == 'full'
    assert sal2.mode == 'test'

    ct1, nonce1 = sal1.encrypt(data, key)
    ct2, nonce2 = sal2.encrypt(data, key)

    assert ct1 != ct2
    assert nonce1 != nonce2
def test_generate_block_number():
    sal = Salsa20()
    number = 1234
    binary_number = sal.generate_block_number(number)
    assert type(binary_number) is str
    assert len(binary_number) == 64
    assert binary_number == '0000000000000000000000000000000000000000000000000000010011010010'
 def gen_QR(self, key, data):
     self.sal = Salsa20(mode='test', static_nonce=self.stat_nonce)
     ciphertext = self.sal.encrypt(key=key, data=data)
     self.QR_x = self.sal.prg.QR_x
     self.QR_y = self.sal.prg.QR_y
     self.key = key
     self.data = data
     self.gen_QR_runs += 1
def test_add_padding():
    sal = Salsa20()
    a = '01101001'
    b = sal.add_padding(a)
    assert len(b) == 512
    for i in range(512-len(a)):
        assert b[i + len(a)] == '0'
    assert b[0:8] == a
    def __init__(self):
        self.stat_nonce = '0' * 128
        self.sal = Salsa20(mode='test', static_nonce=self.stat_nonce)
        self.prg = PRG(test_mode=True)

        self.key = None
        self.data = None

        self.QR_x = None
        self.QR_y = None

        self.gen_QR_runs = 0
        self.get_QR_runs = 0
def test_readable_full_crypto():
    # Setup
    sal = Salsa20()
    plaintext = "Hello World! How are ye doin' today, matey?"
    
    key = ''
    for i in range(256):
        key += random.choice('01')
    
    # Encrypt --> decrypt --> test.
    ciphertext, nonce = sal.encrypt(plaintext, key)
    decrypted_plaintext = sal.decrypt(ciphertext, key, nonce)
    assert plaintext == decrypted_plaintext
def test_generate_nonce():
    sal = Salsa20()
    nonce = sal.generate_nonce()
    assert type(nonce) is str
    assert len(nonce) == 64

    for char in nonce:
        assert char == '0' or char == '1'
    
    nonce_list = []
    for i in range(100):
        nonce_list.append(sal.generate_nonce())
    
    nonce_set = set(nonce_list)
    assert len(nonce_list) == len(nonce_set)
def full_crypto(p_size, key_size, nonce=None):
    sal = Salsa20()

    # Generate random plaintext
    plaintext = ''
    for i in range(p_size):
        plaintext += random.choice('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
    
    # Generate random key
    key = ''
    for i in range(key_size):
        key += random.choice('01')

    # Encrypt --> decrypt --> return.
    ciphertext, nonce = sal.encrypt(plaintext, key, nonce)
    decrypted_plaintext = sal.decrypt(ciphertext, key, nonce)
    return plaintext, decrypted_plaintext
def test_chacha_salsa_init():
    cha = Salsa20(chacha=True)
    assert type(cha.prg) is Chacha_PRG
Beispiel #16
0
def run_Salsa20():
    runs = 100
    sal = Salsa20()
    binary_numbers, keys, nonces = Salsa20_setup(runs, 512, 256)
    total_time = Salsa20_test(binary_numbers, keys, nonces, sal.encrypt)
    return runs, total_time, runs/total_time
def test_to_text():
    sal = Salsa20()
    binary = '01000001010000100100001101000100'
    text = sal.to_text(binary)
    assert text == 'ABCD'
def test_xor():
    sal = Salsa20()
    a = '01101001'
    b = '01010100'
    c = '00111101'
    assert sal.xor(a, b) == c
Beispiel #19
0
for filename in os.listdir(read_dir):
    if filename.endswith('.txt'):
        files.append(filename)


# Set up crypto
def get_random_bin(length):
    string = ''
    for i in range(length):
        string += random.choice(['0', '1'])
    return string


key = get_random_bin(128)
nonce = get_random_bin(64)
sa = Salsa20()

for file_name in files:
    print(file_name)
    read_path = os.path.join(read_dir, file_name)
    write_path = os.path.join(write_dir, file_name)
    # Read file
    f = open(read_path, 'r', encoding='utf8')
    PT = f.read()

    # Encrypt
    CT, nonce = sa.encrypt(PT, key, nonce)

    # Store file
    f = open(write_path, 'w', encoding='utf8')
    f.write(CT)
def test_remove_padding():
    sal = Salsa20()
    a = '01000001'
    b = a + '00000000'*63
    assert sal.remove_padding(b) == a
def test_to_binary():
    sal = Salsa20()
    text = 'ABCD'
    binary = sal.to_binary(text)
    assert binary == '01000001010000100100001101000100'