CMS_Django_Backend/apps/api/common.py
jayhgq b28732d3ef 1.调整路由命名
2.api中添加Caesar算法
2024-09-07 00:03:37 +08:00

51 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class CaesarCypherClass:
"""
恺撒密码提供以恺撒密码方法进行加密及解密的方法加密方法使用CaesarEncode()函数解密方法使用CaesarDecode()函数
"""
def __init__(self, *args, **kwargs):
pass
@staticmethod
def caesar_encode(s):
"""
恺撒密码加密方法,需要提供需要加密的明文。
"""
s_encode = ''
for c in s:
if 'a' <= c <= 'z':
s_encode += chr(ord('a') + (ord(c) - ord('a') + 3) % 26)
elif 'A' <= c <= 'Z':
s_encode += chr(ord('A') + (ord(c) - ord('A') + 3) % 26)
elif 0x4E00 <= ord(c) <= 0x9FA5:
s_encode += chr(ord(c) + 3)
elif '0' <= c <= '9':
s_encode += chr(ord('0') + (ord(c) - ord('0') + 3) % 10)
else:
s_encode += c
return s_encode
@staticmethod
def caesar_decode(s):
"""
恺撒密码解密方法,需要提供需要解密的密文。
"""
s_decode = ''
for c in s:
if 'a' <= c <= 'z':
s_decode += chr(ord('a') + (ord(c) - ord('a') - 3) % 26)
elif 'A' <= c <= 'Z':
s_decode += chr(ord('A') + (ord(c) - ord('A') - 3) % 26)
elif 0x4E00 <= ord(c) <= 0x9FA5:
s_decode += chr(ord(c) - 3)
elif '0' <= c <= '9':
s_decode += chr(ord('0') + (ord(c) - ord('0') - 3) % 10)
else:
s_decode += c
return s_decode
class Base64CypherClass:
"""
Base64的加解密算法最简单的加密方式可加密短的文字、小图片、小文件图片文件大小不宜超过10M
"""