java的3DES加密算法实践

java的3DES加密算法实践 最近开发需要用户JAVA的3DES加密算法,刚开始不是很清楚,找了一些网页,查了一些资料总算写好了,贴出来大家共享一下,可以直接运行的.. package com.zte.cuixudong.desede; import java.io.IOException; import java.io.UnsupportedEncodingException; 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; import javax.crypto.spec.DESedeKeySpec; import javax.crypto.spec.SecretKeySpec; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class Tool { private static final String Algorithm = "DESede"; private static final String ADD_STRING = "000000000000000000000000"; private static final int KEY_LENGTH = 24; public static void main(String[] args) { // 密钥 String key = "1234567890"; // 原文 String src = "abcdef"; System.out.println("原 文:" + src); Tool tool = new Tool(); // tool.test(src, key); // 密文 String desString = ""; desString = tool.encryCode(src, key); System.out.println("加密结果:" + desString); String newSrc = tool.deCode(desString, key); System.out.println("解密结果:" + newSrc); } // keybyte为加密密钥,长度为24字节 public void test(String src, String key) { // 密钥字节数组 byte[] keybytes = getKeyBytes(key); // 密钥 SecretKey deskey = getKey(keybytes); Cipher cipher; byte[] encbytes; String str = ""; String newString = ""; try { System.out.println("----------加密----------"); cipher = Cipher.getInstance(Algorithm); cipher.init(Cipher.ENCRYPT_MODE, deskey); encbytes = cipher.doFinal(src.getBytes()); BASE64Encoder baseE = new BASE64Encoder(); str = this.base64Encoder(encbytes); // str = baseE.encode(encbytes); System.out.println("密文:" + str); System.out.println("----------加密----------"); System.out.println("----------解密----------"); encbytes = base64Decoder(str); BASE64Decoder base64 = new BASE64Decoder(); // encbytes = base64.decodeBuffer(str); cipher.init(Cipher.DECRYPT_MODE, deskey); byte[] b = cipher.doFinal(encbytes); System.out.println("明文:" + new String(b)); System.out.println("----------解密----------"); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvalidKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalBlockSizeException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BadPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 加密算法 * * @param src * 原文 * @param key * 密钥 * @return encString 加密后的密文 */ public String encryCode(String src, String key) { // 加密 Cipher cipher; // 密钥字节数组 byte[] keybytes = getKeyBytes(key); // 密钥 SecretKey deskey = getKey(keybytes); // 密文数组 byte[] encbytes = new byte[1024]; // 加密后的密文 String encString = ""; try { cipher = Cipher.getInstance(Algorithm); cipher.init(Cipher.ENCRYPT_MODE, deskey); encbytes = cipher.doFinal(src.getBytes()); encString = base64Encoder(encbytes); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvalidKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalBlockSizeException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BadPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return encString; } /** * 解密算法 * * @param des * 密文 * @param key * 密钥 * @return */ public String deCode(String des, String key) { // 加密 Cipher cipher; // 密钥字节数组 byte[] keybytes = getKeyBytes(key); // 密钥 SecretKey deskey = getKey(keybytes); // 解密后的字符串 String desString = ""; byte[] desbytes = new byte[1024]; try { cipher = Cipher.getInstance(Algorithm); cipher.init(Cipher.DECRYPT_MODE, deskey); desbytes = cipher.doFinal(base64Decoder(des)); desString = new String(desbytes); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvalidKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalBlockSizeException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BadPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return desString; } // 检查密钥长充,如不足24位,则加零补齐 public byte[] getKeyBytes(String keyString) { byte[] b = null; int count = 0; count = KEY_LENGTH - keyString.length(); keyString += ADD_STRING.subSequence(0, count); b = keyString.getBytes(); return b; } // 获取密钥 public SecretKey getKey(byte[] keyBytes) { SecretKey deskey = new SecretKeySpec(keyBytes, Algorithm); return deskey; } // base64转码 public String base64Encoder(byte[] b) { BASE64Encoder base64 = new BASE64Encoder(); String str = base64.encode(b); return str; } // base64解码 public byte[] base64Decoder(String str) { BASE64Decoder base64 = new BASE64Decoder(); byte[] bytes = new byte[1024]; try { bytes = base64.decodeBuffer(str); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return bytes; } // 检查密钥格式,只能包含"[0-9],[a-z][A-Z]" public boolean checkKey(String key) { if (key == null || key.length() == 0 || "".equals(key)) { return false; } return true; } }

你可能感兴趣的:(java,算法,Security,sun)