The FAQ was actually quite helpful. It just took me a while to understand
the source enough to understand the FAQ.

To close this thread out here is my simple example of the Triple Des
implementation.


-----------------------------------------------------------------------
#include "modes.h"
#include "des.h"
#include "hex.h"

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

#include <windows.h>

USING_NAMESPACE(CryptoPP)
USING_NAMESPACE(std)

int main()
{

const int MAX_PHRASE_LENGTH=250;

      //lenght Needs to be a multiple of 8
      byte plaintext[] = {'H','e','l','l','o',' ','W','o','r','l','d',' ','
',' ',' ',' '};
      byte * ciphertext;
      byte * result;

      HexEncoder hexEncoder3;
      unsigned int outputLength;

      const byte key[] = {
                  0x1c, 0xd5, 0x7f, 0x1f, 0xa7, 0x46, 0x1f, 0xe6,
                  0xc1, 0x26, 0x15, 0x54, 0x79, 0xfe, 0xcb, 0xe3,
                  0x89, 0x01, 0x1f, 0x16, 0x9e, 0x04, 0xd3, 0x61};

      const byte iv[]  = {0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef};

      // encrypt

      CBC_Mode<DES_EDE3>::Encryption ecbEncryption(key,
DES_EDE3::DEFAULT_KEYLENGTH, iv);
      StreamTransformationFilter encryptor(ecbEncryption, NULL,
StreamTransformationFilter::NO_PADDING);
      encryptor.Put(plaintext, sizeof(plaintext));
      encryptor.MessageEnd();

      outputLength = encryptor.MaxRetrievable();
      ciphertext = new byte[outputLength];
      encryptor.Get(ciphertext, outputLength);

      cout << "outputLength is: " << outputLength << endl;

      hexEncoder3.Put(ciphertext, outputLength);
      hexEncoder3.MessageEnd();
      byte pData3[outputLength*2];
      hexEncoder3.Get(pData3, outputLength*2);

      int j=0;
      cout << "Encrypted Data is: ";
      for (j=0; j<(outputLength*2); j++) {
             cout << pData3[j];
      }
      cout << endl;

      // now decrypt
      CBC_Mode<DES_EDE3>::Decryption ecbDecryption(key,
DES_EDE3::DEFAULT_KEYLENGTH, iv);
      StreamTransformationFilter decryptor(ecbDecryption, NULL,
StreamTransformationFilter::NO_PADDING);
      decryptor.Put(ciphertext, outputLength);
      decryptor.MessageEnd();

      outputLength = decryptor.MaxRetrievable();
      result = new byte[outputLength];
      decryptor.Get(result, outputLength);

      cout << "ciphertext size is " << sizeof(ciphertext) << endl;
      cout << "recovered plaintext is " << result << endl;


      delete [] ciphertext;
      delete [] result;
      return 0;
}

------------------------------------------------------------------------------------------------------------
And for anyone who is interested here is the Java equivalent

-------------------------------------------------------------------------------------------------------------

package com.donbergstrom.crypto;

import java.security.Key;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import com.donbergstrom.util.Hex;

public class DESede {

      private static String myKeyString =
            new String("1cd57f1fa7461fe6c126155479fecbe389011f169e04d361");

      private static String myIVString =
            new String("1234567890abcdef");

      public static void main(String[] args) throws Exception {
            if (args.length != 1) {
                  System.err.println("Usage: java SimpleExample text");
                  System.exit(1);
            }

            Security.addProvider(new com.sun.crypto.provider.SunJCE());

            doWork(args[0]);
      }



      public static byte[] encrypt(byte[] text, Key key, IvParameterSpec
iv)
            throws Exception {
            String returnText = "";

            //create a cipher using a key to initialize it
            Cipher cipher = Cipher.getInstance("DESede/CBC/NOPadding");
            cipher.init(Cipher.ENCRYPT_MODE, key, iv);

            //perform the actual encryption
            byte[] ciphertext = cipher.doFinal(text);

            return ciphertext;
      }


      public static byte[] decrypt(byte[] text, Key key, IvParameterSpec
iv)
            throws Exception {
            String returnText = "";

            //create a cipher using a key to initialize it
            Cipher cipher = Cipher.getInstance("DESede/CBC/NOPadding");
            cipher.init(Cipher.DECRYPT_MODE, key, iv);

            //perform the decryption
            byte[] decryptedText = cipher.doFinal(text);

            return decryptedText;
      }


      public static void doWork(String text) throws Exception {

            System.out.println("String: \n\t" + text + "\n");

            Key key = null;
            IvParameterSpec iv = null;

            //Use predefined key
            Key key = new SecretKeySpec(Hex.decode(myKeyString), "DESede");

            //Get the Initialization Vector
            iv = new IvParameterSpec(Hex.decode(myIVString));

            String padText = padString(text, 8);

            byte[] encryptedText = encrypt(padText.getBytes(), key, iv);
            System.out.println("Encrypted Text Hex Encoded: \n\t" +  new
String( Hex.encode( encryptedText ) )  + "\n" );


            byte[] decryptedText = decrypt(encryptedText, key, iv);
            System.out.println("Decrypted Text Hex Encoded: \n\t" +  new
String( decryptedText )  + "\n" );

      }

      public static String padString(String text, int size) throws
Exception {
            String myText = new String(text);

            int padSize = size - (text.length() % size);

            if (padSize < size)
                  for (int i=0; i<padSize; i++)
                        myText += " ";

            return myText;
      }
}

-------------------------------------------------------------------------------------------------------


Reply via email to