1、背景:
因业务需求,需要与某平台接口对接。平台是Java基于Hutool库实现的SM2加密解密,研究了下SM2的加解密算法,网上找的资料,都是说SM2【椭圆曲线】 公钥长【x,y分量 64字节】,私钥短【32字节】;而平台给的Hutool生成的密钥对,私钥反而比公钥更长,直接用Pyhton做SM2加解密,难以实现数据的互通。后多方查找资料,几经测试终于弄成,其他编程语言亦可参考,现分享如下。
2、密钥分析:
主要参考资料,引用自:https://juejin.cn/post/6981721907653509128
公钥转码分析:
私钥转码分析:
加解密注意事项:
Python Demo 代码:
#!/usr/bin/python3
#coding=utf-8
#用Python实现与Java Hutool库 SM2加密、解密 互通
#重要参考资料 引用 https://juejin.cn/post/6981721907653509128
from gmssl import sm2
from base64 import b64encode, b64decode
import requests
# 常量定义 用Java Hutool库 实现的SM2加密、解密的接口 【密钥格式:Base64, 密文格式:Hex字符串,大写】
JAVA_HUTOOL_ENCRYPT_URL = 'http://127.0.0.1:8384/publicKeyEncrypt'
JAVA_HUTOOL_DECRYPT_URL = 'http://127.0.0.1:8384/privateKeyDecrypt'
def sm2_key_pair_java_hutool_to_python(public_sm2_key_base64, private_sm2_key_base64):
"""
将Java Hutool生成的SM2公钥和私钥转换为Python中使用的格式
Args:
public_sm2_key_base64 (str): Java Hutool生成的SM2公钥的Base64编码字符串
private_sm2_key_base64 (str): Java Hutool生成的SM2私钥的Base64编码字符串
Returns:
tuple: 包含两个元素的元组,分别为:
- str: 截取后的SM2公钥(大写的Hex字符串)
- str: 截取后的SM2私钥(大写的Hex字符串)
"""
public_sm2_key_bin = b64decode(public_sm2_key_base64)
private_sm2_key_bin = b64decode(private_sm2_key_base64)
public_sm2_key_hex = ''.join(format(x, '02X') for x in public_sm2_key_bin)
private_sm2_key_hex = ''.join(format(x, '02X') for x in private_sm2_key_bin)
sm2_public_key = public_sm2_key_hex[-128:]
sm2_private_key = private_sm2_key_hex[72:72+64]
return sm2_public_key, sm2_private_key
def encrypt(message, sm2_crypt):
"""
使用SM2加密消息
Args:
message (str): 要加密的消息
sm2_crypt (sm2.CryptSM2): SM2加密对象
Returns:
str: 加密后的Hex字符串
"""
try:
byte_array = sm2_crypt.encrypt(message.encode(encoding="utf-8"))
encode_info = '04' + ''.join(format(x, '02X') for x in byte_array)
return encode_info
except Exception as e:
print(f"Encryption error: {e}")
return None
def decrypt(hex_string, sm2_crypt):
"""
使用SM2解密Hex字符串
Args:
hex_string (str): 要解密的Hex字符串
sm2_crypt (sm2.CryptSM2): SM2加密对象
Returns:
str: 解密后的明文信息
"""
try:
if hex_string.startswith("04"):
hex_string = hex_string[2:]
byte_array = bytes.fromhex(hex_string)
decode_info = sm2_crypt.decrypt(byte_array).decode(encoding="utf-8")
return decode_info
except Exception as e:
print(f"Decryption error: {e}")
return None
def encrypt_java_hutool(message, public_key_base64):
"""
使用Java Hutool加密消息
Args:
message (str): 要加密的消息
public_key_base64 (str): Java Hutool生成的公钥的Base64编码字符串
Returns:
str: 加密后的字符串
"""
try:
data = {
'publicKey': public_key_base64,
'plainTxt': message
}
response = requests.post(url=JAVA_HUTOOL_ENCRYPT_URL, data=data)
return response.json()['data']
except Exception as e:
print(f"Java Hutool Encryption error: {e}")
return None
def decrypt_java_hutool(cipher, private_key_base64):
"""
使用Java Hutool解密消息
Args:
cipher (str): 要解密的Hex字符串
private_key_base64 (str): Java Hutool生成的私钥的Base64编码字符串
Returns:
str: 解密后的明文信息
"""
try:
data = {
'privateKey': private_key_base64,
'ciphertext': cipher
}
response = requests.post(url=JAVA_HUTOOL_DECRYPT_URL, data=data)
return response.json()['data']
except Exception as e:
print(f"Java Hutool Decryption error: {e}")
return None
if __name__ == "__main__":
# JavaHutool 生成的公钥私钥对
public_sm2_key_base64 = 'MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAErcXffRE7psQ7xTkrxEBzH3VQVviZ/pd1HBDFTfUwOmqx5n4zNjqJfDzS7yvFCfAmehoejdeE2UTecJgb72dtDQ=='
private_sm2_key_base64 = 'MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgXFpB6hO0RjwT7UhFkqBX85/hclTf45nPZ8ljuANGTu+gCgYIKoEcz1UBgi2hRANCAAStxd99ETumxDvFOSvEQHMfdVBW+Jn+l3UcEMVN9TA6arHmfjM2Ool8PNLvK8UJ8CZ6Gh6N14TZRN5wmBvvZ20N'
sm2_public_key, sm2_private_key = sm2_key_pair_java_hutool_to_python(public_sm2_key_base64, private_sm2_key_base64)
sm2_crypt = sm2.CryptSM2(
public_key=sm2_public_key,
private_key=sm2_private_key
)
message = "呵呵呵1234567890123abc"
print("原始内容为: " + message)
cipher = encrypt(message, sm2_crypt)
print("Python加密结果为: " + cipher)
java_hutool_message = decrypt_java_hutool(cipher, private_sm2_key_base64)
print("Java Hutool解密结果为: " + java_hutool_message)
java_hutool_cipher = encrypt_java_hutool(message, public_sm2_key_base64)
print("Java Hutool加密结果为: " + java_hutool_cipher)
python_message = decrypt(java_hutool_cipher, sm2_crypt)
print("Python解密结果为: " + python_message)