Implementing Triple DES on Java Card

來源:互聯網
上載者:User

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*

 

 

 

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.