标签:write new 路径 文件名 lag __file__ class rip 成功
一:程序主逻辑
#codeing:UTF-8 #__author__:Administrator #date:2018/4/27/027 0:23 import time import os import sys base_dir = os.path.dirname((__file__)) #路径的加入 # print(base_dir) sys.path.append(base_dir)#路径的加入 import wiener def encode(N,e): file_name = input("请输入数据保存的文件名:>如:RSA加密1.txt>>") file = open(file_name, "w", encoding="utf-8") need_code_data = input("请输入需要加密的数据>>>").strip() need_code_data = int(need_code_data) print("需要加密的数据为:",need_code_data) # need_code_data_list = [] # encode_data_list = [] # col_num = int(len(need_code_data)/8) +1 # for i in range(col_num): # if i < col_num -1: # deal_data = need_code_data[i*8:i*8+8] # else: # deal_data = need_code_data[i*8:] # # print(int(deal_data)) # need_code_data_list.append(int(deal_data)) # print(need_code_data_list) # print(bin(e)) e_of_b = str(bin(e))[2:] # print(e_of_b) save_data = 1 for x in e_of_b: g = need_code_data if x == "1": save_data = (save_data*g)%N else: save_data = (save_data ** 2)%N # print(save_data) file.write(str(save_data)) file.close() return save_data # for i in need_code_data_list: #分组计算 # save_data = 1#pow(i,e) # # cal_num = 2 # print(i,type(i)) # for x in e_of_b: # if x == "1": # save_data = (save_data*i)%N # else: # save_data = (save_data ** 2) % N # print(save_data) # encode_data_list.append(save_data) # print(encode_data_list) def calculate_data(N,e): file_name = input("请输入数据保存的文件名:>如:连分数展开1.txt>>") file = open(file_name,"w",encoding="utf-8") # N = 28562942440499 # e = 7502876735617 a_list = wiener.calculate_a(e / N) # print(a_list) A_list = wiener.calcul_A(a_list, len(a_list)) # print(A_list) for i in range(len(a_list)): mas = "a"+str(i)+"="+str(a_list[i])+" A"+str(i)+"="+str(A_list[i])+"="+str(A_list[i][0])+"/"+str(A_list[i][1])+"\n" file.write(mas) file.close() def mi_key_hack(N,e): file_name = input("请输入数据保存的文件名:>如:RSA攻击1.txt>>") file = open(file_name, "w", encoding="utf-8") a_list = wiener.calculate_a(e / N) A_list = wiener.calcul_A(a_list, len(a_list)) d_use_data = wiener.check_N(e, N, A_list) finall_d_data = wiener.finally_check_d(d_use_data, e, N) for i in range(len(finall_d_data)): mas = "d=" + str(finall_d_data[i][0]) + " p=" + str(finall_d_data[i][1]) + " q=" + str( finall_d_data[i][2]) + "PHI=" + str(finall_d_data[i][3]) + "\n" file.write(mas) file.close() # print(finall_d_data) def write_N_e(N,e): print("之前的N,e分别为:>>>>",N,e) input_data = input("请重新输入N、e,用空格隔开>>>>").split(" ") # print(input_data) N,e = int(input_data[0]),int(input_data[1]) # print(N,e) return (N,e) def exit_fun(N,e): exit() def main(N,e): choice_dict = {1: encode, 2: calculate_data, 3: mi_key_hack ,4:write_N_e,5:exit_fun} while True: masg = """ =====功能菜单:============================ === 1:数据加密 === === 2:连分式展开 === === 3:密钥的攻击 === === 4:重置N,e === === 5:退出函数 ========================================== """ print(masg) choice = int(input("请输入你选择的功能号:>>>>")) N_and_e = choice_dict[choice](N,e) if choice == 4: N,e = N_and_e[0],N_and_e[1] print("更改后的N、e为:",N,e) print("执行结束,即将返回....") time.sleep(1) if __name__ == "__main__": N= 28562942440499 e = 7502876735617 print("初始公钥为N,e :",N,e) print("提示:进入菜单后可以更改公钥") print("提示:进入菜单......") # N=160523347 #公钥1 # e=60728973 # N=4254607303 #公钥2 # e=3231248239 # time.sleep(2) main(N,e)
连分数攻击主程序 提示1024bit的攻击未成功
#codeing:UTF-8 #__author__:Administrator #date:2018/4/26/026 10:06 def calculate_a(calculate_num): list_a = [] deal_num = calculate_num for i in range(300): moment_a = deal_num % 1 append_a = deal_num - moment_a list_a.append(append_a) # print(deal_num,append_a) deal_num = 1 /(deal_num-append_a) return list_a def calcul_A(list_a,length_a): list_A = [] for i in range(length_a): deal_list = list_a[:i+1] deal_num =len(deal_list) flag = True while deal_num > 1: if flag == True: p = deal_list[deal_num- 1] * deal_list[deal_num-2] + 1 q = deal_list[deal_num- 1] append_tuple_pq = (int(p),int(q)) # print(append_tuple_pq) deal_num -=2 flag = False else: new_q = append_tuple_pq[0] # p 3 new_p = append_tuple_pq[1] # q 2 p = deal_list[deal_num - 1] * new_q + new_p q = new_q append_tuple_pq = (int(p),int(q)) # print(append_tuple_pq) deal_num -= 1 else: if flag == True and deal_num ==1 : q = deal_list[deal_num-1] p = 1 append_tuple_pq = (int(p),int(q)) # print(append_tuple_pq) else : new_q = append_tuple_pq[0] # p 3 new_p = append_tuple_pq[1] # q 2 p = deal_list[deal_num - 1] * new_q +new_p q = new_q append_tuple_pq = (int(p), int(q)) list_A.append(append_tuple_pq) return list_A def check_N(e,N,list_A): #过滤可用的k/d useful_data = [] for i in list_A: k = int(i[0]) d = int(i[1]) if (e*d-1)%k==0 and d !=1 and d<N and d!=0: useful_data.append(i) return useful_data def finally_check_d(d_list,e,N): result_data = [] for i in d_list: k = int(i[0]) d = int(i[1]) FIN = int((e * i[1] - 1) / i[0]) # print(F_and_N) p_and_q = N - FIN +1 # print(p_and_q) for x in range(int(p_and_q/2),p_and_q): if x * (p_and_q - x) == N: result_data.append((d ,x,p_and_q-x,FIN)) return result_data # N = 10088821 # e=3905745 if __name__ == "__main__": # N=160523347 #公钥1 # e=60728973 # N=4254607303 #公钥2 # e=3231248239 N = 28562942440499 #公钥3 e = 7502876735617 # 公钥4 # N=67413492557336846539848944654588427510674136710336496477536280434149753427064025563334904527604187742536389581868217454855192496861370028546941279678384891963660931858219195511402645528868253750722549516549902488662436218392592253709764220778391139803580637991034641103694298696184202191875835150171491066691 # e=55668825058376388576113884040385292667510043490011858212473465931416310111625430534836493367797004997544370288958836993585093703688885638705916296545617964378468562419875481220082488169256684043454124045215432169627429196661617858371465847627303530906317588472913047600188255485102837752107180962349209908663 # # 相关数据 # [(27763604.0, 33620999.0)] # PHI 67413492557336846539848944654588427510674136710336496477536280434149753427062861174765678995902826007920042704951772509122046163305406233434817786489802796641662960034324556978613332350467285294794906107031877163314306505878423580552717416452810213936256441506719861839980217226929893614313258623609084051456 a_list = calculate_a(e / N) # print(a_list) A_list = calcul_A(a_list, len(a_list)) print(A_list) print(len(A_list)) d_use_data = check_N(e, N, A_list) print(d_use_data) finall_d_data = finally_check_d(d_use_data, e, N) print(finall_d_data)
标签:write new 路径 文件名 lag __file__ class rip 成功
原文地址:https://www.cnblogs.com/duke77--null/p/9026458.html