Exemple #1
0
def verify(pk, m, s, p, q, g):
    (s1, s2) = s
    s2I = multInv(s2, q)

    pks1 = modExp(pk, s1, p)
    gm = modExp(g, m, p)

    s1N = modExp(gm * pks1, s2I, p) % q

    if (s1 == s1N):
        return 1
    else:
        return 0
def verify(pk, m, s):
    (n, e) = pk
    ms = modExp(s, e, n)
    if (ms == m):
        return 1
    else:
        return 0
Exemple #3
0
def sign(sk, m, p, g, q):
    r = randRelPrime(q)
    s1 = modExp(g, r, p) % q

    rI = multInv(r, q)
    s2 = rI * (m + sk * s1) % q
    return (s1, s2)
def keyGen(k):
    (p, q) = getRandSafePrime(k)
    g = findGenSubGroup(p, q)
    #sk is element of Zq
    sk = randInt(q - 1)
    #pk is element of Gq
    pk = modExp(g, sk, p)
    return (pk, sk, p, q, g)
Exemple #5
0
        print(i)
print()

print("gcd(7,15)", helper.gcd(7, 15))
print("gcd(9,15)", helper.gcd(9, 15))
print("gcd(10,15)", helper.gcd(10, 15))
print("gcd(11,15)", helper.gcd(11, 15))
print()

print("multInv(7,1)", helper.multInv(7, 11))
print("multInv(1,11)", helper.multInv(1, 11))
print("multInv(3,11)", helper.multInv(3, 11))
print("multInv(8,11)", helper.multInv(8, 11))
print()

print("modexp(2,7,11)", helper.modExp(2, 7, 11))
print("modexp(5,17,11)", helper.modExp(5, 17, 11))
print("modexp(8,3,11)", helper.modExp(8, 3, 11))
print("modexp(4,8,11)", helper.modExp(4, 8, 11))

print("randprime", helper.randPrime(8))
print("randprime", helper.randPrime(40))
print("randprime", helper.randPrime(3))

print("randrelprime", helper.randRelPrime(9))
print("randrelprime", helper.randRelPrime(8))
print("randrelprime", helper.randRelPrime(40))

print("modexp(4,8,11)", helper.modExp(2, 3, 15))
print("modexp(4,8,11)", helper.modExp(8, 3, 15))
def sign(sk, m):
    (n, d) = sk
    s = modExp(m, d, n)
    return s
def dec(c, sk):
    (n, d) = sk
    return modExp(c, d, n)
def enc(m, pk):
    (n, e) = pk
    return modExp(m, e, n)
def dec(sk, c, p, q, g):
    (a, b) = c
    s = modExp(a, sk, p)
    m = (b * multInv(s, p)) % p
    #m = b*(modExp(a,multInv(sk,p),p)) % p
    return m
def enc(pk, m, p, q, g):
    r = randInt(q - 1)
    print("R", r)
    c = (modExp(g, r, p), m * modExp(pk, r, p) % p)
    return c