def check_CP(): data = loadCP() if data['hesh'] == '1': H = SHA.sha256(data['message']) elif data['hesh'] == '2': H = SHA.sha512(data['message']) elif data['hesh'] == '3': H = stibog.Main(data['message'], 256) elif data['hesh'] == '4': H = stibog.Main(data['message'], 512) R_test = (int( fun.obrat(pow(data['U'] * data['L'], data['L'], data['p']), data['p'])) * pow(data['a'], data['S'], data['p'])) % data['p'] kon = data['message'] + str(R_test) + str(data['U']) if data['hesh'] == '1': E_test = SHA.sha256(kon) elif data['hesh'] == '2': E_test = SHA.sha512(kon) elif data['hesh'] == '3': E_test = Stibog.Main(kon, 256) elif data['hesh'] == '4': E_test = Stibog.Main(kon, 512) if data['E'] == E_test: print(True) else: print(False)
def FBI_OPEN_UP(m=512): PKS=list() with open("publick.txt","r") as priv: Bi=priv.read().split("\n") n=Bi[1] Bi=Bi[0].split() with open("Cp.txt","r") as priv: s=priv.read().split() t=s[1] s=s[0] with open("Cp_Document.txt","r") as Cp_D: for i in Cp_D: PKS.append(i) M=PKS[13] w=int(t)*int(t) j=1 i=0 k=0 z=Pim(Bi,s,n) w=int((w*z)%int(n)) print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") print(w) H=SHA.sha512(str(M)+str(w)) H = str(convert_base(H, 2, 16)) while len(H)!=512: H="0"+H print(len(s)) print(len(H)) print("s = "+str(s)) print("s'= "+str(H)) if s==H: print(True) else: print(False)
def FBI_OPEN_UP(): cp_list = [] with open("ЦП.txt", "r") as prifile: gamma, delta = (prifile.read().split()) with open("Cp_Document.txt", "r") as pubfile: cp_list = pubfile.read().split('\n') hm = cp_list[1] alpha, beta, p = cp_list[3].split() mes = cp_list[13] alpha, beta, p, gamma, delta = int(alpha), int(beta), int(p), int( gamma), int(delta) if PKCS_7[1] == hm: hesh = int(SHA.sha256(mes), base=16) elif PKCS_7[1] == hm: hesh = int(SHA.sha512(mes), base=16) elif PKCS_7[1] == hm: hesh = int(STR.Stribog(mes, 256), base=16) elif PKCS_7[1] == hm: hesh = int(STR.Stribog(mes, 512), base=16) print( pow(beta, gamma, p) * pow(gamma, delta, p) % p, '==', pow(alpha, hesh, p)) if pow(beta, gamma, p) * pow(gamma, delta, p) % p == pow(alpha, hesh, p): print("true") else: print("false")
def hesh_message(message: str, type_hesh: int): if type_hesh == 1: H = SHA.sha256(message) elif type_hesh == 2: H = SHA.sha512(message) elif type_hesh == 3: H = Stribog.Stribog(message, 256) elif type_hesh == 4: H = Stribog.Stribog(message, 512) else: H = '0' return H
def hesh_choose(message, type): if type == '1': H = SHA.sha256(message) return H elif type == '2': H = SHA.sha512(message) return H elif type == '3': H = Stibog.Main(message, 256) return H elif type == '4': H = Stibog.Main(message, 512) return H else: print('Error') return 0
def Cp(Bi,n,Ai,M): n=int(n) r=random.randint(1,n) u=pow(r,2,n) H=SHA.sha512(str(M)+str(u)) s=convert_base(H, 2, 16) while len(s)!=512: s="0"+s z=1 print("s = " + str(len(s))) print(u) z=Pim(Ai,s,n) t=int((r*z)) with open("Cp.txt","w") as cp: strin=str(s)+" "+str(t) cp.write(strin) return s
def LEAD_3(p, q, a, R_l: list, M, U, type): T = random.randint(2, q - 1) R_lead = pow(a, T, p) R = 1 for i in range(len(R_l)): R = R * R_l[i] R = R_lead * (R % p) E = M + str(R) + str(U) if type == '1': E = SHA.sha256(E) return E, T # Лидер отправляет каждому пользователю E, лидер T не передаёт, но тут передаёт elif type == '2': E = SHA.sha512(E) return E, T elif type == '3': E = Stibog.Main(E, 256) return E, T elif type == '4': E = Stibog.Main(E, 512) return E, T
def n_iteration(M, method): Hm = M n = randint(2, 100) HM_list = list() if method == 1: for i in range(1, n): Hm = SHA.sha512(Hm) HM_list.append(Hm) elif method == 2: for i in range(1, n): Hm = STR(Hm, 512) HM_list.append(Hm) with open("HnList.txt", "w") as txt: strin = "" for i in HM_list: strin = strin + i + "\n" if method == 1: strin = strin + "SHA 0" elif method == 2: strin = strin + "STR 0" txt.write(strin)
def autoriz(KeyWord): with open("HnList.txt", "r") as txt: hash_list = txt.read().split("\n") type = hash_list[-1].split() type_hash = type[0] num_enters = int(type[1]) if num_enters >= len(hash_list) - 5: choose = input( "Need generate new password. Do it now?\nYes or No?\n>>") if "Yes" == choose: n_iteration() elif "No" == choose: pass Hm = KeyWord if type_hash == "SHA": for i in range(0, int(num_enters) + 1): Hm = SHA.sha512(Hm) #if num_enters==0 or num_enters==1: # Hm = SHA.sha512(Hm) elif type_hash == "STR": for i in range(1, int(num_enters) + 1): Hm = STR(Hm, 512) #if num_enters==0 or num_enters==1: # Hm = SHA.sha512(Hm) if Hm == hash_list[num_enters]: print("Success") with open("HnList.txt", "w") as txt: strin = "" for i in hash_list: strin = strin + i + "\n" if type_hash == "SHA": strin = strin + "SHA " + str(num_enters + 1) elif type_hash == "STR": strin = strin + "STR " + str(num_enters + 1) txt.write(strin) return Hm else: print("bad try") return False
def FBI_OPEN_UP(): flag = True PKCS_7 = "" sor = "" while flag: task = input("Pleas enter file with cp with .txt\n>> ") try: with open(task, "r") as cp_file: PKCS_7 = cp_file.read().split("\n") with open("ЦП.txt", "r") as cp_file: sor = cp_file.read() flag = False break except FileNotFoundError: print("cant find file, please try new name write\n") mes = PKCS_7[13] s = int(PKCS_7[10], 16) e = PKCS_7[3].split() n = e[1] e = e[0] if PKCS_7[1] == "SHA256": hm = SHA.sha256(mes) elif PKCS_7[1] == "SHA512": hm = SHA.sha512(mes) elif PKCS_7[1] == "STR256": hm = STR.Stribog(mes, 256) elif PKCS_7[1] == "STR512": hm = STR.Stribog(mes, 512) hmshtrix = pow(s, int(e), int(n)) print("\nResult - " + "None\n" + "Algorithm hash - " + PKCS_7[2] + "\nAlgoritm CP - " + PKCS_7[9] + "\nAuthor - " + PKCS_7[6] + "\nTime create CP - " + PKCS_7[11] + "\n") print("ЦП = " + str(sor)) print("Cp_s = " + str(s)) # print("NC = "+str(hex(int(hmshtrix)))) print("NC = " + str(int(hmshtrix)))
n = Bi[1] Bi = Bi[0].split() PKCS_7[3] = str(n) except FileNotFoundError: print("Need generate keys. Please, do it and try later\n") break print("\nstart cp\priv") cp = Cp(Bi,n,Ai,mes) PKCS_7[6] = input("Enter you Name and Fname pleas ( Name Fname)\n>> ") SignVal = hex(int(cp)) PKCS_7[10] = SignVal[2:] PKCS_7[11] = date.today() if PKCS_7[1] == "SHA256": PKCS_7[12] = SHA.sha256(mes + cp) elif PKCS_7[1] == "SHA512": PKCS_7[12] = SHA.sha512(mes + cp) elif PKCS_7[1] == "STR256": PKCS_7[12] = STR.Stribog(mes + cp, 256) elif PKCS_7[1] == "STR512": PKCS_7[12] = STR.Stribog(mes + cp, 512) with open("Cp_Document.txt", "w") as doc: for i in PKCS_7: stri = "" stri = stri + str(i) + "\n" doc.write(stri) doc.write(mes) elif point == "4": FBI_OPEN_UP() elif point == "5": flag = False break
def new_g(w: str, p: int): g = SHA.sha512(w) g = int(g, 16) g = g % p return g