Exemple #1
0
from helper import *
from generate_keys import gen_keys


# Asymmetric encryption
def encrypt(msg, public_key):
    h, q, g = public_key
    k = rand_coprime_with(q)  # Private key for sender
    s = power(h, k, q)

    en_msg = []
    for i in range(0, len(msg)):
        en_msg.append(msg[i])
    for i in range(0, len(en_msg)):
        en_msg[i] = s * ord(en_msg[i])

    p = power(g, k, q)
    cipher = [p, en_msg]
    return cipher


if __name__ == '__main__':

    public_key, private_key = gen_keys()
    print("Enter Message to be encoded : ")
    msg = input()

    cipher = encrypt(msg, public_key)
    print(cipher)
Exemple #2
0
def decrypt(cipher, keys):
    public_key, private_key = keys
    n, _ = public_key
    d = private_key[0]
    en_msg = cipher[0]

    hlen = 20  # SHA-1 hash length
    k = ceil(n.bit_length() / 8)
    assert len(en_msg) == k
    assert k >= 2 * hlen + 2

    dr_msg = pow(os2ip(en_msg), d, n)
    dr_msg = i2osp(dr_msg, k)

    dr_msg = oaep_decode(dr_msg, k).decode()
    return dr_msg


if __name__ == '__main__':
    keys = gen_keys(256)
    public_key, private_key = keys
    print(public_key, private_key)
    print("Enter Message to be encoded : ")
    msg = input()

    cipher = encrypt(msg, public_key)
    print(cipher)

    decoded_text = decrypt(cipher, keys)
    print(decoded_text)
Exemple #3
0
from helper import *
from generate_keys import gen_keys
from encrypt import encrypt


def decrypt(cipher, keys):
    public_key, private_key = keys
    h, q, g = public_key
    a = private_key[0]
    p, en_msg = cipher

    dr_msg = []
    h = power(p, a, q)
    for i in range(0, len(en_msg)):
        dr_msg.append(chr(int(en_msg[i] / h)))
    return dr_msg


if __name__ == '__main__':
    keys = gen_keys()
    public_key, private_key = keys
    print("Enter Message to be encoded : ")
    msg = input()

    cipher = encrypt(msg, public_key)
    print(cipher)

    decoded_text = decrypt(cipher, keys)
    print(decoded_text)
Exemple #4
0
from helper import *
from generate_keys import gen_keys
from encrypt import encrypt


def decrypt(cipher, keys):
    public_key, private_key = keys
    n, e = public_key
    d = private_key[0]
    en_msg = cipher[0]
    dr_msg = pow(en_msg, d, n)
    dr_msg = i2osp(dr_msg, n.bit_length() // 8).decode()
    return dr_msg


if __name__ == '__main__':
    keys = gen_keys(128)
    public_key, private_key = keys
    print(public_key, private_key)
    print("Enter Message to be encoded : ")
    msg = input()

    cipher = encrypt(msg, public_key)
    print(cipher)

    decoded_text = decrypt(cipher, keys)
    print(decoded_text)
Exemple #5
0
from generate_keys import gen_keys
from encrypt import encrypt


def decrypt(cipher, keys):
    public_key, private_key = keys
    n, e = public_key
    d = private_key[0]
    en_msg = cipher[0]

    dr_msg = []
    for i in range(0, len(en_msg)):
        dr_msg.append(chr(power(en_msg[i], d, n)))

    # dr_msg = power(en_msg, d, n)
    return dr_msg


if __name__ == '__main__':
    keys = gen_keys(16)
    public_key, private_key = keys
    print(public_key, private_key)
    print("Enter Message to be encoded : ")
    msg = input()
    # msg = int(input())

    cipher = encrypt(msg, public_key)
    print(cipher)

    decoded_text = decrypt(cipher, keys)
    print(decoded_text)