代码语言:python 代码运行次数:2 复制 Cloud Studio代码运行 sm2_crypt=sm2.CryptSM2(public_key=base64.b64decode(public_key).hex(),private_key="",mode=1,asn1=True)ciphertxt=sm2_crypt.encrypt(data.encode("utf-8"))encrypted_text=base64.b64encode(ciphertxt).decode(encoding='utf-8') 最后解密时...
(2)加密:计算密文C = (C1, C2, C3) = (k \cdot G, M ^ t, H(\ x2 \ ||\ M \ ||\ y2\ )) 例:encode_info=sm2_crypt.encrypt(info.encode(encoding="utf-8"))encode_info=b64encode(encode_info).decode() (3)解密:解密密文M = C \cdot2 ^ t 例:decode_info=b64decode(info.enc...
enc_data = sm2_crypt.encrypt(data) #print("enc_data:%s" % enc_data) #print("enc_data_base64:%s" % base64.b64encode(bytes.fromhex(enc_data))) dec_data = sm2_crypt.decrypt(enc_data) print(b"dec_data:%s" % dec_data) assert data == dec_data print("---test sign and verify--...
public_keydefload_key_pair(private_key,public_key):private_key=sm2.CryptSM2().load_private_key(private_key)public_key=sm2.CryptSM2().load_public_key(public_key)returnprivate_key,public_keydefencrypt(public_key,data):cipher_text=public
(public_key=public_key, private_key=private_key) # 待加密的明文 plaintext = b'Hello, SM2!' # 使用公钥加密 ciphertext = sm2_crypt.encrypt(plaintext) print("加密后的密文:", ciphertext) # 使用私钥解密 decrypted_text = sm2_crypt.decrypt(ciphertext) print("解密后的明文:", decrypted_text...
private_key=SM2_PRIVATE_KEY)# 加密defencrypt(info):encode_info=sm2_crypt.encrypt(info.encode(encoding="utf-8"))returnencode_info# 解密defdecrypt(info):decode_info=sm2_crypt.decrypt(info).decode(encoding="utf-8")returndecode_infoif__name__=="__main__":info="123456"encode_info=encrypt(...
decode_info = sm2_crypt.decrypt(info).decode(encoding="utf-8") return decode_info if __name__ == "__main__": action = sys.argv[1] # 取命令中的加解密动作 contact_info = sys.argv[2] # 取命令中需要加解密的内容 if action == "encrypt": ...
:param encrypt_key: sm4加密key :param value: 待加密的字符串 :return: sm4加密后的十六进制值 """crypt_sm4 = self.crypt_sm4 crypt_sm4.set_key(encrypt_key.encode(), sm4.SM4_ENCRYPT) date_str =str(value) encrypt_value = crypt_sm4.crypt_cbc(self.crypt_sm4_iv, date_str.encode())# 返...
()) # 通过base64解码成二进制bytes decode_info = sm2_crypt.decrypt(decode_info).decode(encoding="utf-8") return decode_infoif __name__ == "__main__": origin_pwd = '123456' sm2 = sm2Encrypt() # 加密的密码 encrypy_pwd = sm2.encrypt(origin_pwd) print(encrypy_pwd) # 解密的密码...
fromgmsslimportsm2,func# 密钥对生成private_key='00'*32# 私钥可以通过安全的方法生成public_key='00'*65# 公钥应根据私钥生成sm2_crypt=sm2.SM2(private_key,public_key)# 明文plain_text=b'Hello, SM2!'# 加密cipher_text=sm2_crypt.encrypt(plain_text)print(f'Cipher Text:{cipher_text.hex()}')#...