Exemplo n.º 1
0
def selectE(fn, halfkeyLength):
    while True:
        # e and fn are relatively prime
        e = random.randint(0, 1 << halfkeyLength)
        (x, y, r) = extendedGCD(e, fn)
        if r == 1:
            return e
Exemplo n.º 2
0
def modularInverse(a, n):
    assert (extendedGCD(a, n) == 1)

    s = 0
    t = 1
    r = n
    old_s = 1
    old_t = 0
    old_r = a

    while r != 0:
        quotient = old_r // r
        old_r, r = r, old_r - quotient * r
        old_s, s = s, old_s - quotient * s
        old_t, t = t, old_t - quotient * t

    if old_s < 0:
        old_s = n + old_s

    return old_s
Exemplo n.º 3
0
def computeD(fn, e):
    (x, y, r) = extendedGCD(fn, e)
    # y maybe < 0, so convert it
    if y < 0:
        return fn + y
    return y