Mailing List Archive

code
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
Re: code [ In reply to ]
Hi,

salvatore:
> int encrypt_buffer(char *b_to_encrypt, char *b_encrypted )
> int decrypt_buffer(char *b_to_decrypt, char *b_decrypted, int n_bytes )
>
> 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?!
>
Hmmm... you might want to send a complete test program (i.e. one that
has main() ;-) which demonstratess the problem.

--
Matthias Urlichs | {M:U} IT Design @ m-u-it.de | smurf@smurf.noris.de
Re: code [ In reply to ]
I can't sent you all the program but i can make a short test program.Anyway the program i am working on is basically a chat,which encrypt
the test before it sends it(with his public key) and it decripts the message with the private.I also find out that the program works locally,but i
tested it on two different computers i get a SegFault.So i was wondering if those functions are correct... (?)

Thanks
Salvo

On Saturday 17 July 2004 23:26, you wrote:
> Hi,
>
> salvatore:
> > int encrypt_buffer(char *b_to_encrypt, char *b_encrypted )
> > int decrypt_buffer(char *b_to_decrypt, char *b_decrypted, int n_bytes )
> >
> > 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?!
> >
> Hmmm... you might want to send a complete test program (i.e. one that
> has main() ;-) which demonstratess the problem.
>
> --
> Matthias Urlichs | {M:U} IT Design @ m-u-it.de | smurf@smurf.noris.de
>
Re: code [ In reply to ]
On Sat, 17 Jul 2004 23:14:02 +0200, salvatore said:

> fprintf(stderr,"Error while converting from char to MPI\n");

fprintf(stderr,"Error while converting from char to MPI: %s\n",
gg_strerror (err) );

should give you a better insight what's going wrong.


Werner
Re: code [ In reply to ]
s/gg_strerror/gpg_strerror/
Re: code [ In reply to ]
On Monday 19 July 2004 09:04, Werner Koch wrote:
> s/gg_strerror/gpg_strerror/
>
>
Didn't work for me... but i tried gcry_sterror()
Thanks