python 数字字典加密非汉字
Posted Wind_LPH
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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())
以上是关于python 数字字典加密非汉字的主要内容,如果未能解决你的问题,请参考以下文章
python数字转汉字 青少年编程电子学会python编程等级考试二级真题解析2020年3月
python数字转汉字 青少年编程电子学会python编程等级考试二级真题解析2020年3月
python的文字和unicode/ascll 相互转换函数,和简单的加密解密。。。