📄 rsatest.c
字号:
/* RSATEST.C - RSA Testing Application using RSAEURO cryptographic toolkit Copyright(C) 2002 by charry, charry@email.com.cn RSAEURO - RSA Library compatible with RSAREF(tm) 2.0. */#include <stdio.h>#include <string.h>
#include "r_random.c"#include "r_keygen.c"
#include "r_stdlib.c"
#include "prime.c"
#include "nn.c"
#include "rsa.c"/* Internal function prototypes */static R_RANDOM_STRUCT *InitRandomStruct PROTO_LIST((void));static void DoGenerateKeys PROTO_LIST((R_RANDOM_STRUCT *));static void DoOpenkeys PROTO_LIST((void));static void WriteKeypair PROTO_LIST((void));
//static int GetPublicKey PROTO_LIST((R_RSA_PUBLIC_KEY **));//static int GetPrivateKey PROTO_LIST((R_RSA_PRIVATE_KEY **));
static void Print PROTO_LIST((char *));R_RSA_PUBLIC_KEY PUBLIC_KEY;R_RSA_PRIVATE_KEY PRIVATE_KEY;int KEYPAIR_READY = 0;void main(void){ int cmd; int done = 0; R_RANDOM_STRUCT *randomStruct; randomStruct = InitRandomStruct(); /* setup random object */ printf("\nRSA Keypair Generation Application Using RSAEURO\n\n");
while(!done) { printf("1 - Generate a keypair\n"); printf("2 - Load keypair\n"); printf("3 - Quit\n"); printf("Enter choice: ");
scanf("%d",&cmd); switch(cmd) {
case 1: DoGenerateKeys(randomStruct); break; case 2: DoOpenkeys(); break; case 3: done = 1; break;
default: printf("ERROR: Unrecognized command.\n"); break; } }
R_RandomFinal(randomStruct); R_memset((POINTER)&PRIVATE_KEY, 0, sizeof(PRIVATE_KEY));}/* Initialize the random structure with all NULL seed bytes for test purposes. This will NOT produce a random stream, for a random stream one needs a random seed. See: R_RandomCreate */static R_RANDOM_STRUCT *InitRandomStruct(void){ static unsigned char seedByte = 0; unsigned int bytesNeeded; static R_RANDOM_STRUCT randomStruct; R_RandomInit(&randomStruct); /* Initialize with all zero seed bytes, which will not yield an actual random number output.
*/ while (1) { R_GetRandomBytesNeeded(&bytesNeeded, &randomStruct); if(bytesNeeded == 0) break; R_RandomUpdate(&randomStruct, &seedByte, 1); }
return(&randomStruct);}static void DoGenerateKeys(randomStruct)R_RANDOM_STRUCT *randomStruct;{ R_RSA_PROTO_KEY protoKey; int status;
protoKey.bits=1024; protoKey.useFermat4 = 1;
printf("Generating RSA Keypair(may take a long time)...\n"); status = R_GeneratePEMKeys(&PUBLIC_KEY, &PRIVATE_KEY, &protoKey, randomStruct);
if(status) { printf("ERROR: Cannot Generating keypair.\n"); return; }
printf("Public key and Private key are now ready to use.\n"); KEYPAIR_READY = 1; WriteKeypair();}static void DoOpenkeys(void){ FILE *file; char filename[256];
while(1) {
printf("Enter filename of file for the keypair: ");
scanf("%s",&filename);
if((file = fopen (filename, "rb")) != NULL)
/* successfully opened */
break;
printf("ERROR: Cannot open a file with that name.\n"); }
if((fread(&PUBLIC_KEY, sizeof(PUBLIC_KEY), 1, file)) != 1) { printf("ERROR: Cannot Read Public Key from File.\n"); } else { if((fread(&PRIVATE_KEY, sizeof(PRIVATE_KEY), 1, file)) != 1) printf("ERROR: Cannot Read Private Key from File.\n"); }
printf("Public key and private key are now ready to use.\n"); KEYPAIR_READY = 1; fclose (file);}static void WriteKeypair(void){ FILE *file; char filename[256]; while(1) { printf("Enter filename to save the keypair: "); scanf("%s",&filename);
if((file = fopen (filename, "wb")) != NULL)
/* successfully opened */
break;
printf("ERROR: Cannot open a file with that name.\n"); }
if((fwrite(&PUBLIC_KEY, sizeof(PUBLIC_KEY), 1, file)) != 1) { printf("ERROR: Cannot Write Public Key to File.\n"); } else { if((fwrite(&PRIVATE_KEY, sizeof(PRIVATE_KEY), 1, file)) != 1) printf("ERROR: Cannot Write Private Key to File.\n"); }
fclose(file);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -