2 way encryption with MD5 DES

Standard

Data encryption standard (DES) is a old way of encrypting data. It effectively encrypts data to a unreadable string; however it should be noted that it is not secure. The following code is for demonstration purposes only and should not be implemented as a security protocol. DES is a breakable algorithm smaller key, and block size than a advanced encryption standard (AES) algorithm.

Use case

A user is promptedĀ to enter some non sensitive data. For the sake of this example lets assume we have a requirement that the user telephone number must be encrypted. The user enters a phone number and theĀ encode method secures the data for storage in the database. Upon rendering back to the end user the data is translated using the decode method.

public class MD5andDES {

    // A user chosen password used for the PEKeySpec. Once set do not change or all previously encoded data will be lost
    private static final char[] PBEKeyPassword = "nviowefjklaasdlkjweklnvq".toCharArray();

    // Private salt for PBE algorithm
    private static final byte[] PBESALT = {(byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12, (byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12};

    /**
     * Takes in a string which is then salted with a given password used for the
     * 2 way encryption
     *
     * @param data
     * @return
     * @throws GeneralSecurityException
     * @throws UnsupportedEncodingException
     */
    public static String encode(String data) throws GeneralSecurityException, UnsupportedEncodingException {

        // Get algorithm to use
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");

        // Generates a secretKey object from the provided key specification
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PBEKeyPassword));

        // Returns a cipher object that implements the specified transformation
        Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");

        // Initializes cipher with public key
        pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(PBESALT, 20));

        // Returns the cipher encoded to a 64bit string (aka base64 encoded). getBytes() forms a new byte array
        return DatatypeConverter.printBase64Binary(pbeCipher.doFinal(data.getBytes("UTF-8")));
    }

    /**
     * Takes in encrypted data and uses the salt to decrypt it to its original
     * state
     *
     * @param data
     * @return
     * @throws GeneralSecurityException
     * @throws IOException
     */
    public static String decode(String data) throws GeneralSecurityException, IOException {

        // Get algorithm to use
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");

        // Generates a secretKey object from the provided key specification
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PBEKeyPassword));

        // Returns a cipher object that implements the specified transformation
        Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");

        // Initializes cipher with public key
        pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(PBESALT, 20));

        // Returns a string of the orignally encrypted data
        return new String(pbeCipher.doFinal(DatatypeConverter.parseBase64Binary((data))), "UTF-8");
    }

}

A complete working copy with test cases is available for checkout on Github.

Leave a Reply

Your email address will not be published.