Implementing Triple DES on Java card

Source: Internet
Author: 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  Secret ey secret ey; cipher ciphercbc;  Final   Short Dataoffset = (Short  ) Iso7816.offset _ CDATA;  Static   Byte [] Tripplepolicey = {( 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) {secret ey = (Cipher ey) 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 ){  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 ){ 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  //  Cipher ey cipher ey = (Cipher ey) keybuilder. buildkey (keybuilder. type_des, keybuilder. length_des3_3key, false ); Secret ey. setkey (tripplepolicey ,( Short ) 0 ); Ciphercbc. INIT (Cipher ey, cipher. mode_encrypt,  New   Byte [] {0, 0, 0, 0, 0, 0 },( Short ) 0 ,( Short ) 8 ); Ciphercbc. dofinal (,(  Short ) Dataoffset, incominglength, ,( Short ) (Dataoffset + 24 ); Ciphercbc. INIT (Cipher ey, cipher. mode_decrypt,  New  Byte [] {0, 0, 0, 0, 0, 0 },( Short ) 0 ,( Short ) 8 ); Ciphercbc. dofinal (,(  Short ) (Dataoffset + 24), incominglength, ,( Short ) (Dataoffset + 48 ));  //  Send results  APDU. setoutgoing (); APDU. setoutgoinglength ((  Short ) 72 ); APDU. sendbyteslong (,( 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 0 Select -Aid 01020304050607 Command --> 00a404000701020304050607wrapped command --> 00a404000701020304050607response & Lt; -- 9000 Send_apdu -SC 0-APDU 00aa109180102030405060708090a0b0c0d0e0f101112131415161718 command --> 00aa10000180102030405060708090a0b0c0d0e0f101112131415161718wrapped command --> 00aa213180102030405060708090a0b0c0d0e0f101112131415161718response <-- 0102030405060708090a0b0c0d0e0f101112131415161718 * 3cefff8cfebad556aed ** 98796a03111f15bbcd6adac61058a * 0102030405060708090a0b0c0d0e0f101112131415161718900 0 Card_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 ("3812a419c63be7710036919803be771011201010103e771". 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 = 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 *

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.