crypto的一些脚本

base64

 1 import base64
 2 
 3 
 4 def encode(s):
 5     bytes_s = s.encode(encoding='utf-8')
 6     b64en_s = base64.b64encode(bytes_s)
 7     print("加密后:{}".format(b64en_s))
 8     return
 9 
10 
11 def decode(s):
12     b64de_s = base64.b64decode(s).decode('utf-8')
13     print("解密后:{}".format(b64de_s))
14     return
15 
16 
17 if __name__ == "__main__":
18     data = input('请输入字符串:')
19     print("选项:\n1、加密        2、解密")
20     op = int(input('请输入你的选择:'))
21     if op == 1:
22         encode(data)
23     else:
24         decode(data)

凯撒密码

 1 def Caesar(s):
 2     print('初始的字符串:{}'.format(s))
 3     if 'A' <= s[0] <= 'Z':
 4         for i in range(1, 27):
 5             offset_s = ''
 6             for j in range(len(s)):
 7                 tmp = (ord(s[j]) + i)
 8                 if tmp > ord('Z'):
 9                     tmp -= 26
10                 offset_s += chr(tmp)
11             print('偏移值为{}的字符串:{}'.format(i, offset_s))
12     else:
13         for i in range(1, 27):
14             offset_s = ''
15             for j in range(len(s)):
16                 tmp = (ord(s[j]) + i)
17                 if tmp > ord('z'):
18                     tmp -= 26
19                 offset_s += chr(tmp)
20             print('偏移值为{}的字符串:{}'.format(i, offset_s))
21     return
22 
23 
24 if __name__ == '__main__':
25     data = input('请输入字符串:')
26     Caesar(data)

md5

 1 # 由于MD5模块在python3中被移除
 2 # 在python3中使用hashlib模块进行md5操作
 3 
 4 import hashlib
 5 
 6 # 待加密信息
 7 str = input('请输入要加密的字符串:')
 8 
 9 # 创建md5对象
10 m = hashlib.md5()
11 
12 # Tips
13 # 此处必须encode
14 # 若写法为m.update(str)  报错为: Unicode-objects must be encoded before hashing
15 # 因为python3里默认的str是unicode
16 # 或者 b = bytes(str, encoding='utf-8'),作用相同,都是encode为bytes
17 b = str.encode(encoding='utf-8')
18 m.update(b)
19 str_md5 = m.hexdigest()
20 # md5在线解密网站:https://cmd5.com/
21 
22 print('MD5加密前为 :' + str)
23 print('MD5加密后为 :' + str_md5)
24 
25 # 另一种写法:b‘’前缀代表的就是bytes
26 # str_md5 = hashlib.md5(b'this is a md5 test.').hexdigest()
27 # print('MD5加密后为 :' + str_md5)

莫斯密码(需要分隔符)

 1 # 没有分割符的话,可以试试在线网站:http://zhongguosou.com/zonghe/moErSiCodeConverter.aspx
 2 MorseList = {
 3     ".-": "A", "-...": "B", "-.-.": "C", "-..": "D", ".": "E", "..-.": "F", "--.": "G",
 4     "....": "H", "..": "I", ".---": "J", "-.-": "K", ".-..": "L", "--": "M", "-.": "N",
 5     "---": "O", ".--.": "P", "--.-": "Q", ".-.": "R", "...": "S", "-": "T",
 6     "..-": "U", "...-": "V", ".--": "W", "-..-": "X", "-.--": "Y", "--..": "Z",
 7 
 8     "-----": "0", ".----": "1", "..---": "2", "...--": "3", "....-": "4",
 9     ".....": "5", "-....": "6", "--...": "7", "---..": "8", "----.": "9",
10 
11     ".-.-.-": ".", "---...": ":", "--..--": ",", "-.-.-.": ";", "..--..": "?",
12     "-...-": "=", ".----.": "'", "-..-.": "/", "-.-.--": "!", "-....-": "-",
13     "..--.-": "_", ".-..-.": '"', "-.--.": "(", "-.--.-": ")", "...-..-": "$",
14     ".-...": "&", ".--.-.": "@", ".-.-.": "+",
15 }
16 
17 
18 def morse(string: object, sign: object) -> object:
19     # 分割,字符串string,分割标识符sign
20     lists = string.split(sign)
21     print('解密后:',end='')
22     for code in lists:
23         print(MorseList.get(code), end="")
24 
25 
26 if __name__ == '__main__':
27     data = input('请输入带分隔符的字符串:')
28     morse(data,'/')

URL编码

 1 import urllib.parse
 2 
 3 
 4 def URL_en(s):
 5     print("URL编码:{}".format(urllib.parse.quote(s)))
 6     return
 7 
 8 
 9 def URL_de(s):
10     print('URL解码:{}'.format(urllib.parse.unquote(s)))
11     return
12 
13 
14 def menu(data_s):
15     op = int(input('1、编码      2、解码\n请输入选项:'))
16     if op == 1:
17         URL_en(data_s)
18     else:
19         URL_de(data_s)
20     return
21 
22 
23 if __name__ == '__main__':
24     data = input('请输入数据:')
25     menu(data)

 

posted @ 2022-03-09 17:22  vi0let  阅读(53)  评论(0编辑  收藏  举报