Skip to main content

BlueKing KMS Python SDK

提供信封的本地解密功能,支持 Python >=3.11,<3.15。

安装

pip install bk-kms-sdk

需要 SM2/SM4 国密算法时安装:

pip install "bk-kms-sdk[gm]"

国密依赖兼容平台的 Tongsuo 后端;RSA/AES 无需安装国密依赖。

解密

import os
from pathlib import Path

from bk_kms import decrypt

plaintext = decrypt(
    envelope=Path("envelope.txt").read_text(encoding="utf-8").strip(),
    private_key=os.environ["BK_KMS_PRIVATE_KEY"].strip(),
)

envelope.txt 保存 Base64 编码的信封;环境变量 BK_KMS_PRIVATE_KEY 保存配套的 Base64 编码 PEM 私钥内容,不是私钥文件路径。

decrypt(envelope, private_key) 返回原始 UTF-8 明文字符串。信封中的算法字段决定使用 RSA/SM2、AES/SM4 和 CBC/CTR,无需额外配置。

如果录入的内容是 JSON,由应用解析并读取所需字段:

import json

credentials = json.loads(plaintext)

应用负责获取信封和配套私钥、管理配置及使用明文。完整示例见 examples/decrypt.py。

错误处理

  • EnvelopeDecodeError:参数为空或类型错误、信封格式或算法不支持、私钥不匹配、解密失败,或明文不是 UTF-8。
  • CryptoBackendUnavailableError:国密后端不可用;安装兼容平台的 bk-kms-sdk[gm]。
  • CryptoError:以上解密错误的公共基类。

请勿记录私钥、明文或完整信封。

Metadata

Release files for bk-kms-sdk 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bk-kms-sdk 1.0.0
File Size Uploaded
bk_kms_sdk-1.0.0.tar.gz 8.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bk-kms-sdk 1.0.0
File Interpreter ABI Platform
bk_kms_sdk-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 19.8 kB

Release files / bk_kms_sdk-1.0.0.tar.gz

Download URL bk_kms_sdk-1.0.0.tar.gz
Size 8.5 kB
Tags Source
SHA-256 checksum
How to use checksums
34962182f15f7e1c6b4edbde25ab43610d0d0344373b2977e9084808484a0fd4
BLAKE2b-256 checksum
How to use checksums
8209005d9c6a32574b78bcc1f7fa08b230ce73ed69eb75292fbfc02b6e03fe76
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / bk_kms_sdk-1.0.0-py3-none-any.whl

Download URL bk_kms_sdk-1.0.0-py3-none-any.whl
Size 11.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ba7357de1ec442e676a2e203ecd465fb7c79f0c5d0614a380d4629e85484107d
BLAKE2b-256 checksum
How to use checksums
37b89cc7810f14b2f7c81191f372598f4ee83ea76d300c8db04811df47ff6204
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page