Hi,Here is the implementation of triple DES on java card.
/*package handson_crypto_des;import javacard.framework.*;import javacard.security.*;import javacardx.crypto.*;/** * * Class HandsOn * */ public class HandsOnCryptoDES extends javacard.framework.Applet{ //globals DESKey deskey; Cipher cipherCBC; final short dataOffset = (short) ISO7816.OFFSET_CDATA; static byte[] TrippleDESKey = { (byte) 0x38, (byte) 0x12, (byte) 0xA4, (byte) 0x19, (byte) 0xC6, (byte) 0x3B, (byte) 0xE7, (byte) 0x71, (byte) 0x00, (byte) 0x12, (byte) 0x00, (byte) 0x19, (byte) 0x80, (byte) 0x3B, (byte) 0xE7, (byte) 0x71 , (byte) 0x01, (byte) 0x12, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x03, (byte) 0xE7, (byte) 0x71 }; //constructor private HandsOnCryptoDES(byte bArray[], short bOffset, byte bLength) { deskey = (DESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_DES, KeyBuilder.LENGTH_DES3_3KEY, false); cipherCBC = Cipher.getInstance(Cipher.ALG_DES_CBC_NOPAD, false); register(bArray, (short) (bOffset + 1), bArray[bOffset]); } //install public static void install(byte bArray[], short bOffset, byte bLength) { new HandsOnCryptoDES(bArray, bOffset, bLength); } public void process(APDU apdu) { byte[] buf = apdu.getBuffer(); if (selectingApplet()) { return; } doTrippeDES(apdu); } // EncDecDES method // Takes an input 24 byte message from the incomming APDU and Encrypts it using // a DES Key. It then Decrypts the cryptogram using the same DES Key. // Response APDU sends the message (24 bytes) plus Cryptogram (24 bytes) plus decrypted message (24 bytes)) private void doTrippeDES(APDU apdu) { byte a[] = apdu.getBuffer(); short incomingLength = (short) (apdu.setIncomingAndReceive()); if (incomingLength != 24) ISOException.throwIt(ISO7816.SW_WRONG_LENGTH); //perform encryption and append results in APDU Buffer a[] automatically //DESKey desKey = (DESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_DES, KeyBuilder.LENGTH_DES3_3KEY, false); deskey.setKey(TrippleDESKey, (short)0); cipherCBC.init(deskey, Cipher.MODE_ENCRYPT,new byte[]{0,0,0,0,0,0,0,0},(short)0,(short)8); cipherCBC.doFinal(a, (short) dataOffset, incomingLength, a, (short) (dataOffset + 24)); cipherCBC.init(deskey, Cipher.MODE_DECRYPT,new byte[]{0,0,0,0,0,0,0,0},(short)0,(short)8); cipherCBC.doFinal(a, (short) (dataOffset + 24), incomingLength, a, (short) (dataOffset + 48)); // Send results apdu.setOutgoing(); apdu.setOutgoingLength((short) 72); apdu.sendBytesLong(a, (short) dataOffset, (short) 72); }}
GP shell script and output
mode_211enable_traceestablish_contextcard_connect -readerNumber 2* reader name SCM Microsystems Inc. SCR33x USB Smart Card Reader 0select -AID 01020304050607Command --> 00A404000701020304050607Wrapped command --> 00A404000701020304050607Response <-- 9000send_apdu -sc 0 -APDU 00AA0000180102030405060708090a0b0c0d0e0f101112131415161718 Command --> 00AA0000180102030405060708090A0B0C0D0E0F101112131415161718Wrapped command --> 00AA0000180102030405060708090A0B0C0D0E0F101112131415161718Response <-- 0102030405060708090A0B0C0D0E0F101112131415161718*3CEFFF8CFEBAD556AED**98796A03111F15BBCD6ADAC61058A*0102030405060708090A0B0C0D0E0F1011121314151617189000card_disconnectrelease_context
Corresponding Java Representation
import java.security.GeneralSecurityException;import javax.crypto.Cipher;import javax.crypto.spec.SecretKeySpec; /** * * @author amit.deshpande */public class TripleDES { byte[] staticENC = HexConverter.decodeHex("3812A419C63BE77100120019803BE771011201010103E771".toCharArray()); private static SecretKeySpec getKeySpec(byte[] key) { if (key.length == 16) { byte[] key24 = new byte[24]; System.arraycopy(key, 0, key24, 0, 16); System.arraycopy(key, 0, key24, 16, 8); return new SecretKeySpec(key24, "DESede"); } else { return new SecretKeySpec(key, "DESede"); } } public static void main(String args[]) { TripleDES tripleDES = new TripleDES(); try { tripleDES.tripleDES(); } catch (Exception e) { } } protected void tripleDES() throws GeneralSecurityException { Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding"); byte[] data = HexConverter.decodeHex("0102030405060708090a0b0c0d0e0f101112131415161718".toCharArray()); java.security.spec.AlgorithmParameterSpec alSpec = new javax.crypto.spec.IvParameterSpec(new byte[]{(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00}); cipher.init(Cipher.ENCRYPT_MODE, getKeySpec(staticENC), alSpec); byte[] encryptedData = cipher.doFinal(data); System.out.println("Encrypted Data ::" + Util.printBytes(encryptedData)); }}
Java Output
Encrypted Data ::*3cefff8cfebad556aed98796a03111f15bbcd6adac61058a*