Esempio n. 1
0
def giant_step(alpha, p, fname):
    m = getm(p)
    fid = open(fname, 'w')
    for xg in xrange(m):
        fid.write(
            str(primes.square_multiply(alpha, m * xg, p)) + ',' + str(xg) +
            '\n')
    fid.close()
Esempio n. 2
0
def baby_step(alpha, beta, p, fname):
    m = getm(p)
    fid = open(fname, 'w')
    for xb in xrange(m):
        fid.write(
            str((primes.square_multiply(alpha, xb, p) * beta) % p) + ',' +
            str(xb) + '\n')
    fid.close()
def giant_step(alpha, p, fname):
    m = calc_m(p)
    inter_ls = []
    for xg in range(m):
        inter = primes.square_multiply(alpha, (m * xg), p)
        inter_ls.append(str(inter))
    with open(fname, "w") as outfile:
        output = "\n".join(inter_ls)
        outfile.write(output)
    return inter_ls
def baby_step(alpha, beta, p, fname):
    m = calc_m(p)
    inter_ls = []
    for xb in range(m):
        inter = (primes.square_multiply(alpha, xb, p) * beta) % p
        inter_ls.append(str(inter))
    with open(fname, "w") as outfile:
        output = "\n".join(inter_ls)
        outfile.write(output)
    return inter_ls
Esempio n. 5
0
def giant_step(alpha, p, fname):
    m = math.ceil(math.sqrt(p))
    with open(fname, 'w') as giant_file:
        for x_g in range(m):
            # giant_file.write(str((alpha**(m*x_g)) % p)+'\n')
            giant_file.write(str(primes.square_multiply(alpha, m*x_g, p)) + ',' + str(x_g) + '\n')

    giant_list = [line.strip().split(',') for line in open(fname, 'r').readlines()]

    return giant_list
Esempio n. 6
0
def decrypt_cipher(cipher, n, d):
    message_int = square_multiply(cipher, d, n)
    message_bytes = int_to_bytes(message_int)
    return message_bytes.decode('utf-8')
Esempio n. 7
0
def encrypt_message(message, n, e):
    message_bytes = message.encode('utf-8')
    message_int = int.from_bytes(message_bytes, "little")
    return square_multiply(message_int, e, n)
Esempio n. 8
0
    print("message hash is the same as hash from signature!")
    print()

    print("------------- Part II -------------")
    print("------ RSA Encryption Protocol Attack ------")
    ip_int = 100
    print("Encrypting: {}".format(ip_int))
    print()

    ip_int_bytes = int_to_bytes(ip_int).decode('utf-8')
    y = encrypt_message(ip_int_bytes, n, e)
    print("Result: ")
    print(base64.b64encode(int_to_bytes(y)).decode("utf-8"))
    print()

    y_s = square_multiply(2, e, n)
    m = y * y_s % n
    print("Modified to: ")
    print(base64.b64encode(int_to_bytes(m)).decode("utf-8"))
    print()

    decrypted_int = square_multiply(m, d, n)
    print("Decrypted: {}".format(decrypted_int))
    print()

    print("------ RSA Digital Signature Attack ------")
    print("POV - Eve")
    random_signature = signature + random.randint(-sys.maxsize, sys.maxsize)
    print("random_signature: {}".format(random_signature))

    digest_x = verify_signature(random_signature, n, e)
Esempio n. 9
0
def get_pub_key(alpha, a, p):
    public_key = primes.square_multiply(alpha, a, p)
    return public_key
Esempio n. 10
0
def giant_step(alpha,p,m,giant_list):
    for i in xrange(m):
        res = primes.square_multiply(alpha, (m*i), p)
        giant_list.append(res)
Esempio n. 11
0
                # print j, giant_list[j]
                break

    # print "xg:",xg, "xb:", xb
    answer = xg * m - xb
    return answer


if __name__=="__main__":
    """
    test 1
    My private key is:  264
    Test other private key is:  7265

    """
    p=17851
    print find_m(p)
    alpha=17511
    A=2945
    B=11844
    sharedkey=1671
    a=baby_giant(alpha,A,p)
    b=baby_giant(alpha,B,p)
    print a, b
    guesskey1=primes.square_multiply(A,b,p)
    guesskey2=primes.square_multiply(B,a,p)
    print 'Guess key 1:',guesskey1
    print 'Guess key 2:',guesskey2
    print 'Actual shared key :',sharedkey

Esempio n. 12
0
def get_pub_key(alpha, a, p):
    return primes.square_multiply(alpha, a, p)
Esempio n. 13
0
def baby_step(alpha, beta, p, m, baby_list):
    for i in xrange(m):
        # res = math.pow(alpha,i)*beta
        res = (primes.square_multiply(alpha, i, p) * beta) % p
        baby_list.append(res)
Esempio n. 14
0
def giant_step(alpha, p, m, giant_list):
    for i in xrange(m):
        res = primes.square_multiply(alpha, (m * i), p)
        giant_list.append(res)
Esempio n. 15
0
def get_signature(h_hex, n, d):
    h_int = int(h_hex, 16)
    return square_multiply(h_int, d, n)
Esempio n. 16
0
def verify_signature(signature, n, e):
    message_int = square_multiply(signature, e, n)
    return hex(message_int).lstrip("0x")
Esempio n. 17
0
def baby_step(alpha,beta,p,m,baby_list):
    for i in xrange(m):
        # res = math.pow(alpha,i)*beta
        res = (primes.square_multiply(alpha,i,p) * beta ) % p
        baby_list.append(res)
Esempio n. 18
0
def get_shared_key(keypub, keypriv, p):
    shared_key = primes.square_multiply(keypub, keypriv, p)
    return shared_key
if __name__ == "__main__":
    """
    test 1
    My private key is:  264
    Test other private key is:  7265
    
    """
    p = 17851
    alpha = 17511
    A = 2945
    B = 11844
    sharedkey = 1671
    a = baby_giant(alpha, A, p)
    b = baby_giant(alpha, B, p)
    guesskey1 = primes.square_multiply(A, b, p)
    guesskey2 = primes.square_multiply(B, a, p)
    print('Guess key 1:', guesskey1)
    print('Guess key 2:', guesskey2)
    print('Actual shared key :', sharedkey)

    for i in range(3):
        # generates 16-bit shared keys and breaks them, taking note of the time
        print("trial {}".format(i + 1))
        p, alpha = dhke.dhke_setup(16)
        a = dhke.gen_priv_key(p)
        b = dhke.gen_priv_key(p)
        A = dhke.get_pub_key(alpha, a, p)
        B = dhke.get_pub_key(alpha, b, p)
        sharedkey = dhke.get_shared_key(B, a, p)