extra chars added to the decrypted string ( RSA encryption)

Vinod Jacob vinod.jacob at gmail.com
Fri Aug 18 22:06:48 CEST 2006


Hi all,
    This is my second  mail to the list, thanks for the book
references, i could start coding with more confidence.  thanks Werner!
     Still i have to admit that i am still a newbie to the libgcrypt
world, but had to start coding because of the time constraints of the
project. Hope people would bear with me and would help me !

the Problem : I get some additional characters to the decrypted char buffer.

Say, i am trying to encrypt, a 256 byte buffer, with a 2048 rsa key.
when i print the decrypted data, there are some additional characters
added . But if the data buffer is less than 256, (say 255) , the
decrypted data matches with the initial data.


here is the code:
----------------------
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "./src/gcrypt.h"
int main()
{
gcry_ac_data_t data;
gcry_ac_key_t key1,key2;
gcry_ac_key_pair_t keypair;
gcry_ac_handle_t handle;
gcry_ac_key_spec_rsa_t rsa_spec;
gcry_error_t err;

char buf[256]={"123456 8123456 8123456 8123456 8123456 8123456 8123456
8123456 8123456 8123456 8123456 8123456 8123456 8123456 8123456
8123456 8123456 8123456 8123456 8123456 8123456 8123456 8123456
8123456 8123456 8123456 8123456 8123456 8123456 8123456 8123456
8123456 8"};
char newbuf[256];

gcry_control (GCRYCTL_DISABLE_SECMEM, 0);

size_t r,v;
rsa_spec.e = gcry_mpi_new (0);
gcry_mpi_set_ui (rsa_spec.e, 1) ;
err = gcry_ac_open (&handle, GCRY_AC_RSA, 0);
err = gcry_ac_key_pair_generate (handle, 2048, &rsa_spec, &keypair, NULL);

key1 = gcry_ac_key_pair_extract (keypair, GCRY_AC_KEY_SECRET);
key2 = gcry_ac_key_pair_extract (keypair, GCRY_AC_KEY_PUBLIC);

int err1= gcry_mpi_scan (&mpi1, GCRYMPI_FMT_USG, buf , 256, &r);
if (err1==0)
 {
  printf ("Scan Success\n");
 }
printf ("bytes Scanned = %d \n", r);
err = gcry_ac_data_encrypt (handle, GCRY_AC_FLAG_NO_BLINDING, key2,
mpi1, &data );
if (err==0)
 {
  printf ("Success\n");
 }
printf ("In Return : %d \n", r);
err = gcry_ac_data_decrypt (handle, GCRY_AC_FLAG_NO_BLINDING, key1,
&mpi2, data);
if (err==0)
 {
  printf ("Success\n");
 }
gcry_mpi_print (GCRYMPI_FMT_USG, (unsigned char *)newbuf ,256, &v, mpi2 );
printf ("bytes Printed = %d \n", v);
printf ("\n buffer: %s\n", newbuf );
gcry_ac_close(handle);
return 0;
}



here is the output printed on terminal if the buffer is actually 255
bytes: ( this works)
====================================================

Scan Success
bytes Scanned = 0
Success
In Return : 0
Success
bytes Printed = 256

 buffer: 123456 8123456 8123456 8123456 8123456 8123456 8123456
8123456 8123456 8123456 8123456 8123456 8123456 8123456 8123456
8123456 8123456 8123456 8123456 8123456 8123456 8123456 8123456
8123456 8123456 8123456 8123456 8123456 8123456 8123456 8123456
8123456


here is the output printed on terminal if the buffer is actually 256
bytes: (this doesn't Work)
=================================================================

Scan Success
bytes Scanned = 0
Success
In Return : 0
Success
bytes Printed = 256

 buffer: 123456 8123456 8123456 8123456 8123456 8123456 8123456
8123456 8123456 8123456 8123456 8123456 8123456 8123456 8123456
8123456 8123456 8123456 8123456 8123456 8123456 8123456 8123456
8123456 8123456 8123456 8123456 8123456 8123456 8123456 8123456
8123456 8123456 8123456 8123456 8123456 8123456 8123456 8123456
8123456 8123456 8123456 8123456 8123456 8123456 8123456 8123456
8123456 8123456 8123456 8123456 8123456 8123456 8123456 8123456
8123456 8123456 8123456 8123456 8123456 8123456 8123456 8123456
8123456 8xsÐ

See the extra characters in this case.

Any Pointers?

 thanks in advance,
    Vinod



More information about the Gnupg-users mailing list