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

run zhang zhang4run at yahoo.com
Wed Mar 21 23:10:29 EDT 2007


Thanks very much Jack!
  Also I met problem in buliding in staic library when compiling with gcc -static, even with the builtin bench. I always get "ld returned 1 exit status". Can you give some hints?  

Jack Lloyd <lloyd at randombit.net> wrote:
  
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 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 // for libraryinitalizer,
> > etc
> > #include // for the key info
> > #include // for get_pk_*
> > using namespace Botan;
> > 
> > #include 
> > #include 
> > 
> > SecureVector do_encrypt(const SymmetricKey&
> > key,
> > const
> > ElGamal_PublicKey& elg_key)
> > {
> > std::auto_ptr

> > enc(get_pk_encryptor(elg_key, "PKCS1v15"));
> > return enc->encrypt(key.bits_of());
> > }
> > 
> > SecureVector do_decrypt(const
> > SecureVector& ciphertext,
> > const
> > ElGamal_PrivateKey& elg_key)
> > {
> > std::auto_ptr

> > 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 enc_key = do_encrypt(sym_key,
> > pub_key);
> > SecureVector 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
_______________________________________________
botan-devel mailing list
botan-devel at randombit.net
http://www.randombit.net/mailman/listinfo/botan-devel


 
---------------------------------
Be a PS3 game guru.
Get your game face on with the latest PS3 news and previews at Yahoo! Games.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.randombit.net/pipermail/botan-devel/attachments/20070321/6df06fde/attachment.htm


More information about the botan-devel mailing list