GmSSL对数据进行非对称加解密

GitHub - guanzhi/GmSSL: 支持国密SM2/SM3/SM4/SM9/SSL的密码工具箱下载最新的版本并进程解压,使用CMake进行编译

]

BUILD_SHARED_LIBS 要打钩,否则编译出来就是静态库

将Sm2.h中的 SM2_MAX_PLAINTEXT_SIZE 和 SM2_MAX_CIPHERTEXT_SIZE 修改为最大支持的明文和密文长度

然后执行 make && make install 后在安装目录下出现所需要的动态库

 使用sm2_key_generate 函数产生公钥和私钥,并记录

    SM2_KEY sm2_key;
	if (sm2_key_generate(&sm2_key) != 1) {
		fprintf(stderr, "error\n");
		return 1;
	}
	sm2_key_print(stdout, 0, 0, "SM2PrivateKey", &sm2_key);
	sm2_public_key_print(stdout, 0, 0, "SM2PublicKey", &sm2_key);

 编写测试代码sm2Test.cpp

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/sm2.h>
#include <gmssl/error.h>
#include <iostream>
#include <string>
#include <unistd.h>
#define SM2KEY_STR_LEN     64
#define SM2KEY_BYTE_LEN    (SM2KEY_STR_LEN/2)
#define PUBLIC_KEY_X    "33605BEF8A2A43215F4E1AB2B111B31E6C2A0C45A1A18F7A435EEEA1F0DD4B84"
#define PUBLIC_KEY_Y    "DD2F13C68810B5F7655C7AFE8A779ABC9B0B24E280FBFF61676E7256E06D34F1"
#define PRIAVTE_KEY     "DCD24765DFCBACD240A25942AD5DEBCA59808F3B3B7006A6D7F961B68F86B8DE"



/*****************************
typedef struct {
	uint8_t x[32];
	uint8_t y[32];
} SM2_POINT;
typedef struct {
	SM2_POINT public_key;
	uint8_t private_key[32];
} SM2_KEY;
*********************/


static uint16_t charToInt(const char hex)
{
    if (hex>='0' && hex <='9')
        return hex - '0';
    if (hex>='A' && hex <= 'F')
        return hex-'A'+10;
    if(hex>='a' && hex <= 'f')
        return hex-'a'+10;
    return 0;
}

int getSM2Key(SM2_KEY &sm2_key){
    int byteLen=0;
    const  char *pubXStr=PUBLIC_KEY_X;
    const  char *pubYStr=PUBLIC_KEY_Y;
    const  char *privateKeyStr=PRIAVTE_KEY;
    for(int i=SM2KEY_STR_LEN-1;i>=0;){
        sm2_key.public_key.x[byteLen]=charToInt(pubXStr[i])+charToInt(pubXStr[i-1])*16;
        sm2_key.public_key.y[byteLen]=charToInt(pubYStr[i])+charToInt(pubYStr[i-1])*16;
        sm2_key.private_key[byteLen]=charToInt(privateKeyStr[i])+charToInt(privateKeyStr[i-1])*16;
        byteLen++;
        i-=2;   
    }
    return 1;
}


int sm2Encrypt(const SM2_KEY &sm2_key, const std::string& plain_text,uint8_t *ciphertext){
    size_t outLength=0;
    if(sm2_encrypt(&sm2_key, (const uint8_t *)plain_text.c_str(), plain_text.length(), ciphertext, &outLength)==1){
        return outLength;
    }
    return 0;
}

int sm2Decrypt(const SM2_KEY &sm2_key, const uint8_t *ciphertext,size_t ciphertextLen,std::string &plainString){
    uint8_t plaintext[SM2_MAX_PLAINTEXT_SIZE]={0};
    size_t outLength=0;
    if(sm2_decrypt(&sm2_key, ciphertext, ciphertextLen, plaintext, &outLength)==1){
        plaintext[outLength]='\0';
        plainString=(const char *)plaintext;
        return outLength;
    }
    return outLength;
}

int main(int argc,char *argv[]){
const char *plainText="hellworld1234567890qwertyuiopasdfghjklzxcvbnm{113132424},fdsafads}";
    SM2_KEY sm2Key;
    getSM2Key(sm2Key);
    uint8_t ciphertext[SM2_MAX_CIPHERTEXT_SIZE]={0};
    size_t ciphertextLen; 
    std::string plainString;
    if((ciphertextLen=sm2Encrypt(sm2Key,plainText,ciphertext))>0){
        std::cout<<"miwenLen:"<<ciphertextLen<<"\n";
        for(int i=0;i<ciphertextLen;i++){
            printf("%02X ",ciphertext[i]);
            if((i+1)%30==0)
                printf("\n");
        }
        printf("\n");
        if(sm2Decrypt(sm2Key,ciphertext,ciphertextLen,plainString)>0){
            std::cout<<"mingwen:"<<plainString<<"\n";
        }
    }
    return 1;
}

编译测试程序

g++ -std=c++11 sm2Test.cpp \ 
-I/home/jdtf/Xlza/GmSSL-3.1.0/build/install/include \
-L/home/jdtf/Xlza/GmSSL-3.1.0/build/install/lib -lgmssl \
-o sm2Test

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值