code
salvatore
salvatore_uni at tiscali.it
Sat Jul 17 23:14:02 CEST 2004
I'm trying to figure out what is wrong with my decrypt_function,could you have a look at it..
/* Encrypt the buffer in b_to_encrypt */
int encrypt_buffer(char *b_to_encrypt, char *b_encrypted )
{
gcry_sexp_t data_to_encrypt, data_encrypted;
gcry_mpi_t mpi_string;
int err;
/* i don't really know if it's necessary */
memset( b_encrypted,0,MAX_CHARS );
/* Char ---> MPI */
if( (err = gcry_mpi_scan( &mpi_string,GCRYMPI_FMT_USG, b_to_encrypt, MAX_CHARS , NULL)) )
{
fprintf(stderr,"Error while converting from char to MPI\n");
return 0;
}
/* MPI -----> S-exp */
if( (err = gcry_sexp_build( &data_to_encrypt, NULL, "(data(flags raw)(value %m))",mpi_string)) )
{
fprintf(stderr,"Error while converting from mpi to S-exp\n");
return 0;
}
gcry_mpi_release( mpi_string );
/* Encrypt the S-exp with his public key */
if( (err = gcry_pk_encrypt( &data_encrypted, data_to_encrypt ,key.his_public_key)) )
{
fprintf(stderr,"Error while encrypting\n");
return 0;
}
gcry_sexp_release( data_to_encrypt );
/* Convert encrypted S-exp to a valid format so i can send it trought internet (or save in a file) */
gcry_sexp_sprint(data_encrypted, GCRYSEXP_FMT_CANON, b_encrypted, MAX_CHARS );
gcry_sexp_release( data_encrypted );
return 1;
}
/* Decrypt buffer in b_to_decrypt */
int decrypt_buffer(char *b_to_decrypt, char *b_decrypted, int n_bytes )
{
gcry_sexp_t data_to_decrypt, data_decrypted;
gcry_mpi_t mpi_decrypted;
size_t written;
int rc, error_code;
/* Char ----> S-exp */
if( (rc = gcry_sexp_sscan(&data_to_decrypt, &error_code, b_to_decrypt, n_bytes )) )
{
fprintf(stderr,"Error while converting from char to S-exp ");
return 0;
}
/* Decrypt with my private key */
if( (rc = gcry_pk_decrypt( &data_decrypted, data_to_decrypt, key.my_private_key)) )
{
fprintf(stderr,"Error while decrypting \n");
return 0;
}
gcry_sexp_release( data_to_decrypt );
/* I think here is the error,because i don't understand how this function work */
if( (mpi_decrypted = gcry_sexp_nth_mpi( data_decrypted, 0 , GCRYMPI_FMT_USG )) == NULL )
{
fprintf(stderr,"Error while converting from S-exp to mpi \n");
return 0;
}
gcry_sexp_release( data_decrypted );
/* Convert the decrypted mpi to the original string */
if( (rc = gcry_mpi_print(GCRYMPI_FMT_USG, b_decrypted, MAX_CHARS , &written, mpi_decrypted ) ))
{
fprintf(stderr,"Error while converting from mpi to char \n");
return 0;
}
gcry_mpi_release( mpi_decrypted );
return 1;
}
I don't get any error from neither of these functions but when i display the decrypted buffer it's still encrypted.
Can anybody give me a hint?!
Regards
Salvo
More information about the Gcrypt-devel
mailing list