Python SM4 加密&解密


# SM4加密/解密

# CryptSM4中的crypt_ecb,crypt_ecb加解密,完成后,均为十六进制的bytes类型,用base64.b64encode,base64.b64decode 去转码成接口常用的类型

# 因为是刚学习不久,对封装的处理不尽人意,后续再来优化,注释中涉及的转码的解释,也比较不准确,大概是那么个意思,好在加密/解密已经实现


import json

from gmssl.sm4 import CryptSM4, SM4_ENCRYPT, SM4_DECRYPT
import base64

class SM4Utils:


def __init__(self, ):
pass

# 加密方法
def encryptData_ECB(self, plain_text):
# 创建 SM4对象
crypt_sm4 = CryptSM4()
# 定义key值
secret_key = b"JeF38U9wT9wlMfs2"
# print("key: ", secret_key)

# 设置key
crypt_sm4.set_key(secret_key, SM4_ENCRYPT)

# 调用加密方法加密(十六进制的bytes类型)
encrypt_value = crypt_sm4.crypt_ecb(plain_text)
# print("encrypt_value: ", encrypt_value)

# 用base64.b64encode转码(编码后的bytes)
cipher_text = base64.b64encode(encrypt_value)

# print("加密后:", cipher_text)
# print(cipher_text.decode('utf-8', 'ignore'))
# 返回加密后的字符串
return cipher_text.decode('utf-8', 'ignore')

def decryptData_ECB(self, cipher_text):
crypt_sm4 = CryptSM4()
secret_key = b"JeF38U9wT9wlMfs2"
print(secret_key)
crypt_sm4.set_key(secret_key, SM4_DECRYPT)
# 将转入参数base64.b64decode解码成十六进制的bytes类型
byt_cipher_text = base64.b64decode(cipher_text)
# 调用加密方法解密,解密后为bytes类型
decrypt_value = crypt_sm4.crypt_ecb(byt_cipher_text)
print(decrypt_value)
print(decrypt_value.decode('utf-8', 'ignore'))
print(json.dumps(decrypt_value.decode('utf-8', 'ignore')))

return decrypt_value.decode('utf-8', 'ignore')



if __name__ == '__main__':
SM4_Utils = SM4Utils()

plain_text = b'{"t":1625451188275,"userToken":"c49bdd6a558948ec86c957c8bd07c1c8"}'
# plain_text = "aaa"
SM4_Utils.encryptData_ECB(plain_text)

cipher_text = r"vk2cLOFpUU9fpOs5p9+Mjdl2Ik3s/XXS/BJE1Sgbt8jPFh1kmV2Gf0UUO4ak/Xq4NVARIeK73Q9w7G4MOnRdvxnS4XCWYl7huV2GNew6MEo="
SM4_Utils.decryptData_ECB(cipher_text)


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM