zoukankan      html  css  js  c++  java
  • 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())
    View Code

    使用算法参考: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())

  • 相关阅读:
    codesmith 模板
    asp.net 开发知识小结【转】
    一种页面数据错误输入提示方法 【转】
    vs2008 调试js
    安装vs2008出现的问题
    Godaddy主机导入MSSQL数据库教程
    asp.net用url重写URLReWriter实现任意二级域名(续)
    Visio2007 与Microsoft Studio 2008不兼容
    dos 必知八项命令
    Godaddy空间设置说明
  • 原文地址:https://www.cnblogs.com/wind-man/p/12980846.html
Copyright © 2011-2022 走看看