python 數字字典加密非漢字

2022-06-29 21:09:10 字數 3980 閱讀 1324

簡單版

特點是,無需借助任何第三方庫。

#

加密和解密檔案

#欄位內容以 ; 分割

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 ={}

defgetnumthrowchar(self,cha):

i =0

for t in

self.list:

if(t.__eq__

(cha)):

return

i i = i+1

return

0

defencryptchr(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

defencrypt(self):

rs = ''

#加密,list對應字母,小寫字母在list上增加26

for t in

self.line:

rt =self.encryptchr(t)

rs = rs.__add__

(rt)

rs = rs.__add__("

;=;"

)

return

rs

defdecryptchr(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

defdecrypt(self):

rs = ''

temp = self.line.replace('

@_@',"

").split("

;=;"

)

for t in

temp:

ts =self.decryptchr(t)

rs = rs.__add__

(ts)

return

rsif

__name__=="

__main__":

aa = crypt("

經;=;@;=;27;=;30;=;41;=;*;=;b;=;@_@;=;a;=;b;=;c;=;b;=;曾;=;的;=;夢;=;")

print(aa.decrypt())

view code

使用演算法參考:

# 加密和解密檔案# 字段內容以 ; 分割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())

漢字字典樹

字典樹的概念我就不說了,不過大多題目都是英文的字典樹,我就閒的蛋疼去寫了中文的字典樹,實現起來也挺簡單的。include include include include include using namespace std 字典樹,根節點為1 struct node tree 1005 表示字典樹...

Oracle PL SQL 過濾非數字字元

背景 給同事做了個報表,使用時從excel 網頁等向報表中拷貝查詢條件 局編 使用者反映查不出資料 調查後發現使用者填的局編帶有空格 隨手加了trim 又有查不出資料的情況 帶有全形空格 用replace替換全形空格 還有查不到,調查發現可能混有換行 excel中神秘的空白符等。處理 第一反應是讓使...

JS 遮蔽非數字字元的輸入

為了避免無效資料的另一種方法是在使用者錄入資料時 對無效輸入進行遮蔽,例如在輸入銀行卡號時,要求使用者必須輸入數字,當使用者輸入非數字字元是,給出提示。下面給出 以上 中 is number 函式 用於遮蔽非數字字元的輸入。函式中,通過event 物件 的屬性 得到按下 鍵 的 unicode 編碼...