def main(): p, q = find_p_q() #1 print 'Moving on..' n = p * q # 2 et = euler(p, q) # 3 e = (2**16) + 1 # 4 d = mulinv(e, et) #5 #6 NA """ Test values """ m1 = 8049 encm1 = rypto.mod_exp(m1, e, n) decm1 = rypto.mod_exp(encm1, d, n) print 'Checking m1 is decrypted correctly' print m1, decm1, m1 == decm1 m2 = 'Hi this is short' m2encoded = int(m2.encode("hex"), 16) encm2 = rypto.mod_exp(m2encoded, e, n) decm2 = rypto.mod_exp(encm2, d, n) hexad = rypto.standard(hex(decm2)) decm2b = hexad[:-1].decode("hex") print 'Checking m2 is decrypted correctly' print m2, decm2b, m2 == decm2b
def main(): N = 402394248802762560784459411647796431108620322919897426002417858465984510150839043308712123310510922610690378085519407742502585978563438101321191019034005392771936629869360205383247721026151449660543966528254014636648532640397857580791648563954248342700568953634713286153354659774351731627683020456167612375777 e1 = 3 e2 = 0x10001 c1 = 4020137574131575546540268502595841326627069047574502831387774931737219358054228401772587980633053000 c2 = 170356929377044754324767086491413709789303946387160918939626824506821140429868670769571821346366209258416985269309515948776691067548265629489478628756185802183547222688698309731374342109385922509501909728895585636684978295199882599818258590851085977232207148101448845575681189389906429149193460620083999406237 g, x, y = rypto.egcd(e1, e2) #Euclidean algorightm i = rypto.mulinv(c2, N) #multiplicative inverse of C2 because y is -1 test = (rypto.mod_exp(c1, x, N) * rypto.mod_exp(i, -y, N)) % N #plain = (c1^a) * (i^-b) %N answer = rypto.standard(hex(test)).decode("hex") print answer
def diffie(p, g): a = int((os.urandom(256).encode("hex")), 16) % p b = int((os.urandom(256).encode("hex")), 16) % p A = rypto.mod_exp(g, a, p) B = rypto.mod_exp(g, b, p) s1 = rypto.mod_exp(A, b, p) s2 = rypto.mod_exp(B, a, p) if s1 == s2: print 'Keys are the same' return s1 else: print 'Key exchange failed' exit()
def main(): session = setup() #Setup mutual variables a = int((os.urandom(256).encode("hex")),16) % session.n #Secret value A (For client) A = rypto.mod_exp(session.g,a,session.n) i = session.i #username (will soon send) #client_recv = None """ Server stores the password using the steps: 1. Generate salt as a random integer 2. Generate string xH = SHA256(salt|password) 3. Convert xH to integer x 4. Generate v = g^x mod N 5. Save everything but x, xH """ session.store() #Simulating server storing values to db """ Client -> Server: """ #session.client_send(i, A) #Simulating sending username and public A for auth session.client_send(i, 0) #Hacker sends 1 """ Server -> Client: """ user_recv = session.response() #Simulating receiving salt and public B from server client_recv = {'s': user_recv[0], 'B':user_recv[1] } #Storing previous line into something easier to understand """ Server, Client: """ session.calc_u() #Server calculating u """ Server: 1. Generate S = (A*v^u)^b mod N 2. Generate K = SHA256(S) """ session.generate_k() """ Validate """ print 'Hacker trying Key 0....' hacker_k = rypto.sha256(str(0)) user_try = rypto.sha256(hacker_k+str((client_recv['s']))) session.validate(user_try)
def main(): session = setup() #Setup mutual variables a = int((os.urandom(256).encode("hex")),16) % session.n #Secret value A (For client) A = rypto.mod_exp(session.g,a,session.n) i = session.i #username (will soon send) #client_recv = None """ Server stores the password using the steps: 1. Generate salt as a random integer 2. Generate string xH = SHA256(salt|password) 3. Convert xH to integer x 4. Generate v = g^x mod N 5. Save everything but x, xH """ session.store() #Simulating server storing values to db """ Client -> Server: """ session.client_send(i, A) #Simulating sending username and public A for auth """ Server -> Client: """ user_recv = session.response() #Simulating receiving salt and public B from server client_recv = {'s': user_recv[0], 'B':user_recv[1] } #Storing previous line into something easier to understand """ Server, Client: """ uH = rypto.sha256(str(A) + str(client_recv['B']))#Client calculating u client_u = int(uH,16) #Client calculating u session.calc_u() #Server calculating u """ Client: 1. Generate string xH = SHA256(salt|password) 2. Convert xH to integer x 3. Generate S = (B-k*g^x)^(a+u*x) mod N 4. Generate K = SHA256(S) """ xH = rypto.sha256(str(client_recv['s'])+session.p) client_x = int(xH,16) client_S = rypto.mod_exp((client_recv['B']-session.k*(rypto.mod_exp(session.g,client_x,session.n))),(a+client_u*client_x),session.n) client_K = rypto.sha256(str(client_S)) #print client_K """ Server: 1. Generate S = (A*v^u)^b mod N 2. Generate K = SHA256(S) """ session.generate_k() #print session.server_K """ Validate """ user_try = rypto.sha256(client_K+str(client_recv['s'])) session.validate(user_try)