openssl sm2 国密算法

sm2算法演示

  • 获取曲线参数
  • 生成秘钥对
  • 自定义userid
  • 加密解密
  • 签名验签
  • 输出公私钥对

#include <openssl/evp.h>
#include <string.h>
#include <openssl/ec.h>
#include <openssl/bio.h>
#include <openssl/pem.h>
#include <iostream>
#include <stdlib.h>
void sm2test() {
	//直接创建一个group会导致i2d方法错误,原因是没有group name,可能需要调用EC_GROUP_set_curve_name()
	//EC_GROUP* gm_group = create_EC_group(
	//	"fffffffeffffffffffffffffffffffffffffffff00000000ffffffffffffffff",
	//	"fffffffeffffffffffffffffffffffffffffffff00000000fffffffffffffffc",
	//	"28e9fa9e9d9f5e344d5a9e4bcf6509a7f39789f515ab8f92ddbcbd414d940e93",
	//	"32c4ae2c1f1981195f9904466a39c9948fe30bbff2660be1715a4589334c74c7",
	//	"bc3736a2f4f6779c59bdcee36b692153d0a9877cc62a474002df32e52139f0a0",
	//	"fffffffeffffffffffffffffffffffff7203df6b21c6052b53bbf40939d54123",
	//	"1");
	EC_KEY* key = EC_KEY_new();
	EC_GROUP* gm_group = EC_GROUP_new_by_curve_name(NID_sm2);
	
	int ret = EC_KEY_set_group(key, gm_group);

	using namespace std;
	if (!ret) {
		cout << "set group error" << endl;
		return;
	}
	//生成秘钥对
	ret = EC_KEY_generate_key(key);
	if (!ret) {
		cout << "EC_KEY_generate_key error" << endl;
		return;
	}
	const BIGNUM* pri = EC_KEY_get0_private_key(key);

	const EC_POINT* point = EC_KEY_get0_public_key(key);


	EVP_PKEY* pkey = EVP_PKEY_new();
	ret = EVP_PKEY_set1_EC_KEY(pkey, key);

	if (!ret) {
		cout << "EVP_PKEY_set1_EC_KEY error" << endl;
		return;
	}
	//如果使用create_EC_group,这里需要替换成国密算法
	//ret = EVP_PKEY_set_alias_type(pkey, EVP_PKEY_SM2);
	//if (!ret) {
	//	cout << "EVP_PKEY_set_alias_type error" << endl;
	//	return;
	//}

	EVP_PKEY_CTX* pkeyCtx = EVP_PKEY_CTX_new(pkey, NULL);
	//设置SM2的id,idlen
	const char* id = "adgdfdfdfdfd";
	EVP_PKEY_CTX_set1_id(pkeyCtx, id, strlen(id));


	EVP_PKEY_encrypt_init(pkeyCtx);
	const  char* data = "hello";

	unsigned char* out = (unsigned char*)malloc(strlen(data) + 96);
	size_t  outlen = strlen(data) + 96; //如果输入数据传空,返回需要的密文空间大小,
	ret = EVP_PKEY_encrypt(pkeyCtx, out, &outlen, (const unsigned char*)data, strlen(data));
	if (!ret) {
		cout << "EVP_PKEY_encrypt error" << endl;
		return;
	}
	EVP_PKEY_decrypt_init(pkeyCtx);
	unsigned char debuf[10] = { 0 };
	size_t dlen = 10;
	EVP_PKEY_decrypt(pkeyCtx, debuf, &dlen, out, outlen);
	debuf[dlen] = '\0';
	printf("%s\n", debuf);

	//可以加密可以解密

	EVP_PKEY_sign_init(pkeyCtx);
	const  char* tbs = "hello";
	size_t signlen;

	ret = EVP_PKEY_sign(pkeyCtx, NULL, &signlen, (const unsigned char*)tbs, (size_t)strlen((const char*)tbs));

	if (!ret) {
		cout << "EVP_PKEY_sign error" << endl;
		return;
	}
	string str(signlen, '\0');

	//可以签名验签
	ret = EVP_PKEY_sign(pkeyCtx, (unsigned char*)str.data(), &signlen, (const unsigned char*)tbs, (size_t)strlen((const char*)tbs));
	if (!ret) {
		cout << "EVP_PKEY_sign error" << endl;
		return;
	}
	EVP_PKEY_verify_init(pkeyCtx);
	ret = EVP_PKEY_verify(pkeyCtx, (unsigned char*)str.data(), signlen, (const unsigned char*)tbs, (size_t)strlen((const char*)tbs));
	if (!ret) {
		cout << "EVP_PKEY_verify error" << endl;
		return;
	}

	ret = PEM_write_ECPrivateKey(stdout, key, NULL, NULL, 0, NULL, NULL);
	if (!ret) {
		cout << "PEM_write_ECPrivateKey error" << endl;
		return;
	}
	ret = PEM_write_EC_PUBKEY(stdout, key);
	if (!ret) {
		cout << "PEM_write_EC_PUBKEY error" << endl;
		return;
	}
	//unsigned char* der = (unsigned char*)malloc(2048);
	//int derlen=i2d_EC_PUBKEY(key, &der);
	//if (derlen > 0) {
	//	cout << derlen << endl;
	//}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值