3 Data Encryption Standard (DES) Algorithm in Java

Triple DES is another mode of DES operation.It takes three 64-bit keys, for an overall key length of 192 bits.
Triple Data Encryption Algorithm (TDEA or Triple DEA) symmetric-key block cipher,
which applies the Data Encryption Standard (DES) cipher algorithm three times to each data block.
The data is encrypted with the first key, decrypted with the second key, and finally encrypted again with the third key.

Triple DES runs three times slower than DES, but is much more secure if used properly
currently used in typical retail transaction environments.However ,this standard is usually only used
when Debit Card pin number is being processed .
DES is typically not used for Payment card Number and typical data  found on the 3 tracks of Magnetic Strip.

Remark:
DES is the most widely used symmetric algorithm in the world, despite claims that the key length is too short.
Ever since DES was first announced, controversy has raged about whether 56 bits is long enough to guarantee security.

Advantages:
  
    3DES is easy to implement (and accelerate) in both hardware and software.
    3DES is ubiquitous: most systems, libraries, and protocols include support for it.
Syntax

import java.security.spec.KeySpec;

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

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

/**
 * @author Aditya Mishra Helper class for encryption using specified algorithm
 *      
 */
public class EncryptionHelper {
    // private static final Logger LOGGER =
    // LogManager.getLogger(EncryptionHelper.class);
    private static final String UNICODE_FORMAT = "UTF8";
    private static final String DESEDE_ENCRYPTION_SCHEME = "DESede";
    private static SecretKeyFactory mySecretKeyFactory;
    private static KeySpec myKeySpec;
    private static Cipher cipher;
    private static String myEncryptionScheme;
    private static SecretKey secretKey;

    /**
     * @param key
     * @throws Exception
     */
    private static void init3DES(String key) throws Exception {
        myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME;
        byte[] rawkey = new byte[(int) key.length()];
        myKeySpec = new DESedeKeySpec(rawkey);
        mySecretKeyFactory = SecretKeyFactory.getInstance(myEncryptionScheme);
        cipher = Cipher.getInstance(myEncryptionScheme);
        secretKey = mySecretKeyFactory.generateSecret(myKeySpec);
    }

    /**
     * @param key
     * @param unencryptedString
     * @return
     */
    public static String encrypt3DES(String key, String unencryptedString) {
        String encryptedString = null;
        try {

            init3DES(key);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] plainText = unencryptedString.getBytes(UNICODE_FORMAT);
            byte[] encryptedText = cipher.doFinal(plainText);
            encryptedString = new String(Base64.encodeBase64(encryptedText));
            // encryptedString = new String((encryptedText));
        } catch (Exception e) {
            // LOGGER.error("encrypt3DES : exception occured : " + e);
            e.printStackTrace();
        }
        return encryptedString;
    }

    /**
     * @param key
     * @param encryptedString
     * @return
     */
    /*
     * public static String decrypt3DES(String key, String encryptedString) {
     * String decryptedText = null; try { init3DES(key);
     * cipher.init(Cipher.DECRYPT_MODE, secretKey); byte[] encryptedText =
     * Base64.decode(encryptedString); byte[] plainText =
     * cipher.doFinal(encryptedText); decryptedText = new String(plainText); }
     * catch (Exception e) { //
     * LOGGER.error("decrypt3DES : exception occured : " + e); } return
     * decryptedText; }
     */

    public static void main(String[] args) {
        EncryptionHelper encryptionHelper = new EncryptionHelper();
        System.out.println("::"
                + encryptionHelper.encrypt3DES(
                        "Your Merchant Key put here",
                        "card number"));
                   
    }
}

Comments