Hello,

I have a problem for which I found no real solution in the manual or the 
list archives.
The basic idea is to encrypt data using RSA_private_encrypt and retrieve it 
using RSA_public_decrypt. For RSA_private_encrypt, I set flen to RSA_size() 
to encrypt just one block and decrypt it later. If there is more data, it 
is processed blockwise in a loop.
RSA_NO_PADDING is used (yeah, I know one shouldn´t do that). For most 
blocks, the decryption works fine. For some block it just doesn´t work. I 
don´t get any error reports, the decrypted data just isn´t what it should be.

Below you find an excerpt from the code (some NULL checkings and the like 
omitted). What am I doing wrong? Once again: There is no overlapping memory 
or the like, the process functions properly in most cases, but in some 
cases (it seems to be depending on the data actually!) the routine fails, 
either at encryption or decryption (or even both?).

Ciao
Jan
------------------------------------------------------------
// get key
rsaStruct = PEM_read_RSAPrivateKey(fp, NULL, NULL, password);
// srcLen is original length given as a function parameter
unsigned long   destLen = srcLen;

// Now pad to correct block size, resulting in a destination length of
// N*RSA_size()
unsigned long blocklength = RSA_size(rsaStruct);
destLen = (((destLen - 1)/ blocklength) + 1) * blocklength;

// create destination array
dest = new unsigned char[destLen];
memset(dest, 0, destLen);

// create source array
unsigned char   *tmpSrc = new unsigned char[destLen];
memset(tmpSrc, 0, destLen);

// copy original source data, result is an array of correct length containing
// the source and trailing zeroes
memcpy(tmpSrc, src, srcLen);

// now encrypt blockwise
for (unsigned long i = 0; i < destLen; i+= blocklength) {
         if (blocklength!=RSA_private_encrypt(blocklength, (tmpSrc+i), 
(dest+i), rsaStruct, RSA_NO_PADDING)) {
                 printf("RSA Encrpytion Error.\n");
                 delete [] dest;
                 delete [] tmpSrc;
                 return 0;
         }
}

// and now decrypt the data again
// array to contain the decrypted data
unsigned char *tmpDest = new unsigned char[destLen];
for (i = 0; i < destLen; i+= blocklength) {
         if (blocklength!=RSA_public_decrypt(blocklength, (dest+i), 
(tmpDest+i), rsaStruct, RSA_NO_PADDING)) {
                 printf("RSA Decryption Error.\n");
                 delete [] dest;
                 delete [] tmpDest;
                 delete [] tmpSrc;
                 return 0;
         }
}
------------------------------------------------------------

--
Jan Zoellner - VidSoft GmbH
eMail: [EMAIL PROTECTED] - Tel: ++49 351 435 34 17
WWW:   http://www.vidsoft.de

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [EMAIL PROTECTED]
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to