SM2 加解密 python 代码
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
| from gmssl import sm2 from base64 import b64encode, b64decode
SM2_Public_Key = '99B7A22845A59292F2841AA543C36D55FF92221EC919C67B4847571FE2256265B614F70053057908955757395AD18290BFC7B1B6111305760F8DAB5003AFA148' SM2_Private_Key = '91EA7935FF011F08CBA52E221C52BBC6042058107DCE6D22F84D9A8D338B8075'
sm2_crypt = sm2.CryptSM2(public_key=SM2_Public_Key, private_key=SM2_Private_Key)
def Encrypt(message): encode_info = sm2_crypt.encrypt(message.encode(encoding="utf-8")) encode_info = b64encode(encode_info).decode() return encode_info
def Decrypt(cipher): decode_info = b64decode(cipher.encode()) decode_info = sm2_crypt.decrypt(decode_info).decode(encoding="utf-8") return decode_info
if __name__ == "__main__": message = "1234567890123abc" encode_message = Encrypt(message) print("加密结果为: " + encode_message) decode_cipher = Decrypt(encode_message) print("解密结果为: " + decode_cipher)
|
实验结果
1 2 3 4
| 加密结果为: Md2B+Ipf2QnOSiS3yoCHN4qx4bAUJLj3S3Ac/7ssEdDOj00G8QePWUWCMH+ik5h9+2JY9qeiYLM2jUK0/tkNQAJMb1tF7fXeqRpr5lxg5e1tJilxUolsjEFO5j0Motn3MxzV/MvBst4dgIlorQ0ftw== 解密结果为: 1234567890123abc
Process finished with exit code 0
|
Author:
QUANQUAN
License:
Copyright (c) 2019 CC-BY-NC-4.0 LICENSE
Slogan:
Make the world MORE PERFECT !