from gmssl.sm4 import CryptSM4, SM4_ENCRYPT, SM4_DECRYPT import binascii import base64 from gmssl import sm2, func # GMSSL for Python # 安装 pip install gmssl # keycode = '101231221289195374403401962572899' key = '1012312212891953' iv = '1012312262572899' inputText = '{"key":"1012312212891953","iv":"1012312262572899","encryptText":"123456","inputType":"sm4-cbc","formatOut":"base64"}' # bytes类型 private_key = 'CB772811F1FEF955CE1B4051130870D86CCA6AFEDE806F1E7C225D7359591D2B' public_key = '0475E60AB5B94860DAD0C2D193551A8B7A628A611DF332E23DFCB42F6ECC348653B8A49418E52FF8872B500EEAF8BE8C43B7389D115E91B7432BB1C939E764D31A' cipherMode = 1 keyBytes = key.encode() ivBytes = iv.encode() inputTextBytes = inputText.encode() def ByteToHex( bins ): return ''.join( [ "%02X" % x for x in bins ] ).strip() def HexToByte( hexStr ): return bytes.fromhex(hexStr) #sm4CBC crypt_sm4 = CryptSM4() crypt_sm4.set_key(keyBytes, SM4_ENCRYPT) encrypt_value = crypt_sm4.crypt_cbc(ivBytes , inputTextBytes) # bytes类型 crypt_sm4.set_key(keyBytes, SM4_DECRYPT) decrypt_value = crypt_sm4.crypt_cbc(ivBytes, encrypt_value) decrypt_value = decrypt_value.decode('utf-8') assert inputText == decrypt_value #print("SM4CBC加密(bytes):",encrypt_value) print("SM4CBC加密 (hex):",ByteToHex(encrypt_value)) print("SM4CBC加密 (base64):",base64.b64encode(encrypt_value)) print("SM4CBC解密(utf-8):",decrypt_value) #sm2 cipherMode (c1c3c2) sm2_crypt = sm2.CryptSM2( public_key = public_key, private_key = private_key, mode = cipherMode ) enc_data = sm2_crypt.encrypt(inputTextBytes) dec_data =sm2_crypt.decrypt(enc_data).decode('utf-8') assert dec_data == dec_data #print("SM2加密(bytes):",enc_data) print("SM2加密 (hex):","04" + ByteToHex(enc_data)) print("SM2加密 (hex):","04" + enc_data.hex()) print("SM2解密(utf-8):",dec_data)
from gmssl.sm4 import CryptSM4, SM4_ENCRYPT, SM4_DECRYPT import binascii import base64 from gmssl import sm2, func # GMSSL for Python # 安装 pip install gmssl # 封装函数 def sm2Encrypt(inputText, public_key, private_key, cipherMode = 1): inputTextBytes = inputText.encode() sm2_crypt = sm2.CryptSM2( public_key = public_key, private_key = private_key, mode = cipherMode ) sm2EncryptData = sm2_crypt.encrypt(inputTextBytes) return '04' + sm2EncryptData.hex() def sm4Encrypt(inputText, keycode): key = keycode[:16] iv = keycode[:8] + keycode[-8:] keyBytes = key.encode() ivBytes = iv.encode() inputTextBytes = inputText.encode() crypt_sm4 = CryptSM4() crypt_sm4.set_key(keyBytes, SM4_ENCRYPT) sm4EncryptData = crypt_sm4.crypt_cbc(ivBytes, inputTextBytes) # bytes类型 return base64.b64encode(sm4EncryptData).decode('utf-8') def sm4Decrypt(inputText, keycode): key = keycode[:16] iv = keycode[:8] + keycode[-8:] keyBytes = key.encode() ivBytes = iv.encode() #inputTextBytes = inputText.encode() inputTextBytes = base64.b64decode(inputText) crypt_sm4 = CryptSM4() crypt_sm4.set_key(keyBytes, SM4_DECRYPT) sm4DecryptData = crypt_sm4.crypt_cbc(ivBytes, inputTextBytes) # bytes类型 return sm4DecryptData.decode('utf-8') #sm2加密 inputText = '{"key":"1012312212891953","iv":"1012312262572899","encryptText":"123456","inputType":"sm4-cbc","formatOut":"base64"}' # bytes类型 private_key = 'CB772811F1FEF955CE1B4051130870D86CCA6AFEDE806F1E7C225D7359591D2B' public_key = '0475E60AB5B94860DAD0C2D193551A8B7A628A611DF332E23DFCB42F6ECC348653B8A49418E52FF8872B500EEAF8BE8C43B7389D115E91B7432BB1C939E764D31A' sm2EncryptData = sm2Encrypt(inputText, public_key, private_key, cipherMode = 1) print('sm2函数加密:',sm2EncryptData) #sm4CBC加密 keycode = '101231221289195374403401962572899' inputText = '{"key":"1012312212891953","iv":"1012312262572899","encryptText":"123456","inputType":"sm4-cbc","formatOut":"base64"}' sm4EncryptData = sm4Encrypt(inputText, keycode) print('sm4函数加密:',sm4EncryptData) #sm4CBC解密 keycode = '101231221289195374403401962572899' inputText = 'BHHr6W74yPnZEx4kFixG6bCTE7VJabZwANxpsh0G2cmd1ztFH5qSA7Qf1M6bxN4mh1wXxv0yDSO8sZnmvuH3XHNIiE7EaI+fxRsOQTcmZei5fKoaxSnT9ltv79VqSJ8D4m3YO5jiWOVEaJ+x5i8yx4iOWpcc5ZPhHT8Y6f8EP0o=' sm4EncryptData = sm4Decrypt(inputText, keycode) print('sm4函数解密:',sm4EncryptData)
标签:CBC,python,crypt,sm4,sm2,key,inputText,keycode From: https://www.cnblogs.com/zhouein/p/18202419