mirror of
https://git.oceanpay.cc/danial/kami_itunes_third_api.git
synced 2025-12-18 22:20:08 +00:00
feat(crypto):优化AES CBC加密实现- 添加函数参数类型注解- 使用零填充替代PKCS7填充
- 明确数据块大小为16字节 - 简化加密逻辑并提高可读性 -保持Base64编码输出格式 - 移除冗余的日志记录语句
This commit is contained in:
@@ -14,21 +14,27 @@ def decode_base64(data: str):
|
||||
return base64.decodebytes(text.encode()).decode("utf-8")
|
||||
|
||||
|
||||
def encrypt_cbc_base64(data, key, iv):
|
||||
def encrypt_cbc_base64(data: str, key: str, iv: str) -> str:
|
||||
"""使用AES CBC模式加密并返回Base64编码的结果"""
|
||||
key_bytes = key.encode("utf-8")
|
||||
iv_bytes = iv.encode("utf-8")
|
||||
to_encrypt_bytes = data.encode("utf-8")
|
||||
|
||||
if len(key_bytes) != 16 or len(iv_bytes) != 16:
|
||||
raise ValueError("Key and IV must be 16 bytes long.")
|
||||
|
||||
# Pad data to AES block size (16 bytes)
|
||||
pad_length = 16 - len(to_encrypt_bytes) % 16
|
||||
to_encrypt_bytes += bytes([0] * pad_length)
|
||||
|
||||
# Create cipher and encrypt
|
||||
cipher = Cipher(
|
||||
algorithms.AES(key_bytes), modes.CBC(iv_bytes), backend=default_backend()
|
||||
)
|
||||
encryptor = cipher.encryptor()
|
||||
# PKCS7 padding
|
||||
block_size = 16
|
||||
pad_length = block_size - len(to_encrypt_bytes) % block_size
|
||||
to_encrypt_bytes += bytes([pad_length] * pad_length)
|
||||
encrypted_bytes = encryptor.update(to_encrypt_bytes) + encryptor.finalize()
|
||||
|
||||
# Return base64 encoded result
|
||||
encrypted_base64 = base64.b64encode(encrypted_bytes).decode()
|
||||
return encrypted_base64
|
||||
|
||||
|
||||
@@ -85,7 +85,6 @@ class MasterNodeService:
|
||||
AESKey.load_from_base64("nywao1XkDXeYwbPeWh+SxA=="),
|
||||
)
|
||||
result.data.password = self.__password
|
||||
logger.info(f"查询到订单:{result.data}")
|
||||
return result.data
|
||||
|
||||
# 修改充值状态
|
||||
|
||||
Reference in New Issue
Block a user