Main
 
Programs
Writings
Algorithms
About
Links
Contact
 
libgcrypt Example

  It's quick and dirty, but functional example of using "libgcrypt" (GPG/OpenPGP's encryption library) to do basic file encryption.

 

Download libgcrypt

Filename MD5
libgcrypt_example-1.0.tar.gz d20465cb54631e268f36592732f678f1
libgcrypt_example-1.0.zip cef11e8b6756ab27d3a9d163b54dd6c8

NOTE: None of the distributions come with a compiled executable.

I don't feel like learning how cygwin's copyrights work

 

To compile:
   Type "make" in the unpacked directory.  You will, of coarse, need "libgcrypt" and "libgpg-error" compiled and installed for this to work.

 

The hell with the rest of the documentation, I have some CRASS that needs to be played at high volume.  I'll show you how it works latter.

class CipherClass
 
CipherClass( int CipherAlgorithmParameter , int CipherModeParameter , int KeySizeParameter , int IV_SizeParameter )
 
int CreateIV( uint8** IV );
 
int CreateIV( uint8* IV );
 
int InitilizeCipher( uint8 const* Key , int KeySize , uint8 const* IV );
 
int EncryptBuffer( uint8* OutputBuffer , uint8 const * InputBuffer , size_t& BufferLength );
 
int EncryptBuffer( uint8* Buffer , size_t& BufferLength );
 
int DecryptBuffer( uint8* OutputBuffer , uint8 const * InputBuffer , size_t& BufferLength );
 
int DecryptBuffer( uint8* Buffer , size_t& BufferLength );
 
void CloseCipher();
   
class HashClass
 
HashClass( int HashAlgorithmParameter ) 
 
void Initilize();
 
void AddData( uint8* Buffer , size_t& Length );
 
void Flush();
 
uint8 const * GetHash() const
 
~HashClass();
   
class KeyExpantionClass
 
KeyExpantionClass( int HashAlgorithmParameter , int SaltLengthParameter , int KeySizeParameter )
 
int ExpandKey( char const * Passphrase , uint8 const* Salt , uint8** Key );
 
int CreateNewExpandedKey( char const * Passphrase , uint8** Salt , uint8** Key );
 
int CreateNewExpandedKey( char const * Passphrase , uint8* Salt , uint8** Key );


 
CipherClass::CipherClass( int CipherAlgorithmParameter , int CipherModeParameter , int KeySizeParameter , int IV_SizeParameter )

   The 'CipherClass' constructor initilizes the class with the algorithm 'CipherAlgorithmParameter' in the mode 'CipherModeParameter' with a key size of 'KeySizeParameter' bytes and an IV size of 'IV_SizeParameter'.  'CipherAlgorithmParameter' is any of the ciphers in the gcrypt library (see gcrypt documentation for more information).  Same goes for 'CipherModeParameter'.  Both key and IV sizes are specified in bytes. 

Example:

 
   CipherClass Cipher( 
     GCRY_CIPHER_AES256 , 
     GCRY_CIPHER_MODE_CFB , 
     ( 256 / 8 ) , 
     ( 128 / 8 ) );

   In this example, the CipherClass 'Cipher' is initilized using the algorithm AES with a 256-bit key size, running in CFB (Cipher Feed Back) mode.  The key size is 256 bits and the IV size is 128 bits.


 

int CipherClass::CreateIV( uint8** IV );

int CipherClass::CreateIV( uint8* IV );

    IV creation produces random data for the initilizing vector and places this in 'IV'.  There are two methods in which this function can be called.  'CreateIV' can allocate memory for the IV data, or already allocated memory can be used.
    The exception 'GeneralError' is thrown if for whatever reason, random data could not be produced.


 
int CipherClass::InitilizeCipher( uint8 const* Key , int KeySize , uint8 const* IV );

    Initilize the cipher using the key 'Key' of 'KeySize' length and the initlizing vector (IV) 'IV'.  This is required before any calls for encrypting or decrypting.

    The exception 'GeneralError' is thrown if for:

  • Unable to initilize cipher; due to bad algorithm or mode
  • Incorrect key size
  • Unable to set initilizing vector

 

int CipherClass::EncryptBuffer( uint8* OutputBuffer , uint8 const * InputBuffer , size_t& BufferLength );

int CipherClass::EncryptBuffer( uint8* Buffer , size_t& BufferLength );

    Encrypt a buffer of 8-bit data.  There are two methods by which this function can be called: Encrypting data to a new location, done by specifiying both an output buffer and an input buffer.  This method will not modify anything in the input buffer.  The second method will place the cipher text into the plain text buffer, with no need for a seprate output buffer.

    The exception 'GeneralError' is thrown if there are any problems trying to encrypt.


 

int CipherClass::DecryptBuffer( uint8* OutputBuffer , uint8 const * InputBuffer , size_t& BufferLength );

int CipherClass::DecryptBuffer( uint8* Buffer , size_t& BufferLength );

    Decrypt a buffer of 8-bit data.  There are two methods by which this function can be called: Decrypting data to a new location, done by specifiying both an output buffer and an input buffer.  This method will not modify anything in the input buffer.  The second method will place the plain text into the cipher text buffer, with no need for a seprate output buffer.

    The exception 'GeneralError' is thrown if there are any problems trying to decrypt.


 

void CipherClass::CloseCipher();

    This function could be called when the cipher is no longer needed.  It will cleanup internal storage, such as the cipher state.


 

HashClass::HashClass( int HashAlgorithmParameter ) 

    Hash class constructor.  'HashAlgorithmParameter' is any of the digest algorithms in the gcrypt library (see gcrypt documentation for more information).


 

void HashClass::Initilize();

    Start a new hash session.  This should be called before each hash generated.


 

void HashClass::AddData( uint8* Buffer , size_t& Length );

    Add data into hash. 


 

void HashClass::Flush();

    Clear hash state.  This is recomended after the hash has completed it's operation.  This function is also called by the destructor.


 

uint8 const * HashClass::GetHash() const

    Returns a pointer to the resulting hash.  This will finilize the hash and no further hashing will be posibal after this until the hash is reinitilized.


 

uint8 const * HashClass::~HashClass();

    Destructor.  Called 'Flush' and that's about it.


 

KeyExpantionClass::KeyExpantionClass( int HashAlgorithmParameter , int SaltLengthParameter , int KeySizeParameter )

    Key expantion constructor.  'HashAlgorithmParameter' is a message digest algrorithm (see gcrypt documentation for more information).  'SaltLengthParameter' is the length (in bytes) of "salt" value to add to the begining of the key.  'KeySizeParameter' is the desired length of the key (in bytes).  If the digest algorithm generates more then this, the key will be truncated.


 

int KeyExpantionClass::ExpandKey( char const * Passphrase , uint8 const* Salt , uint8** Key );

    Expand 'Passphrase' into a strong crypto key.  This is done by adding the salt value to the begining of the passphrase and running this through the selected digest algorithm.  The result will be placed in 'Key'.  Note that memory for 'Key' will be allocated.


 

int KeyExpantionClass::CreateNewExpandedKey( char const * Passphrase , uint8** Salt , uint8** Key );

int KeyExpantionClass::CreateNewExpandedKey( char const * Passphrase , uint8* Salt , uint8** Key );

    This function is simmilar to 'ExpandKey', except it will generate a random salt value.  This should be used to create encryption keys, where 'ExpandKey' is used to expand keys durring decryption

 


Copyright ©2001-2005, Punkroy. Bla, bla, bla...
=:(