创建sm2密钥对demo

创建sm2密钥对demo

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <openssl/evp.h>
#include <openssl/sm2.h>
#include <openssl/sm3.h>
#include <openssl/pem.h>
#include <openssl/bio.h>
#include <openssl/pkcs7.h>
#include <openssl/objects.h>

//gcc create_sm2.c -I ./openssl/include -L ./openssl/lib -lssl -lcrypto -Wl,-rpath=/home/lib/openssl/

void print_hex(const char *tips, const unsigned char *pdata, const int len) {

	if (tips)
		printf("%s[%d]: \n", tips, len);

	int i  = 0;
	for (i = 0; i < len; i++) {
		printf("0x%02X", pdata[i]);
		if (i != len - 1)		printf(", ");
		if ((i + 1) % 8 == 0)	printf("\n");
	}
	printf("\n");
}

static EC_KEY *create_sm2(void)
{
    EC_KEY  *eckey = NULL;

    /* 根据 NID 创建指定密钥结构体 */
    eckey = EC_KEY_new_by_curve_name(EVP_PKEY_SM2);
    if (eckey == NULL) {
        return NULL;
    }

    /* 指定参数编码方法 */
    EC_KEY_set_asn1_flag(eckey, OPENSSL_EC_NAMED_CURVE);

    /* 坐标不压缩 */
    EC_KEY_set_conv_form(eckey, POINT_CONVERSION_UNCOMPRESSED);

    /* 产生密钥 */
    if (EC_KEY_generate_key(eckey) == 0) {
        EC_KEY_free(eckey);
        return NULL;
    }

    return eckey;
}

int main(){
	EC_KEY *eckey = create_sm2();
	printf("create_sm2.\n");
	
	EC_KEY_print_fp(stdout, eckey, 0);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值