def verify_key_candidate_list(key1_list, key2, pt2, ct2): flag = False # 후보 없음 for key1 in key1_list: key1_state = int_list_lib.int2list(key1) mid2 = TC20_Enc_lib.TC20_Enc(pt2, key1_state) ct2_calc = TC20_Enc_lib.TC20_Enc(mid2, key2) if ct2_calc == ct2: print('\nkey1 = ', key1_state, 'key2 = ',key2) flag = True return flag
def make_enc_dic(pt): dic = {} print('Making Encryption Dictionary', end='') N = 1<<24 # 24bit 키 , N = 2^24 for idx in range(0,N): key = int_list_lib.int2list(idx) mid = TC20_Enc_lib.TC20_Enc(pt, key) int_mid = int_list_lib.list2int(mid) int_key = int_list_lib.list2int(key) #int_key = idx if int_mid in dic: # int_mid : 전에 나온적이 있는 값 dic[int_mid].append(int_key) else: # 처음 나온 int_mid dic[int_mid] = [int_key] if (idx%(1<<18)) == 0: print('.', end='') return dic
def one_tmto_table_search(ct, P0, m, t, ell): key_candid_list = [] file_name = 'TMTO_Tables/TMTO-' + str(ell) + '.dic' tmto_dic = pickle_file.load_var_from_file(file_name) Xj = R(ct) current_j = t for idx in range(0, t): Xj_int = int_list_lib.list2int(Xj) if Xj_int in tmto_dic: #Xj가 EP중에 있는지? SP = int_list_lib.int2list(tmto_dic[Xj_int]) # EP --> SP key_guess = chain_EP(SP, P0, current_j-1) key_candid_list.append(key_guess) new_ct = TC20_Enc_lib.TC20_Enc(P0, Xj) Xj = R(new_ct) current_j = current_j - 1 return key_candid_list
import TC20_Enc_lib, TC20_Dec_lib import int_list_lib #--------------------------- # 준비 : 평문과 키로 암호문 구하기 # given_pt = [65, 66, 67, 68] # key = [0, 1, 2, 3] # ct = TC20_Enc_lib.TC20_Enc(given_pt, key) # print(ct) #---------------------------- # 평문과 암호문을 이용해 키 추측하기 # 전수조사 공격 given_pt = [65, 66, 67, 68] given_ct = [246, 31, 30, 133] KeySize = 1 << 22 # 키 20bit만 전수조사 (최대 : 32bit) print('plain text :', given_pt) print('cipher text :', given_ct) print('key searching', end='') for idx in range(0, KeySize): guess_key = int_list_lib.int2list(idx) pt = TC20_Dec_lib.TC20_Dec(given_ct, guess_key) if pt == given_pt: print('\nkey = ', guess_key) break if (idx % (1 << 15)) == 0: print('.', end='')
# 공격조건 : (pt1, ct1), (pt2, ct2) 로부터 key1, key2 찾기 #========================================================== pt1 = [1, 2, 3, 4] ct1 = [156, 101, 28, 237] pt2 = [4, 3, 2, 1] ct2 = [59, 108, 107, 113] #============================================ # 사전 만들기 mid_dic = make_enc_dic(pt1) # 사전 파일로 저장하기 pickle_file.save_var_to_file(mid_dic, 'TC20/TC20EncDic.p') # 사전을 파일에서 가져오기 mid_dic = pickle_file.load_var_from_file('TC20/TC20EncDic.p') print('Dictionary loaded') #============================================ N = 1<<22 # key2의 크기 2^24 for idx in range(0, N): key2 = int_list_lib.int2list(idx) mid1 = TC20_Dec_lib.TC20_Dec(ct1, key2) # ct1을 key2로 복호화 int_mid1 = int_list_lib.list2int(mid1) if int_mid1 in mid_dic: list_key1_candidate = mid_dic[int_mid1] if len(list_key1_candidate)>0: verify_key_candidate_list(list_key1_candidate, key2, pt2, ct2) if(idx%(1<<18)) == 0: print('.',end='') print('\n key search completed !!!')