资源简介
采用该用例生成的SM2的公私钥为字符串,非常方便SM2公私钥密钥对的导入导出,同时还写了一个SM2加解密的算法,其中密钥传入都是以字符串的形式传入的。
代码片段和文件信息
#include
#include “openssl/err.h“
#include
#include
#include
#include
#include
#include
#include
using namespace std;
EC_KEY* CreateEC(unsigned char* key int is_public)
{
EC_KEY *ec_key = NULL;
BIO *keybio = NULL;
keybio = BIO_new_mem_buf(key -1);
if (keybio==NULL) {
cout << “Failed to Get Key“ << endl;
exit(1);
}
if(is_public) {
ec_key = PEM_read_bio_EC_PUBKEY(keybio NULL NULL NULL);
}
else {
ec_key = PEM_read_bio_ECPrivateKey(keybio NULL NULL NULL);
}
if(ec_key == NULL) {
cout << “Failed to Get Key“ << endl;
exit(1);
}
return ec_key;
}
pair GenKey(void)
{
EC_KEY *keypair = NULL;
EC_GROUP *group1 = NULL;
keypair = EC_KEY_new();
if(!keypair) {
cout << “Failed to Gen Key“ << endl;
exit(1);
}
group1 = EC_GROUP_new_by_curve_name(NID_sm2p256v1);
if(group1 == NULL){
cout << “Failed to Gen Key“ << endl;
exit(1);
}
int ret1 = EC_KEY_set_group(keypair group1);
if(ret1 != 1){
cout << “Failed to Gen Key“ << endl;
exit(1);
}
int ret2 = EC_KEY_generate_key(keypair);
if(ret2 != 1){
cout << “Failed to Gen Key“ << endl;
exit(1);
}
size_t pri_len;
size_t pub_len;
char *pri_key = NULL;
char *pub_key = NULL;
BIO *pri = BIO_new(BIO_s_mem());
BIO *pub = BIO_new(BIO_s_mem());
PEM_write_bio_ECPrivateKey(pri keypair NULL NULL 0 NULL NULL);
PEM_write_bio_EC_PUBKEY(pub keypair);
pri_len = BIO_pending(pri);
pub_len = BIO_pending(pub);
pri_key = new char[pri_len + 1];
pub_key = new char[pub_len + 1];
BIO_read(pri pri_key pri_len);
BIO_read(pub pub_key pub_len);
- 上一篇:bpsk的C语言代码
- 下一篇:svm_smo_多分类_c++
相关资源
- C语言openssl库SM2-SM3国密常用函数
- SM2算法C语言实现
- sm2算法源码实现
- sm2算法签名验签实现
- 国密SM2非对称算法C语言实现
- c语言实现国密SM2
- C语言实现的SM2数字签名验证
- 国密SM2算法C代码
- SM2/SM3算法C语言实现
- 国密算法--Openssl 实现国密算法加密和
- SM2_SM3_SM4_C语言实现+SM3_C++实现+国家密
- sm2加密,解密,签名,验签sm3哈希基
- SM2_SM3_SM4_C语言实现
- SM2SM3SM4国密算法C语言实现VS2008
- 国密算法 SM2_SM3_SM4 C语言实现
- Sm2_sm3_sm4_c语言实现.zip
- SM2SM3;SM4;国密算法的C语言实现.rar
- sm2 签名验证 公钥机密私钥解密的实现
评论
共有 条评论