kinit/kinit-api/utils/aes_crypto.py
ktianc 7fbdcb3b0f 版本升级
1. 新增:微信小程序端新增微信手机号登录功能(必须为企业认证小程序)
2. 新增:加入动态更新常见问题
3. 新增:新增小程序分享功能
4. 新增:小程序新增第一次登录需要修改密码
5. 新增:新增接口权限控制
6. 新增:用户新增is_staff用来判断是否为工作人员
7. 新增:软删除新增is_delete字段来判断,delete_datetime当前主要来记录时间
8. 更新:部分接口删除功能已更新,需要试用软删除的才会试用软删除
9. 更新:更新系统配置缓存功能
10. 更新:接口认证依赖项更新
11. 更新:获取系统基础配置信息与用户协议与隐私协议更新
12. 优化:优化接口与数据库操作
2023-02-27 17:28:27 +08:00

54 lines
1.6 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.

import base64
from Crypto.Cipher import AES # 安装pip install pycryptodome
# 密钥key, 密斯偏移量iv CBC模式加密
# base64 详解https://cloud.tencent.com/developer/article/1099008
_key = '0CoJUm6Qywm6ts68' # 自己密钥
def aes_encrypt(data: str):
"""
加密
"""
vi = '0102030405060708'
pad = lambda s: s + (16 - len(s) % 16) * chr(16 - len(s) % 16)
data = pad(data)
# 字符串补位
cipher = AES.new(_key.encode('utf8'), AES.MODE_CBC, vi.encode('utf8'))
encrypted_bytes = cipher.encrypt(data.encode('utf8'))
# 加密后得到的是bytes类型的数据
encode_strs = base64.urlsafe_b64encode(encrypted_bytes)
# 使用Base64进行编码,返回byte字符串
# 对byte字符串按utf-8进行解码
return encode_strs.decode('utf8')
def aes_decrypt(data):
"""
解密
"""
vi = '0102030405060708'
data = data.encode('utf8')
encode_bytes = base64.urlsafe_b64decode(data)
# 将加密数据转换位bytes类型数据
cipher = AES.new(_key.encode('utf8'), AES.MODE_CBC, vi.encode('utf8'))
text_decrypted = cipher.decrypt(encode_bytes)
unpad = lambda s: s[0:-s[-1]]
text_decrypted = unpad(text_decrypted)
# 补位
text_decrypted = text_decrypted.decode('utf8')
return text_decrypted
if __name__ == '__main__':
_data = '16658273438153332588-95YEUPJR' # 需要加密的内容
enctext = aes_encrypt(_data)
print(enctext)
# enctext = "Wzll1oiVs9UKAySY1-xSy_CbrZmelVwyqu8P0CZTrrc="
# _text_decrypted = aes_decrypt(_key, enctext)
# print(_text_decrypted)