Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Simple Triple DES Decryption [part 1]

843811Feb 2 2009 — edited Feb 3 2009
Hello,

I'm trying to decrypt a simple message encrypted with Triple DES CBC. I have they key, ciphertext and IV but an exception is being thrown and I'm not sure why. Here is my code:
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/*
 * DESDecryptor.java
 */
public class DESDecryptor {
    
    /** Session Parameters **/
    public static final String ALGORITHM = "DESede/CBC/NoPadding";
    public static final String CIPHERTEXT = "B08E14B80C19C60925A93DBC9250EB2EC1B6365DCDE42B9F1E44CD4D8BB246BA";
    public static final String KEY_TEXT = "23251942A5130D61940EE3DF251C34B649EAB602DF3DBA61";
    public static final String INITIALIZATION_VECTOR_TEXT = "C618A98F71D7834A";
    
    public static void main(String[] args) {
        // declare cryptographic members
        Cipher cipher = null;
        SecretKeySpec key = null;
        byte[] encryptedBytes, decryptedBytes = null;
        IvParameterSpec iv = null; 
        
        // create IV, instantiate Triple DES key and transform ciphertext into byte array
        iv = new IvParameterSpec(hexToBytes(INITIALIZATION_VECTOR_TEXT));
        key = new SecretKeySpec(hexToBytes(KEY_TEXT), ALGORITHM);
        encryptedBytes = hexToBytes(CIPHERTEXT);

        // only for debugging
        System.out.println("IV byte array is " + hexToBytes(INITIALIZATION_VECTOR_TEXT).length + " bytes long.");
        System.out.println("Key is " + hexToBytes(KEY_TEXT).length + " bytes long.");
        System.out.println("Ciphertext is " + hexToBytes(CIPHERTEXT).length + " bytes long.");
        
        // get a Triple DES cipher and decrypt byte array        
        try {
            cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, key, iv);
            decryptedBytes = cipher.doFinal(encryptedBytes);
        } catch (NoSuchAlgorithmException nsae) {System.out.println("ERROR::Algorithm Not Found\n"+nsae.getMessage());} 
        catch (NoSuchPaddingException nspe) {System.out.println("ERROR::Padding Not Valid\n"+nspe.getMessage());}
        catch (InvalidKeyException ike) {System.out.println("ERROR::Key is not valid\n"+ike.getMessage());}
        catch (IllegalBlockSizeException ibse) {System.out.println("ERROR::Illegal block size\n"+ibse.getMessage());}
        catch (BadPaddingException bpe) {System.out.println("ERROR::Incorrect padding\n"+bpe.getMessage());}
        catch (InvalidAlgorithmParameterException iape) {System.out.println("ERROR::Algorithm not valid\n"+iape.getMessage());}
        
        // output byte array as hex string
        System.out.println("Plaintext String is: " + decryptedBytes.toString());
    }    
    
    private static byte[] hexToBytes(String hex) {
        return hexToBytes(hex.toCharArray());
    }

    private static byte[] hexToBytes(char[] hex) {
        int length = hex.length / 2;
        byte[] raw = new byte[length];
        for (int i = 0; i < length; i++) {
            int high = Character.digit(hex[i * 2], 16);
            int low = Character.digit(hex[i * 2 + 1], 16);
            int value = (high << 4) | low;
            if (value > 127)
                value -= 256;
            raw[i] = (byte) value;
        }
        return raw;
    }
}
Here is the output:
IV byte array is 8 bytes long.
Key is 24 bytes long.
Ciphertext is 32 bytes long.
ERROR::Key is not valid
Wrong algorithm: DESede or TripleDES required
Exception in thread "main" java.lang.NullPointerException
	at DESDecryptor.main(DESDecryptor.java:54)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 3 2009
Added on Feb 2 2009
4 comments
575 views