📄 rsa_example.c
字号:
#include <windows.h>
#include <stdio.h>
#include <time.h>
#include "rsa.h"
#define NUM 1000
int ds_rsaGenerateKeyPair(rsaPublicKey *pbKey, rsaPrivateKey *pvKey, u32 bits, u32 useFermat4);
int ds_rsaPublicEncrypt (u8 *out, u32 *outLen, u8 *in, u32 inLen, rsaPublicKey *pbKey, u8 *random, u32 randomLen);
int ds_rsaPrivateEncrypt (u8 *out, u32 *outLen, u8 *in, u32 inLen, rsaPrivateKey *pvKey);
int ds_rsaPublicDecrypt (u8 *out, u32 *outLen, u8 *in, u32 inLen, rsaPublicKey *pbKey);
int ds_rsaPrivateDecrypt (u8 *out, u32 *outLen, u8 *in, u32 inLen, rsaPrivateKey *pvKey);
char plain[10]="abcdefghi";
char cipher[400],restore[400];
int plainlen=10,cipherlen=400,restorelen=400;
rsaPublicKey pb_key;
rsaPrivateKey pv_key;
char str[512];
char pad[512];
void rsa_example()
{
clock_t start,finish;
int i;
ds_rsaGenerateKeyPair(&pb_key, &pv_key, 1024, 0);
sprintf(str,"Plain:%s\r\n",plain);
start=clock();
for(i=0;i<NUM;i++)
ds_rsaPrivateEncrypt(cipher,&cipherlen,plain,plainlen,&pv_key);
finish=clock();
sprintf(str+strlen(str), "%.4f seconds spent in sign\r\n", (double)(finish - start) / CLOCKS_PER_SEC );
start=clock();
for(i=0;i<NUM;i++)
ds_rsaPublicDecrypt(restore,&restorelen,cipher,cipherlen,&pb_key);
finish=clock();
sprintf(str+strlen(str), "%.4f seconds spent in verify\r\n", (double)(finish - start) / CLOCKS_PER_SEC );
sprintf(str+strlen(str),"After private encrypt and public decrypt:%s\r\n",restore);
srand( (unsigned)time( NULL ) );
for(i=0;i<sizeof(pad);i++)
pad[i]=rand();
ds_rsaPublicEncrypt(cipher,&cipherlen,plain,plainlen,&pb_key,pad,sizeof(pad));
ds_rsaPrivateDecrypt(restore,&restore,cipher,&cipherlen,&pv_key);
sprintf(str+strlen(str),"\r\nAfter public encrypt and private decrypt:%s\r\n",restore);
MessageBox(NULL,str,"RSA test",MB_OK);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -