python 数字字典加密非汉字
简单版
特点是,无需借助任何第三方库。
# 加密和解密文件 # 字段内容以 ; 分割 class Crypt(): def __init__(self,line): self.line = line self.list=[‘‘,‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘,‘G‘,‘H‘,‘I‘,‘J‘,‘K‘,‘L‘,‘M‘,‘N‘,‘O‘,‘P‘,‘Q‘,‘R‘,‘S‘,‘T‘,‘U‘,‘V‘,‘W‘,‘X‘,‘Y‘,‘Z‘] self.de_dict = {} def getNumThrowChar(self,cha): i = 0 for t in self.list: if(t.__eq__(cha)): return i i = i+1 return 0 def encryptChr(self,t): if(t.isdigit()): digt = int(t) return self.list[digt] elif(t.islower()): rs_num = self.getNumThrowChar(t.upper())+26 return str(rs_num) elif(t.isupper()): rs_num = self.getNumThrowChar(t.upper()) return rs_num elif(t.isspace()): # 空白字符 return ‘@‘ else: return t def encrypt(self): rs = ‘‘ # 加密,list对应字母,小写字母在list上增加26 for t in self.line: rt = self.encryptChr(t) rs = rs.__add__(rt) rs = rs.__add__(";=;") return rs def decryptChr(self,t): if(t.isdigit()): t = int(t) if(t>26): return self.list[t-26].lower() else: return self.list[t] elif(t.isupper()): rs_num = self.getNumThrowChar(t.upper()) return str(rs_num) else: return t def decrypt(self): rs = ‘‘ temp = self.line.replace(‘@‘," ").split(";=;") for t in temp: ts = self.decryptChr(t) rs = rs.__add__(ts) return rs if __name__=="__main__": aa = Crypt("经;=;@;=;27;=;30;=;41;=;*;=;B;=;@;=;A;=;B;=;C;=;B;=;曾;=;的;=;梦;=;") print(aa.decrypt())
使用算法参考:https://www.jianshu.com/p/4ba20afacce2
# 加密和解密文件# 字段内容以 ; 分割class Crypt(): def __init__(self,line): self.line = line self.list=[‘‘,‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘,‘G‘,‘H‘,‘I‘,‘J‘,‘K‘,‘L‘,‘M‘,‘N‘,‘O‘,‘P‘,‘Q‘,‘R‘,‘S‘,‘T‘,‘U‘,‘V‘,‘W‘,‘X‘,‘Y‘,‘Z‘] self.de_dict = {} def getNumThrowChar(self,cha): i = 0 for t in self.list: if(t.__eq__(cha)): return i i = i+1 return 0 def encryptChr(self,t): if(t.isdigit()): digt = int(t) return self.list[digt] elif(t.islower()): rs_num = self.getNumThrowChar(t.upper())+26 return str(rs_num) elif(t.isupper()): rs_num = self.getNumThrowChar(t.upper()) return rs_num elif(t.isspace()): # 空白字符 return ‘@‘ else: return t def encrypt(self): rs = ‘‘ # 加密,list对应字母,小写字母在list上增加26 for t in self.line: rt = self.encryptChr(t) rs = rs.__add__(rt) rs = rs.__add__(";=;") return rs
def decryptChr(self,t): if(t.isdigit()): t = int(t) if(t>26): return self.list[t-26].lower() else: return self.list[t] elif(t.isupper()): rs_num = self.getNumThrowChar(t.upper()) return str(rs_num) else: return t
def decrypt(self): rs = ‘‘ temp = self.line.replace(‘@‘," ").split(";=;") for t in temp: ts = self.decryptChr(t) rs = rs.__add__(ts) return rs
if __name__=="__main__": aa = Crypt("经;=;@;=;27;=;30;=;41;=;*;=;B;=;@;=;A;=;B;=;C;=;B;=;曾;=;的;=;梦;=;") print(aa.decrypt())