Hi,everyone,
I modified "test.cpp", created a new command to encrypt a string using RSA.The
encrypted string is printed to the standard output.my new command is:
/cryptest re publickeyFile "String to be encrypted" RandomSeed
First, I ran the command:
/cryptest g 1024 privatekey.txt publickey.txt 123456
to generate RSA priv/public keys.
Then,I ran my new command:
/cryptest re publickey.txt "This is a test" 112233
It is succeeded.
Third,I wrote a new C++ program(test_EncryptRSAString.cpp,see P.S.) to run these two
command. When running this program,I can only generate RSA piv/public keys,but I can't
get the encrypted string.The error message is:
CryptoPP::Exception caught: BER decode error
Why? I can't find the reason.
I will be very appreciated if someone can help me.
Jue Wu
[EMAIL PROTECTED]
2004-09-07
P.S.:
OS: Redhat Linux 9.0, (kernel version: 2.4.20-8)
Gcc version: 3.2.2 20030222
version of Crypto++: 5.1
test_EncryptRSAString.cpp:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#define MAXNUM 2048+1
void GenerateRSAKey(){
char CMDPATH[256];
FILE *piprtn;
char lineread[2*MAXNUM];
int i=0,j=0;
// set up the command parameter
strcpy(CMDPATH,"./cryptest g 1024");
strcat(CMDPATH," ");//add a space
strcat(CMDPATH,"privkey.txt pubkey.txt");//second parameter is where to save
the key
strcat(CMDPATH," ");
strcat(CMDPATH, "123456");//forth parameter is RandomSeed
//run the command
piprtn=popen(CMDPATH,"r");
pclose(piprtn);
}
void RSAEncrypt(char tobeEncrypted[],char Encrypted[]){
char CMDPATH[256];
FILE *piprtn;
int i=0;
//set up the command parameter
strcpy(CMDPATH,"./cryptest re");
strcat(CMDPATH," ");//add a space
strcat(CMDPATH,"pubkey.txt");//second parameter is where to save the key
strcat(CMDPATH," ");
strcat(CMDPATH,tobeEncrypted);
strcat(CMDPATH," ");
strcat(CMDPATH,"112244");//forth parameter is seed
printf("s\n",CMDPATH);
//run the command
piprtn=popen(CMDPATH,"r");
//get the encrypted string from standard output
while(!feof(piprtn)){
Encrypted[i]=fgetc(piprtn);
}
pclose(piprtn);
Encrypted[--i]='\0';
printf("s\n",Encrypted);
}
int main()
{
GenerateRSAKey();
char toencrypt[]="I am a girl";
char encrypted[MAXNUM];
RSAEncrypt(toencrypt, encrypted);
exit(0);
}