Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Bi-directional card reading Reads up to two tracks of card data DES/TDES Encryption

  1. #1
    mageshge21 is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Question Bi-directional card reading Reads up to two tracks of card data DES/TDES Encryption

    Could you please provide me a sample for DUKPTdecrypting the data from card reader.

    References:
    DUKPT Madrock
    https://www.owasp.org/index.php/Usin...and_Decryption
    Derived unique key per transaction - Wikipedia, the free encyclopedia

    For Product:
    [Moderator edit: removed irrelevant link]
    Last edited by DarrylBurke; 05-31-2012 at 05:07 AM. Reason: Removed spammy link

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Bi-directional card reading Reads up to two tracks of card data DES/TDES Encrypti

    Quote Originally Posted by mageshge21 View Post
    Could you please provide me a sample
    This is a forum, not a code factory. Any member who has knowledge regarding the specifics of your question may help you to learn about it, but won't do your work for you.

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    mageshge21 is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: Bi-directional card reading Reads up to two tracks of card data DES/TDES Encrypti

    Thanks db,

    Here is the custom class which uses TDES encryption and decryption but i do no how to generate DUKPT key string and initialization vector.

    import java.security.MessageDigest;
    import java.security.spec.KeySpec;
    import java.util.Arrays;

    import javax.crypto.Cipher;
    import javax.crypto.SecretKey;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.DESedeKeySpec;
    import javax.crypto.spec.IvParameterSpec;

    import org.apache.commons.codec.binary.Base64;

    public class CryptUtil {
    private KeySpec keySpec;
    private SecretKey key;
    private IvParameterSpec iv;

    public CryptUtil(String keyString, String ivString) {
    try {
    final MessageDigest md = MessageDigest.getInstance("md5");
    final byte[] digestOfPassword = md.digest(Base64.decodeBase64(keyString.getBytes(" utf-8")));
    final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
    for (int j = 0, k = 16; j < 8;) {
    keyBytes[k++] = keyBytes[j++];
    }

    keySpec = new DESedeKeySpec(keyBytes);

    key = SecretKeyFactory.getInstance("DESede").generateSec ret(keySpec);

    iv = new IvParameterSpec(ivString.getBytes());
    } catch(Exception e) {
    e.printStackTrace();
    }
    }

    public String encrypt(String value) {
    try {
    Cipher ecipher = Cipher.getInstance("DESede/CBC/PKCS5Padding","SunJCE");
    ecipher.init(Cipher.ENCRYPT_MODE, key, iv);

    if(value==null)
    return null;

    // Encode the string into bytes using utf-8
    byte[] utf8 = value.getBytes("UTF8");

    // Encrypt
    byte[] enc = ecipher.doFinal(utf8);

    // Encode bytes to base64 to get a string
    return new String(Base64.encodeBase64(enc),"UTF-8");
    } catch (Exception e) {
    e.printStackTrace();
    }
    return null;
    }

    public String decrypt(String value) {
    try {
    Cipher dcipher = Cipher.getInstance("DESede/CBC/PKCS5Padding","SunJCE");
    dcipher.init(Cipher.DECRYPT_MODE, key, iv);

    if(value==null)
    return null;

    // Decode base64 to get bytes
    byte[] dec = Base64.decodeBase64(value.getBytes());

    // Decrypt
    byte[] utf8 = dcipher.doFinal(dec);

    // Decode using utf-8
    return new String(utf8, "UTF8");
    } catch (Exception e) {
    e.printStackTrace();
    }
    return null;
    }
    }

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Bi-directional card reading Reads up to two tracks of card data DES/TDES Encrypti

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    mageshge21 is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: Bi-directional card reading Reads up to two tracks of card data DES/TDES Encrypti

    For the implementation of TripleDES I am using bouncy castle.
    Java Code:
    import java.io.UnsupportedEncodingException;
    import java.security.*;
    import javax.crypto.*;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
    import org.bouncycastle.jce.provider.BouncyCastleProvider;
    import org.bouncycastle.util.encoders.Hex;
    
    public class TripleDesCipherFromDES {
    
        public byte[] desEncryptionECBCipher(String key, String text) throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException {
            Security.addProvider(new BouncyCastleProvider());        
            SecretKey keySpec = new SecretKeySpec(this.hexStringToByteArray(key), "DES");
            final Cipher encrypter = Cipher.getInstance("DES/ECB/ZeroBytePadding", "BC");
            encrypter.init(Cipher.ENCRYPT_MODE, keySpec);
            final byte[] plainTextBytes = text.getBytes("utf-8");
            final byte[] cipherText = encrypter.doFinal(plainTextBytes);
            return cipherText;
        }
    
        public String desDecriptionECBCipher(String key, byte[] cipherText) throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, UnsupportedEncodingException, BadPaddingException {
            Security.addProvider(new BouncyCastleProvider());
            SecretKey keySpec = new SecretKeySpec(this.hexStringToByteArray(key), "DES");
            final Cipher decrypter = Cipher.getInstance("DES/ECB/ZeroBytePadding", "BC");        
            decrypter.init(Cipher.DECRYPT_MODE, keySpec);
            final byte[] plainText = decrypter.doFinal(cipherText);
            return new String(plainText, "UTF-8");
        }
    
        public byte[] desEncryptionCBCCipher(String key, String text) throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {
            Security.addProvider(new BouncyCastleProvider());
            byte[] iv = new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
            IvParameterSpec ivSpec = new IvParameterSpec(iv);
    
    
            SecretKey keySpec = new SecretKeySpec(this.hexStringToByteArray(key), "DES");
            final Cipher encrypter = Cipher.getInstance("DES/CBC/ZeroBytePadding", "BC");
            encrypter.init(Cipher.ENCRYPT_MODE, keySpec,ivSpec);
            final byte[] plainTextBytes = text.getBytes("utf-8");
            final byte[] cipherText = encrypter.doFinal(plainTextBytes);
            return cipherText;
        }    
    
        public String desDecriptionCBCCipher(String key, byte[] cipherText) throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, UnsupportedEncodingException, BadPaddingException, InvalidAlgorithmParameterException {
            Security.addProvider(new BouncyCastleProvider());
            byte[] iv = new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
            IvParameterSpec ivSpec = new IvParameterSpec(iv);        
            SecretKey keySpec = new SecretKeySpec(this.hexStringToByteArray(key), "DES");
            final Cipher decrypter = Cipher.getInstance("DES/CBC/ZeroBytePadding", "BC");        
            decrypter.init(Cipher.DECRYPT_MODE, keySpec,ivSpec);
            final byte[] plainText = decrypter.doFinal(cipherText);
            return new String(plainText, "UTF-8");
        }
    
        public String asciiToHex(String ascii) {
            StringBuilder hex = new StringBuilder();
            for (int i = 0; i < ascii.length(); i++) {
                hex.append(Integer.toHexString(ascii.charAt(i)));
            }
            return hex.toString();
        }
    
        public byte[] hexStringToByteArray(String hexstring) {
            int i = 0;
            if (hexstring == null || hexstring.length() <= 0) {
                return null;
            }
            String stringvector = "0123456789ABCDEF";
            byte[] bytevector = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
            byte[] out = new byte[hexstring.length() / 2];
            while (i < hexstring.length() - 1) {
                byte ch = 0x00;
                //Convert high nibble charater to a hex byte
                ch = (byte) (ch | bytevector[stringvector.indexOf(hexstring.charAt(i))]);
                ch = (byte) (ch << 4); //move this to the high bit
    
                //Convert the low nibble to a hexbyte
                ch = (byte) (ch | bytevector[stringvector.indexOf(hexstring.charAt(i + 1))]); //next hex value
                out[i / 2] = ch;
                i++;
                i++;
            }
            return out;
        }    
    
        public String tdesedeECBCipher(String text, String doubleLenghtKey) throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException {
            //key definition
            String key1 = doubleLenghtKey.substring(0, 16);
            String key2 = doubleLenghtKey.substring(16, 32);
            String key3 = key1;
    
            byte[] codedText = new TripleDesCipherFromDES().desEncryptionECBCipher(key1, text);
            String decodedText = new TripleDesCipherFromDES().desDecriptionECBCipher(key2, codedText);
            byte[] codedTextFinal = new TripleDesCipherFromDES().desEncryptionECBCipher(key3, decodedText);
    
          return new String(Hex.encode(codedTextFinal));
        }
    
        public String tdesedeCBCCipher(String text, String doubleLenghtKey) throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {
            //key definition
            String key1 = doubleLenghtKey.substring(0, 16);
            String key2 = doubleLenghtKey.substring(16, 32);
            String key3 = key1;
    
            byte[] codedText = new TripleDesCipherFromDES().desEncryptionCBCCipher(key1, text);
            String decodedText = new TripleDesCipherFromDES().desDecriptionCBCCipher(key2, codedText);
            byte[] codedTextFinal = new TripleDesCipherFromDES().desEncryptionCBCCipher(key3, decodedText);
    
           return new String(Hex.encode(codedTextFinal));
        }    
    
        public static void main(String[] args) throws Exception {
            String text = "FFFF9876543210E0";        
            String key =  "0123456789ABCDEFFEDCBA9876543210";
    
            System.out.println(new TripleDesCipherFromDES().tdesedeECBCipher(text,key));
            System.out.println(new TripleDesCipherFromDES().tdesedeCBCCipher(text,key));
        }
    }
    AS you can see I try using ECB mode as the standard recommend and also CBC mode with a IV of 00000000 but neither of both approaches seems to work.

    Please I need an advice.

    Online Tools:
    Triple DES - TextOpus - all text tools You need!
    Online decrypt tool - Online tool

Similar Threads

  1. Card Game
    By abby0910 in forum New To Java
    Replies: 1
    Last Post: 07-24-2010, 12:38 AM
  2. Question Card Layout, Card Management
    By lrichil in forum AWT / Swing
    Replies: 1
    Last Post: 04-22-2010, 10:11 AM
  3. Card
    By hedonist in forum New To Java
    Replies: 3
    Last Post: 08-13-2009, 02:20 PM
  4. Card Layout
    By Gilbee in forum NetBeans
    Replies: 3
    Last Post: 03-03-2009, 09:37 PM
  5. Card program , need help thanks.
    By carlos123 in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 07:23 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •