File size: 5,648 Bytes
cbc00ce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | # -*- coding: utf-8 -*-
# Copyright (c) Alibaba, Inc. and its affiliates.
# mypy: disable-error-code="annotation-unchecked"
import base64
import json
import os
import requests
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.backends import default_backend
import dashscope
from dashscope.common.constants import (
ENCRYPTION_AES_SECRET_KEY_BYTES,
ENCRYPTION_AES_IV_LENGTH,
)
from dashscope.common.logging import logger
class Encryption:
def __init__(self):
self.pub_key_id: str = ""
self.pub_key_str: str = ""
self.aes_key_bytes: bytes = b""
self.encrypted_aes_key_str: str = ""
self.iv_bytes: bytes = b""
self.base64_iv_str: str = ""
self.valid: bool = False
def initialize(self):
public_keys = self._get_public_keys()
if not public_keys:
return
public_key_str = public_keys.get("public_key")
public_key_id = public_keys.get("public_key_id")
if not public_key_str or not public_key_id:
logger.error("public keys data not valid")
return
aes_key_bytes = self._generate_aes_secret_key()
iv_bytes = self._generate_iv()
encrypted_aes_key_str = self._encrypt_aes_key_with_rsa(
aes_key_bytes,
public_key_str,
)
base64_iv_str = base64.b64encode(iv_bytes).decode("utf-8")
self.pub_key_id = public_key_id
self.pub_key_str = public_key_str
self.aes_key_bytes = aes_key_bytes
self.encrypted_aes_key_str = encrypted_aes_key_str
self.iv_bytes = iv_bytes
self.base64_iv_str = base64_iv_str
self.valid = True
def encrypt(self, dict_plaintext):
return self._encrypt_text_with_aes(
json.dumps(dict_plaintext, ensure_ascii=False),
self.aes_key_bytes,
self.iv_bytes,
)
def decrypt(self, base64_ciphertext):
return self._decrypt_text_with_aes(
base64_ciphertext,
self.aes_key_bytes,
self.iv_bytes,
)
def is_valid(self):
return self.valid
def get_pub_key_id(self):
return self.pub_key_id
def get_encrypted_aes_key_str(self):
return self.encrypted_aes_key_str
def get_base64_iv_str(self):
return self.base64_iv_str
@staticmethod
def _get_public_keys():
url = dashscope.base_http_api_url + "/public-keys/latest"
headers = {
"Authorization": f"Bearer {dashscope.api_key}",
}
response = requests.get(url, headers=headers)
if response.status_code != 200:
logger.error("exceptional public key response: %s", response)
return None
json_resp = response.json()
response_data = json_resp.get("data")
if not response_data:
logger.error("no valid data in public key response")
return None
return response_data
@staticmethod
def _generate_aes_secret_key():
return os.urandom(ENCRYPTION_AES_SECRET_KEY_BYTES)
@staticmethod
def _generate_iv():
return os.urandom(ENCRYPTION_AES_IV_LENGTH)
@staticmethod
def _encrypt_text_with_aes(plaintext, key, iv):
"""使用AES-GCM加密数据"""
# 创建AES-GCM加密器
aes_gcm = Cipher(
algorithms.AES(key),
modes.GCM(iv, tag=None),
backend=default_backend(),
).encryptor()
# 关联数据设为空(根据需求可调整)
aes_gcm.authenticate_additional_data(b"")
# 加密数据
ciphertext = (
aes_gcm.update(plaintext.encode("utf-8")) + aes_gcm.finalize()
)
# 获取认证标签
tag = aes_gcm.tag
# 组合密文和标签
encrypted_data = ciphertext + tag
# 返回Base64编码结果
return base64.b64encode(encrypted_data).decode("utf-8")
@staticmethod
def _decrypt_text_with_aes(base64_ciphertext, aes_key, iv):
"""使用AES-GCM解密响应"""
# 解码Base64数据
encrypted_data = base64.b64decode(base64_ciphertext)
# 分离密文和标签(标签长度16字节)
ciphertext = encrypted_data[:-16]
tag = encrypted_data[-16:]
# 创建AES-GCM解密器
aes_gcm = Cipher(
algorithms.AES(aes_key),
modes.GCM(iv, tag),
backend=default_backend(),
).decryptor()
# 验证关联数据(与加密时一致)
aes_gcm.authenticate_additional_data(b"")
# 解密数据
decrypted_bytes = aes_gcm.update(ciphertext) + aes_gcm.finalize()
# 明文
plaintext = decrypted_bytes.decode("utf-8")
return json.loads(plaintext)
@staticmethod
def _encrypt_aes_key_with_rsa(aes_key, public_key_str):
"""使用RSA公钥加密AES密钥"""
# 解码Base64格式的公钥
public_key_bytes = base64.b64decode(public_key_str)
# 加载公钥
public_key = serialization.load_der_public_key(
public_key_bytes,
backend=default_backend(),
)
base64_aes_key = base64.b64encode(aes_key).decode("utf-8")
# 使用RSA加密
encrypted_bytes = public_key.encrypt(
base64_aes_key.encode("utf-8"),
padding.PKCS1v15(),
)
return base64.b64encode(encrypted_bytes).decode("utf-8")
|