Public Key encryption again

Rüdiger Sonderfeld cplusplushelp@gmx.net
Sat, 20 Jul 2002 16:00:44 +0200


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;
}