[Botan-devel] DES and Tripple DES example needed

Jack Lloyd lloyd at randombit.net
Thu Mar 1 09:29:53 EST 2007


The particular problem you're seeing is two-fold: you must construct a
LibraryInitalizer object or a number of operations (such as memory
allocation, which the DES object will need) fail. When those
operations fail, they throw an exception. However you don't have
toplevel try {} catch(...) block in your main(), so instead the C++
runtime aborts the program (through std::terminate()). Some runtimes
print a helpful message inside terminate() telling you what happened,
most just call abort().  With a try/catch and a printf you should
start seeing a message about how the library was not initialized
before use.

Following is an example that may help (which I have not compiled, so
there may be minor problems, but it should at least help point you in
the right direction in the documentation). To use 3DES instead of DES,
all that is needed is to replace the strings "DES/CBC" with
"TripleDES/CBC" and increase the sizes of all the keys to 192 bits/24
bytes. Unless you need block level processing for some reason, it's a
lot more convenient and simple to use Pipe and let it handle the
buffering and such for you.

Hope this helps,
  Jack

#include <botan/botan.h>
#include <botan/des.h>
#include <stdio.h>

using namespace Botan;

SecureVector<byte> encrypt(const char*, const SymmetricKey&);
std::string decrypt(const SecureVector<byte>&, const SymmetricKey&);

int main()
   {
   LibraryInitializer init; // needed or nothing will work

   const char* msg = "Hello world\n"; // can be any length, we don't care
   SymmetricKey key("ABCDEF0123456789"); // 64 bits

   // SymmetricKey key(8); // a random 64-bit key

   SecureVector<byte> ciphertext = encrypt(msg, key);

   std::string plaintext = decrypt(ciphertext, key);

   printf("%s\n", plaintext.c_str());
   }

SecureVector<byte> encrypt(const char* msg, const SymmetricKey& key)
   {
   Pipe pipe(get_cipher("DES/CBC", key, ENCRYPTION));
   pipe.start_msg();
   pipe.write(msg, strlen(msg));
   pipe.end_msg();

   return pipe.read_all();
   }

std::string decrypt(const SecureVector<byte>& msg,
                    const SymmetricKey& key)
   {
   Pipe pipe(get_cipher("DES/CBC", key, DECRYPTION));
   pipe.start_msg();
   pipe.write(msg);
   pipe.end_msg();

   return pipe.read_all_as_string();
   }


On Thu, Mar 01, 2007 at 01:58:23PM +0000, Brian Mitchell wrote:
> Hi,
> 
> ????????I'm trying to use Botan to do a very simple DES calculation. In 
> reality it 
> will need to be Tripple DES, but I can't even get a straight forward DES 
> calculation working yet. Has anybody got a very simple, make a key, make a 
> buffer and encrypt it program?
> 
> I have appended my simple program, but that always aborts when I construct 
> DES.
> If anyone has information on how to use Tripple DES that would be very nice.
> 
> Thanks
> 
> Brian.
> 
> 
> #include <botan/des.h>
> 
> using namespace Botan;
> 
> int main()
> {
> ????????byte ?inblock[80] ? ? ?= {"11111111"};
> ????????byte ?outblock[80];
> ????????byte ?origblock[80];
> 
> ????????byte ?key[80] = {"11111111"};
> 
> ????????DES des;
> ????????u32bit kl = 8;??
> ????????des.set_key(key,kl);
> ????????des.encrypt(inblock, outblock);
> ????????des.decrypt(outblock, origblock);
> 
> ????????return 1;
> }
> 
> _______________________________________________
> 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