def testEncriptDecript(self): plainText = 'idjpi23j023uc0j1-0i-soxl=kixq[wkz=21ks[qqwdqwd' password = '******' encodedText = aes.encode(password, plainText) decodedText = aes.decode(password, encodedText) self.assertEqual(decodedText, plainText)
def encode_and_save_data(data_list, password): """ Encodes passed data and saves it""" with open(PASSWORD_BASE_PATH, 'wb') as data_file: stringified_data = json.dumps(data_list) encoded_data_string = aes.encode(password, stringified_data) encoded_data_bytes = encoded_data_string.encode() data_file.write(encoded_data_bytes)
def encode(k, p): p_len = len(p) block = 16 str_tmp = '{:0>2x}'.format(p[0]) c_str = '' for i in range(1, p_len): if i % block == 0: c_str += aes.encode(str_tmp, k) str_tmp = '{:0>2x}'.format(p[i]) else: str_tmp = str_tmp + '{:0>2x}'.format(p[i]) delt = block - p_len % block delt_str = '{:0>2x}'.format(delt) for i in range(delt): str_tmp += delt_str c_str += aes.encode(str_tmp, k) c_len = len(c_str) return bytes.fromhex(c_str)
def encode(k,p,IV): p_len=len(p) p_str='' for i in range(p_len): p_str+='{:0>2x}'.format(p[i]) tmp = IV c_str='' index = 0 p_len=len(p_str) while True: if index+32>=p_len: delta = p_len-index tmp = aes.encode(tmp,k) c_str += str_xor(p_str[index:index+delta],tmp[0:delta],delta) break tmp = aes.encode(tmp,k) c_str += str_xor(p_str[index:index+32],tmp,32) index +=32 return bytes.fromhex(c_str)
def ui_de(self): c = self.cInput.get().strip() k = self.kInput.get().strip() self.pInput.delete(0, END) lc = len(c) lk = len(k) if lp != 32: tkinter.messagebox.showwarning('Waring', 'The length of c is wrong!') else: if lk == 32: p = aes.decode(c, k) self.pInput.insert(10, str(p)) elif lk == 48: p = aes.encode(p, k, 6, 12) self.pInput.insert(10, str(p)) elif lk == 64: p = aes.encode(p, k, 8, 14) self.pInput.insert(10, str(p)) else: tkinter.messagebox.showwarning('Waring', 'The length of key is wrong!')
def encode(k, p, IV, s=8): p_len = len(p) p_str = '' s = s // 4 for i in range(p_len): p_str += '{:0>2x}'.format(p[i]) p_len = len(p_str) c = '' index = 0 tmp_reg = aes.encode(IV, k) while True: if (p_len - index) <= s: s = p_len - index s_tmp = tmp_reg[0:s] c_tmp = str_xor(p_str[index:index + s], s_tmp, s) c = c + c_tmp break s_tmp = tmp_reg[0:s] c_tmp = str_xor(p_str[index:index + s], s_tmp, s) c = c + c_tmp index += s tmp_reg = tmp_reg[s:] + c_tmp tmp_reg = aes.encode(tmp_reg, k) return bytes.fromhex(c)
def encode(k, p, IV): p_len = len(p) block = 16 p_str = '' for i in range(p_len): p_str += '{:0>2x}'.format(p[i]) nr = p_len // block + 1 #padding delta = block - p_len % block del_str = '{:0>2x}'.format(delta) for i in range(delta): p_str += del_str c_str = '' c_nr = IV for i in range(nr): c_nr = aes.encode(str_xor_16(c_nr, p_str[i * 32:i * 32 + 32]), k) c_str += c_nr return bytes.fromhex(c_str)