RE: Triple Des (des-ede2 or des-ede3) How?

334 views
Skip to first unread message

Donald_E_...@clarkeamerican.com

unread,
Sep 18, 2003, 2:39:02 PM9/18/03
to crypto...@eskimo.com

I've actually been trying to trace my way throught test.cpp and default.cpp
but I keep getting lost. My C++ skill is nonexistant. I have done this in
Java but now I need a DLL or Com object to finish a proof of concept. The
reason I wanted to convert the sample application I originally gave is that
it is the eaisest for me to understand and modify .

Don Bergstrom
Advisory System Analyst
210-697-6289


Wei Dai

unread,
Sep 18, 2003, 3:06:50 PM9/18/03
to crypto...@eskimo.com
Have you looked at this FAQ entry?

http://www.eskimo.com/~weidai/cgi-bin/fom.cgi?file=79

If you still can't figure it out, since you're not skilled in C++, you
might want to look into a C library such as OpenSSL instead.

Donald_E_...@clarkeamerican.com

unread,
Sep 18, 2003, 3:27:45 PM9/18/03
to crypto...@eskimo.com

Sorry, I wasn't finished with my thought.

My original example has the following:

CBC_Mode_ExternalCipher::Encryption cbcE(desE, iv);
StreamTransformationFilter encryptor(cbcE, NULL,
StreamTransformationFilter::NO_PADDING);
encryptor.Put(plaintext, sizeof(plaintext));
encryptor.MessageEnd();

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

If I know I need DES-EDE I know I have to do at least

CBC_Mode<DES_EDE2>::Encryption cbcE;

How do I define the key and IV for this particular cipher mode and how do I
declare the transformation mode.

Donald_E_...@clarkeamerican.com

unread,
Sep 22, 2003, 6:16:51 PM9/22/03
to crypto...@eskimo.com

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 all
Reply to author
Forward
0 new messages