当前位置: 首页 > 工具软件 > wolfSSL > 使用案例 >

Wolfssl RSA解密

彭华皓
2023-12-01
#include "wolfssl/wolfcrypt/rsa.h"
#include <wolfssl/wolfcrypt/wc_port.h>
#include <stdio.h>
extern TaskHandle_t rsa_decrypt_Task_Handle;
int len =128;
byte out[128] = {142,82,162,181,22,125,59,109,49,194,185,79,96,23,45,251,137,
		      20, 53,228,51, 194,213,177,206,169,54,238,122,206,185,83,136,
		      207,80,64,199,179,1,114,14,60,219,242,185,245,168,238,231,
		      142,29,138,91,41,116,174,18,191,146,48,221,213,50,26,246,
		      242,177,75,59,237,48,229,105,20,174,7,112,136,121,134,181,
		      14,42,85,155,244,247,128,77,110,10,80,12,172,22,93,129,
		      146,249,59,0,57,114,119,206,10,205,171,196,114,216,59,86,
		      34,146,107,31,43,212,13,239,9,69,80,248,26,87,32};
byte PriKey[] = {48,130,2,92,2,1,0,2,129,129,0,222,137,20,25,217,228,50,196,202,63,143,233,102,89,117,226,238,164,36,167,196,1,139,24,143,124,118,119,139,182,191,199,10,206,204,244,21,232,155,138,240,146,29,180,123,73,231,169,122,0,34,92,27,115,123,100,187,166,68,10,166,208,210,131,37,185,242,222,236,110,65,112,211,162,191,9,164,222,228,185,76,104,235,171,29,97,12,111,126,158,158,89,7,72,23,83,155,50,219,237,135,93,122,46,79,214,3,56,204,254,237,115,50,169,169,240,122,100,88,57,45,27,189,101,101,251,183,53,2,3,1,0,1,2,129,128,11,50,214,194,30,70,239,143,27,166,107,53,145,162,250,221,186,168,163,247,149,83,134,142,107,218,21,148,26,129,205,208,212,104,103,195,248,190,69,229,142,9,172,89,2,195,75,87,41,254,139,188,26,221,138,222,28,219,87,29,58,27,212,28,17,104,186,110,211,101,138,241,64,132,12,202,80,5,68,107,124,16,23,193,153,132,69,98,253,219,80,6,14,248,55,124,163,135,247,82,77,254,90,41,93,138,155,252,60,43,49,131,204,228,7,139,194,170,46,180,48,158,146,83,218,103,110,25,2,65,0,233,148,141,146,89,109,236,219,35,145,165,99,173,156,161,16,223,186,192,55,36,201,224,206,122,237,27,70,146,145,6,25,162,53,251,58,7,253,30,226,38,245,11,95,180,115,102,136,136,242,110,248,52,175,7,147,20,207,228,117,145,244,194,195,2,65,0,243,229,34,227,124,118,74,110,155,160,233,86,186,71,169,232,9,170,156,246,31,145,51,114,207,90,30,54,21,214,179,179,194,234,86,91,55,74,156,54,75,8,231,43,212,111,195,58,228,184,231,118,199,246,184,241,232,68,42,183,226,14,14,167,2,64,68,101,33,71,55,52,126,228,115,247,211,203,234,44,48,229,117,253,131,7,34,152,146,97,35,145,134,41,22,5,173,25,152,107,226,18,78,0,138,40,130,107,194,86,213,201,236,190,18,11,154,254,198,190,113,163,89,182,190,24,199,18,58,109,2,65,0,178,255,241,170,41,247,155,75,48,119,89,169,232,79,158,88,137,119,169,121,77,211,192,129,187,194,245,55,86,177,219,243,203,211,55,11,253,57,138,10,162,233,102,216,153,50,105,131,184,200,40,64,218,35,174,187,4,111,187,54,119,247,63,51,2,64,60,232,98,115,149,59,37,75,240,146,46,31,0,247,30,79,177,249,118,123,17,134,126,243,187,66,178,46,94,68,55,94,28,26,175,20,249,134,174,121,101,45,70,212,1,80,213,19,112,89,113,233,201,169,88,37,92,30,194,249,64,149,167,6 };
void rsa_decrypt(void *para)
{
	RNG rng;
	RsaKey enc;
	int ret;
	word32 idx = 0;
	byte out2[128] = { 0 };
    int i;

	ret = wc_InitRsaKey(&enc, NULL);        //初始化RSA key1
	ret = wc_InitRng(&rng);

	if (ret != 0)
	{
		printf("wc_InitRsaKey ret %d\n",ret);
		goto end;
	}

	// 解密私钥,使用golang.x509.MarshalPKCS1PrivateKey生成
	ret = wc_RsaPrivateKeyDecode(PriKey, &idx, &enc, sizeof(PriKey));     //解析私钥,并更新enc

	if (ret != 0)
	{
		printf("wc_RsaPrivateKeyDecode ret %d\n",ret);
		goto end;
	}

	ret = wc_RsaSetRNG(&enc, &rng);
	if (ret < 0)
	{
		printf("wc_RsaSetRNG ret %d\n",ret);
		goto end;
	}
	ret = wc_RsaPrivateDecrypt(out, len,out2,128,&enc);
	if (ret <= 0)
	{
		printf("wc_RsaPrivateDecrypt ret %d\n",ret);
		goto end;
	}
	printf("decrypted:");
	for (i = 0; i < ret; i++)
	{
		printf("%d,",(int)out2[i]);
	}
	printf("decrypted over\n");
end:
    wc_FreeRsaKey(&enc);
	wc_FreeRng(&rng);
	vTaskDelete(rsa_decrypt_Task_Handle); //删除RSA任务;
}

 

 类似资料: