Crypt++ Library 5.2.1

1. Introduction

Crypto++ Library is a free C++ class library of cryptographic schemes written by Wei Dai. You can download this toolkit at this link: http://www.eskimo.com/~weidai/cryptlib.html. This is a wonderful software, which includes most of popular Private key/Hash/Public key algorithms. You can perform any encryption/decryption of file or input strings by modifying a few codes of the program. The codes are orderly structured, well commented, and you can turn to Wei Dai's website for more help. However, it is not perfect and the only drawback I find until now is inefficiency.

2. Structure of Source Codes

There are several basic classes in the program, such as BufferTransformation, filter, file. But I won't talk about much about them, because what really counts to us is how to use this program.

For every type of algorithm, there are two files. One is *.h, and the other is *.cpp. In the following tutorial, I will take DES as example.

For a type of algorithm, maybe there are several branches. As to DES, there are

There is a class set for every branch.

For example, as to DES, the class is defined as:


class DES : public DES_Info, public BlockCipherDocumentation
{
class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_Info>, public RawDES
{
public:
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
};

public:
//! check DES key parity bits
static bool CheckKeyParityBits(const byte *key);
//! correct DES key parity bits
static void CorrectKeyParityBits(byte *key);

typedef BlockCipherFinal<ENCRYPTION, Base> Encryption;
typedef BlockCipherFinal<DECRYPTION, Base> Decryption;
};


At the end of des.h, there are two lines:

typedef DES::Encryption DESEncryption;
typedef DES::Decryption DESDecryption;
These are very important, because we will use this to initialize a structure.

In this program, we input 64 bit key and 64 bit IV. Note that although the input key for DES is 64 bits long, the actual key used by DES is only 56 bits in length. The least significant (right-most) bit in each byte is a parity bit, and should be set so that there are always an odd number of 1s in every byte. These parity bits are ignored, so only the seven most significant bits of each byte are used, resulting in a key length of 56 bits.

3. Test Script of Block Cipher

Add the head file  (eg.des.h) to dll.h first.

There is a test.cpp in the package. If you want to test DES with CBC mode, with setting key, iv, plaintext, you can add such source codes in main() function:


/* test des,add by zy */
else if (command == "des")
{
char plaintext[1024] = "1234567812345678", ciphertext[1024]="";
char detext[1024] = "";
const byte passPhrase[] = {0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40};
/* corresponding 56 bit key is 40,81,02,04,08,10,20 */

gettimeofday(&current_time, &tzp);
t1 = timeval2double(&current_time);
cout << "\nCurrent Time is "<<t1;

const byte iv2[] = {0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40};
CBC_Mode<DES>::Encryption encryption_DES_CBC;
encryption_DES_CBC.SetKeyWithIV(passPhrase, 8, iv2);//8x8bits = 64 bit key, 64bit IV

/* or we can change above two commands into: CBC_Mode<DES>::Encryption encryption_DES_CBC(passPhrase, 8, iv2);*/

encryption_DES_CBC.ProcessString((byte*)ciphertext, (byte*)plaintext, strlen(plaintext));

gettimeofday(&current_time, &tzp);
t2 = timeval2double(&current_time);
cout << "\nCurrent Time is "<<t2;
cout << "\nThe time difference is "<<t2-t1<<"\n";
cout << "\nThe cipher text is "<< ciphertext <<"\n";
กก


At the beginning of main() function, I add

struct timeval current_time;
struct timezone tzp;
double t1, t2;

At the beginning of test.cpp, I add:

#include <iostream>
#include <sys/time.h>

The first is for cout, and the second is for gettimeofday.

And such function:


double timeval2double(timeval *tp);
double timeval2double(timeval *tp)
{
return (double)tp->tv_sec + (double)tp->tv_usec/1000000.0;
}

4. Test Script of Block Cipher

Under Linux, enter the directory that the program is in. Type:

./cryptest des

It will show how much time it takes to encrypt.

กก

5. Execution

Under Linux, enter the directory that the program is in. Type:

./cryptest des

It will show how much time it takes to encrypt.