Mailing List Archive

Public Key encryption again
hi,
I modified the code from g10/encode.c but it doesn't work :(

int wcrypt_pkencrypt(enum pkalgo alg,unsigned char*sdata,size_t
ndata,unsigned char*to,size_t nto,pkkey pubkey)
{
GCRY_SEXP list,s_pkey,s_data,s_ciph;
GCRY_MPI* pkey=NULL;
GCRY_MPI data,resarr[2];
wcrypt_key_from_sexp(pkey,pubkey,"public-key", "ne"); /*this is the gpg
key_from_sexp function*/
gcry_mpi_scan(&data,GCRYMPI_FMT_USG/*??*/,sdata,&ndata);
/*is this correct?*/
switch(alg)
{
case GCRY_PK_ELG:
case GCRY_PK_ELG_E:
if(gcry_sexp_build ( &s_pkey, NULL,
"(public-key(elg(p%m)(g%m)(y%m)))",
pkey[0], pkey[1], pkey[2] ))
return 1;
break;
case GCRY_PK_RSA:
if(gcry_sexp_build( &s_pkey, NULL,
"(public-key(rsa(n%m)(e%m)))",
pkey[0] /* n */, pkey[1] /* e */ ))
return 1;
break;
default:
return 1;
}
/* put the data into a simple list */
if ( gcry_sexp_build( &s_data, NULL, "%m", data ) )
return 1;

/* pass it to libgcrypt */
gcry_pk_encrypt( &s_ciph, s_data, s_pkey );
gcry_sexp_release( s_data );
gcry_sexp_release( s_pkey );

/* extract the MPI values */
list = gcry_sexp_find_token( s_ciph, "a" , 0 );
if(!list)
return 1;
resarr[0] = gcry_sexp_nth_mpi( list, 1, 0 );
if(!resarr[0])
return 1;
gcry_sexp_release ( list );

list = gcry_sexp_find_token( s_ciph, "b" , 0 );
if(!list)
return 1;
resarr[1] = gcry_sexp_nth_mpi( list, 1, 0 );
if(!resarr[1])
return 1;
//!!! what is now the content of resarr[0] and resarr[1]? The encrypted text?
gcry_sexp_release ( list );
gcry_mpi_print(GCRYMPI_FMT_USG/*??*/,to,&nto,resarr[0]);
//!!! is this the right way to convert the mpi struct to text?
return 0;
}
Re: Public Key encryption again [ In reply to ]
On Sat Jul 20 2002; 16:00, Rüdiger Sonderfeld wrote:

> hi,
> I modified the code from g10/encode.c but it doesn't work :(

You can download OpenCDK because the interface there is more
general...(http://www.winpt.org/opencdk.html)


> int wcrypt_pkencrypt(enum pkalgo alg,unsigned char *sdata,size_t

"sdata" should be the session key, right?


> gcry_mpi_scan(&data,GCRYMPI_FMT_USG/*??*/,sdata,&ndata);
> /*is this correct?*/

This depends on the format of sdata. FMT_USG is used for general
MPI values that are *not* prefixed with the length in the buffer
like (OpenPGP/SSH).


> //!!! what is now the content of resarr[0] and resarr[1]?
> The encrypted text?

This also depends on your data field. Usually the data should
be the session key and the result is the encrypted session key!


> gcry_mpi_print(GCRYMPI_FMT_USG/*??*/,to,&nto,resarr[0]);
> //!!! is this the right way to convert the mpi struct to text?

To text? I guess you mean to binary data...but yes it is.


Timo
Re: Public Key encryption again [ In reply to ]
Am Samstag, 20. Juli 2002 16:31 schrieben Sie:
> On Sat Jul 20 2002; 16:00, Rüdiger Sonderfeld wrote:
> > I modified the code from g10/encode.c but it doesn't work :(
> You can download OpenCDK because the interface there is more
> general...(http://www.winpt.org/opencdk.html)

The link on this site is death and I can't find anything with google :(

> > int wcrypt_pkencrypt(enum pkalgo alg,unsigned char *sdata,size_t
> "sdata" should be the session key, right?

no, sdata is the data I want to encrypt. What do you mean with a session key?
Re: Public Key encryption again [ In reply to ]
On Sat Jul 20 2002; 21:28, Rüdiger Sonderfeld wrote:

> > > I modified the code from g10/encode.c but it doesn't work :(
> > You can download OpenCDK because the interface there is more
> > general...(http://www.winpt.org/opencdk.html)
>
> The link on this site is death and I can't find anything with google :(

http://www.winpt.org/opencdk.html is the URL and I tested it one
minute ago. It works. If you can't reach the site, try ftp.gnutls.org/pub
there you can download the tarball.


> > > int wcrypt_pkencrypt(enum pkalgo alg,unsigned char *sdata,size_t
> > "sdata" should be the session key, right?
>
> no, sdata is the data I want to encrypt. What do you mean with a
> session key?

Public key algorithms are not useful to encrypt large portions of data.
I guess you use a symmetric session key to encrypt the data and then
you protect the session key with the asymmetric key. If this is not
what you want, I don't understand what you're trying.


Timo