Mailing List Archive

newbie questions
Hi guys,

After reading ALL the messages in the mailing list since 1999 i was finally able to write some working code(almost working).I also had a look at the code in the test dir.Ok i'll go straight to the point... here is the code

int main()
{
gcry_sexp_t p_key, s_key, data_to_encrypt, data_encrypted, data_decrypted;
gcry_mpi_t mpi_string, mpi_decrypted;

char buffer[1024],d_buffer[1024];
size_t len_buffer, written;
int err;

printf("\nGenerating RSA key...\n");
sleep(1);
generate_rsa_keys(&p_key,&s_key);

printf("\nTo encrypt a buffer these are the steps ... \
\nchar -> mpi -> S-exp -> cripta \
\n");

printf("\nString to encrypt ---> ");
fgets(buffer, 1024, stdin);
len_buffer = strlen(buffer);

printf("\nChar -> mpi ...\t");

if( err = gcry_mpi_scan( &mpi_string,GCRYMPI_FMT_USG, buffer, len_buffer, NULL) ) /* From char to mpi */
{
fprintf(stderr,"Errore nella conversione da char a mpi\n");
exit(1);
}

sleep(1); printf("[ok]\n");

printf("\nmpi -> S-exp ...\t");

if( err = gcry_sexp_build( &data_to_encrypt, NULL, "(data(flags raw)(value %m))",mpi_string)) /* From mpi to S-exp */
{
fprintf(stderr,"Errore nella conversione da mpi a S-exp\n");
exit(1);
}

sleep(1); printf("[ok]\n");

gcry_mpi_release( mpi_string ); /* la mpi non serve piu */

printf("\nCripta la S-exp...\t");

if( err = gcry_pk_encrypt( &data_encrypted, data_to_encrypt ,p_key)) /* Encrpt the S-exp in data_to_crypt and put the result in data encrypted */
{
fprintf(stderr,"Errore durante la criptazione\n");
exit(1);
}

sleep(1); printf("[ok]\n");

gcry_sexp_release( data_to_encrypt ); /* non serve piu */

printf("\nAdesso facciamo il contrario... \
\ndecripta -> S-exp -> mpi -> char \
\n");

printf("\nDecripta la S-exp...\t");
sleep(1);

if( err = gcry_pk_decrypt( &data_decrypted, data_encrypted, s_key) ) /* decripta la S-exp */
{
fprintf(stderr,"Errore durante la decriptazione\n");
exit(1);
}

sleep(1); printf("[ok]\n");

gcry_sexp_release( data_encrypted ); /* non server piu */

printf("\nS-exp -> mpi ...\t");

if( (mpi_decrypted = gcry_sexp_nth_mpi( data_decrypted, 0 , 0 )) == NULL ) /* da S-exp a mpi */
{
fprintf(stderr,"Errore nella conversione da S-exp a mpi\n");
exit(1);
}

sleep(1); printf("[ok]\n");

gcry_sexp_release( data_decrypted );

printf("\nmpi -> char ...\t");

/* I think here is the problem */

if( err = gcry_mpi_print(GCRYMPI_FMT_USG, d_buffer, len_buffer, &written, mpi_decrypted )) /* da mpi a char */
{
fprintf(stderr,"Errore nella conversione da mpi a char \n");
exit(1);
}

sleep(1); printf("[ok]\n");

gcry_mpi_release( mpi_decrypted ); /* non serve piu */

printf("\n La stringa decriptata ----> %s",d_buffer); /* se coincide con buffer funziona! */

return 0;
}


The problem is that the string decrypted is the one i entered PLUS some weird characters!!Really don't where those
characters come from.What do i do wrong?!

Thanks in advance

Regards
Salvo