一、示例
CommonUtil.java
package com.ray.test.des; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Arrays; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class CommonUtil { public static void main(String[] args) { byte[] before=new byte[] {80, 75, 3, 4, 10, 60, 82, -83, 68, 8, 0, 28, 0, 80, 97, 121, 108, 108}; String mes=getStringFromBytes(before); byte[] after=getBytesFromString( mes); System.out.println("before= "+Arrays.toString(before)); System.out.println("after = "+Arrays.toString(after)); } public static String getStringFromBytes( byte[] before ) { BASE64Encoder enc=new BASE64Encoder(); String mes=enc.encodeBuffer(before); //使用BASE64编码 return mes; } public static byte[] getBytesFromString( String mes) { BASE64Decoder dec=new BASE64Decoder(); byte[]after=null; try { after =dec.decodeBuffer(mes);//使用BASE64解码 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return after; } }
DESTest.java
package com.ray.test.des; import java.io.IOException; import java.security.SecureRandom; import java.util.Arrays; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; /** * DESTest.java * * @author Techzero * @Email techzero@163.com * @Time 2013-12-12 下午2:22:58 */ public class DESTest { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { String content = "wzm"; // 密码长度必须是8的倍数 String password = "12345678"; System.out.println("密 钥:" + password); System.out.println("加密前:" + content); //1.加密 byte[] result = encrypt(content, password); System.out.println("result length:" + result.length); System.out.println("加密后:" + Arrays.toString(result)); //2.解密 String decryResult = decrypt(result, password); System.out.println("解密后:" + decryResult); //3.将字节转String String mes=CommonUtil.getStringFromBytes(result); System.out.println("mes:" + mes); //4.将String转字节 byte[] after=CommonUtil.getBytesFromString(mes); String decryResultString =decrypt(after, password); System.out.println("decryResultString解密后:" + decryResultString); } /** * 加密 * * @param content * 待加密内容 * @param key * 加密的密钥 * @return */ public static byte[] encrypt(String content, String key) { try { SecureRandom random = new SecureRandom(); DESKeySpec desKey = new DESKeySpec(key.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(desKey); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, securekey, random); byte[] result = cipher.doFinal(content.getBytes()); return result; } catch (Throwable e) { e.printStackTrace(); } return null; } /** * 解密 * * @param content * 待解密内容 * @param key * 解密的密钥 * @return */ public static String decrypt(byte[] content, String key) { try { SecureRandom random = new SecureRandom(); DESKeySpec desKey = new DESKeySpec(key.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(desKey); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, securekey, random); byte[] result = cipher.doFinal(content); return new String(result); } catch (Throwable e) { e.printStackTrace(); } return null; } }
二、参考资料