package ***; import java.security.Key; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; /** * DES安全编码组件 */ public abstract class DESCoder extends EnBaseCoder { public static final String ALGORITHM = "DES"; /** * 转换密钥<br> * * @param key * @return * @throws Exception */ private static Key toKey(byte[] key) throws Exception { DESKeySpec dks = new DESKeySpec(key); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM); SecretKey secretKey = keyFactory.generateSecret(dks); return secretKey; } /** * 解密 * * @param data * @param key * @return * @throws Exception */ public static byte[] decrypt(byte[] data, String key) throws Exception { Key k = toKey(decryptBASE64(key)); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, k); return cipher.doFinal(data); } /** * 加密 * * @param data * @param key * @return * @throws Exception */ public static byte[] encrypt(byte[] data, String key) throws Exception { Key k = toKey(decryptBASE64(key)); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, k); return cipher.doFinal(data); } /** * 生成密钥 * * @return * @throws Exception */ public static String initKey() throws Exception { return initKey(null); } /** * 生成密钥 * * @param seed * @return * @throws Exception */ public static String initKey(String seed) throws Exception { SecureRandom secureRandom = null; if (seed != null) { secureRandom = new SecureRandom(decryptBASE64(seed)); } else { secureRandom = new SecureRandom(); } KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM); kg.init(secureRandom); SecretKey secretKey = kg.generateKey(); return encryptBASE64(secretKey.getEncoded()); } public static void main(String[] args) throws Exception { String inputStr = "DES"; String key = DESCoder.initKey(); System.err.println("原文:\t" + inputStr); System.err.println("密钥:\t" + key); byte[] inputData = inputStr.getBytes(); inputData = DESCoder.encrypt(inputData, key); System.out.println("加密后:\t" + DESCoder.encryptBASE64(inputData)); byte[] outputData = DESCoder.decrypt(inputData, key); String outputStr = new String(outputData); System.out.println("解密后:\t" + outputStr); } }
一步一步行走,做好每一件事情。