[Botan-devel] need sample code for El Gamal Algorithm operations

Jack Lloyd lloyd at randombit.net
Wed Mar 21 08:14:32 EDT 2007


When an ElGamal key is created, you can specify which group (generator
and prime) should be used for that key. The example uses the 1024-bit
IPsec prime, to use the default 512-bit DSA group you can create a key
with:

   ElGamal_PrivateKey priv_key(DL_Group("dsa/jce/512"));

or create a new random group with
   DL_Group grp(DL_Group::DSA_Kosherizer, 512);
   ElGamal_PrivateKey priv_key(grp);

-Jack

On Tue, Mar 20, 2007 at 06:43:44PM -0700, run zhang wrote:
> Thanks very much. It works!
> According to the API reference, ElGamal can use
> DSA-512 group. Which magic should I play to make it
> run with this? 
> 
> --- Jack Lloyd <lloyd at randombit.net> wrote:
> 
> > 
> > Some sample code is attached. It does basic key
> > generation, encryption
> > and decryption. Currently you cannot export ElGamal
> > keys in any sort
> > of PKCS #8 structure as no OIDs have been defined
> > for it that I know
> > of (I'll rectify this in 1.7.0, I've been assigned
> > an OID arc by the
> > IANA and will be allocating nodes for RW, NR, and
> > ElGamal key
> > formats). Until then, if you want to do import
> > export you'll have to
> > do it manually with the accessors in
> > DL_Scheme_{PublicKey,PrivateKey}
> > 
> > -Jack
> > 
> > On Mon, Mar 19, 2007 at 08:42:38PM -0700, run zhang
> > wrote:
> > > Would someone post some sample code for El Gamal
> > key generation and enc/dec?  Thanks a lot. 
> > >    
> > > 
> > >  
> > > ---------------------------------
> > > Food fight? Enjoy some healthy debate
> > > in the Yahoo! Answers Food & Drink Q&A.
> > > _______________________________________________
> > > botan-devel mailing list
> > > botan-devel at randombit.net
> > >
> >
> http://www.randombit.net/mailman/listinfo/botan-devel
> > 
> > > #include <botan/botan.h> // for libraryinitalizer,
> > etc
> > #include <botan/elgamal.h> // for the key info
> > #include <botan/look_pk.h> // for get_pk_*
> > using namespace Botan;
> > 
> > #include <iostream>
> > #include <memory>
> > 
> > SecureVector<byte> do_encrypt(const SymmetricKey&
> > key,
> >                               const
> > ElGamal_PublicKey& elg_key)
> >    {
> >    std::auto_ptr<PK_Encryptor>
> > enc(get_pk_encryptor(elg_key, "PKCS1v15"));
> >    return enc->encrypt(key.bits_of());
> >    }
> > 
> > SecureVector<byte> do_decrypt(const
> > SecureVector<byte>& ciphertext,
> >                               const
> > ElGamal_PrivateKey& elg_key)
> >    {
> >    std::auto_ptr<PK_Decryptor>
> > dec(get_pk_decryptor(elg_key, "PKCS1v15"));
> >    return dec->decrypt(ciphertext);
> >    }
> > 
> > int main()
> >    {
> >    LibraryInitializer init;
> > 
> >    // generate a 2048 bit ElGamal key
> >    ElGamal_PrivateKey
> > priv_key(DL_Group("modp/ietf/1024"));
> >    ElGamal_PrivateKey pub_key = priv_key;
> > 
> >    SymmetricKey sym_key(16); // 128 bits, for, eg
> > AES-128
> > 
> >    SecureVector<byte> enc_key = do_encrypt(sym_key,
> > pub_key);
> >    SecureVector<byte> dec_key = do_decrypt(enc_key,
> > priv_key);
> > 
> >    if(dec_key == sym_key.bits_of()) // check that it
> > worked
> >       std::cout << "Decrypted plaintext matches
> > original\n";
> >    else
> >       std::cout << "Something went wrong, not
> > identical\n";
> >    return 0;
> >    }
> > > _______________________________________________
> > botan-devel mailing list
> > botan-devel at randombit.net
> >
> http://www.randombit.net/mailman/listinfo/botan-devel
> > 
> 
> 
> 
>  
> ____________________________________________________________________________________
> Food fight? Enjoy some healthy debate 
> in the Yahoo! Answers Food & Drink Q&A.
> http://answers.yahoo.com/dir/?link=list&sid=396545367
> _______________________________________________
> botan-devel mailing list
> botan-devel at randombit.net
> http://www.randombit.net/mailman/listinfo/botan-devel


More information about the botan-devel mailing list