[Botan-devel] need sample code for El Gamal Algorithm operations
Jack Lloyd
lloyd at randombit.net
Tue Mar 20 15:34:19 EDT 2007
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
-------------- next part --------------
#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;
}
More information about the botan-devel
mailing list