Java tutorial
//package com.java2s; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.spec.InvalidKeySpecException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; public class Main { private static final String DEFAULT_KEY = "ZLXTYLSD"; private final static String DES = "DES"; private final static String MODE = "DES/ECB/PKCS5Padding"; public static byte[] decryptMessage(String key, byte[] input) throws Exception { if ((input == null) || (input.length <= 0)) { return null; } try { return doFinal(key, Cipher.DECRYPT_MODE, input); } catch (Exception e) { throw new Exception("201"); } } private static byte[] doFinal(String key, int opmode, byte[] input) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { key = checkNull(key) ? DEFAULT_KEY : key; if (checkNull(key)) { return null; } SecureRandom sr = new SecureRandom(); DESKeySpec dks = new DESKeySpec(key.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance(MODE); cipher.init(opmode, securekey, sr); return cipher.doFinal(input); } public static boolean checkNull(String input) { if (input == null || input.length() == 0) return true; else return false; } }