当前位置:   article > 正文

c++国密算法SM2加密解密demo_vc++ sm2加解密

vc++ sm2加解密

c++国密算法SM2加密解密

一、代码

int main()
{
	int ret = 0;
	// added by junxue.zheng SM2加密
	std::string inputStr = "junxue"; //需要加密的字符串,如图所示,字符串中不可以有\0,否则后面的会加密失败
	unsigned char inputChar[65535];            //字符串转换为char类型
	inputStr.copy((char*)inputChar, inputStr.length(), 0); 
	int inputLen = inputStr.length();//未加密前数据的长度
	int a = sizeof(inputStr);

	unsigned char tempbuf[65535] = { 0 };//加密后的数据
	ret = Encrpt_SM2(inputChar, inputLen, tempbuf);//加密,秘钥封装在了Encrpt_SM2(内部)
	int tempLen = strlen((const char*)tempbuf);
	// 输出的就是发送给js解密的字符串,可以把以下打印出的字符发送给js解密验证
	printf("enc result:");
	printf("\n");
	int i = 0;
	for (i = 0; i < tempLen; i++)
	{
		printf("%02X", tempbuf[i]);
		//printf("%x", sendbuf[i]);
		if (1 == (i + 1) % 32 && i!=0)
			printf("\n");
	}
	printf("\n");

	// 解密nodejs发送的数据。若收到数据是string,则需要转换为char数组 ,若收到的数据是char,则直接引用。以下以收到的数据为string为例说明;
	// encData_str是收到js发送的数据;也可以是上面加密后的数据tempbuf
	std::string  encData_str = "04EBFC718E8D1798620432268E77FEB6415E2EDE0E073C0F4F640ECD2E149A73E858F9D81E5430A57B36DAAB8F950A3C64E6EE6A63094D99283AFF767E124DF089D494BCA98353CF1EB00DF753450763D85E90162610262E97C3E75FA335F75EB0176C8F5AA2";
	int encDataLen = encData_str.length();
	unsigned char encData_char[65535];
	unsigned char decData[65535];
	encData_str.copy((char*)encData_char, encDataLen, 0);
	ret = Decrypt_SM2(encData_char, encDataLen, decData);//解密,秘钥封装在了Decrypt_SM2(内部)

	system("pause");
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/388361
推荐阅读
相关标签
  

闽ICP备14008679号