def minus(N,n): subList = [] for v in N: if v != n: subList.append(v) return subList def crt(C, N): Sum = 0 NT = product(N) for c,n in zip(C,N): Sum += (c * product(minus(N,n)) * (eea(product(minus(N,n)),n) % n)) # for c,n in zip(C,N): # if Sum % n != c: # print "Error CRT" # print Sum % n, c, n # exit() return Sum % NT P = [11, 17, 41, 53, 27, 47, 31, 37] m = 55 N = [P[0] * P[3], P[1] * P[4], P[2] * P[5]] pN = [(P[0] -1) * (P[3] - 1), (P[1] -1) * (P[4] - 1), (P[2] -1) * (P[5] - 1)] E = [3, 3, 3] for e,n in zip(E,pN): print gcd(e,n) C = [(m ** e) % n for e,n in zip(E,N)] result = crt(C,N) print result ** (1/3.0)
Created on Jul 7, 2015 @author: mewolot ''' from ModMath import eea, gcd import math plaintext = 120 p1 = 13 q1 = 23 n1 = p1 * q1 et1 = (p1-1) * (q1-1) e1 = 7 #GCD must be 1 to work if gcd(e1,et1) == 1: print "Good e" else: print "Bad e" d1 = eea(e1,et1) % et1 pub1 = e1,n1 priv1 = d1,n1 c1 = plaintext ** e1 % n1 print c1, n1, d1 p2 = 17 q2 = 11 n2 = p2 * q2 et2 = (p2-1) * (q2-1) e2 = 7
Created on Jul 7, 2015 @author: mewolot ''' from ModMath import eea, gcd import math plaintext = 120 p1 = 13 q1 = 23 n1 = p1 * q1 et1 = (p1 - 1) * (q1 - 1) e1 = 7 #GCD must be 1 to work if gcd(e1, et1) == 1: print "Good e" else: print "Bad e" d1 = eea(e1, et1) % et1 pub1 = e1, n1 priv1 = d1, n1 c1 = plaintext**e1 % n1 print c1, n1, d1 p2 = 17 q2 = 11 n2 = p2 * q2 et2 = (p2 - 1) * (q2 - 1) e2 = 7 #GCD must be 1 to work
''' import gensafeprime as prime from ModMath import eea, gcd # p = 7919 # q = 6271 # n = p * q # et = (p-1) * (q-1) # e = 97 # #GCD must be 1 to work # print gcd(e,et) # d = eea(e,et) % et # pub = e,n # priv = d,n # # plaintext = 1337808 # c = plaintext ** e % n # m = c ** d % n # print plaintext, c, plaintext == m # print # print c,n,d # print c ** d % n # print p = 17 q = 11 n = p * q et = (p - 1) * (q - 1) e = 7 print gcd(e, et) print eea(e, et) % et
for v in N: if v != n: subList.append(v) return subList def crt(C, N): Sum = 0 NT = product(N) for c, n in zip(C, N): Sum += (c * product(minus(N, n)) * (eea(product(minus(N, n)), n) % n)) # for c,n in zip(C,N): # if Sum % n != c: # print "Error CRT" # print Sum % n, c, n # exit() return Sum % NT P = [11, 17, 41, 53, 27, 47, 31, 37] m = 55 N = [P[0] * P[3], P[1] * P[4], P[2] * P[5]] pN = [(P[0] - 1) * (P[3] - 1), (P[1] - 1) * (P[4] - 1), (P[2] - 1) * (P[5] - 1)] E = [3, 3, 3] for e, n in zip(E, pN): print gcd(e, n) C = [(m**e) % n for e, n in zip(E, N)] result = crt(C, N) print result**(1 / 3.0)
''' import gensafeprime as prime from ModMath import eea,gcd # p = 7919 # q = 6271 # n = p * q # et = (p-1) * (q-1) # e = 97 # #GCD must be 1 to work # print gcd(e,et) # d = eea(e,et) % et # pub = e,n # priv = d,n # # plaintext = 1337808 # c = plaintext ** e % n # m = c ** d % n # print plaintext, c, plaintext == m # print # print c,n,d # print c ** d % n # print p = 17 q = 11 n = p * q et = (p-1) * (q-1) e = 7 print gcd(e,et) print eea(e,et) % et