对称加密,这东东现在已经没啥用了,但据说可以帮助我了解加密思想,我还是把它弄出来了
新手心情好的话就看看吧
首先一个帮助类,找地址的
package zeke.security; //路径的控制 public class PathUtil { public static String getPath() { String path = PathUtil.class.getResource("/").getPath(); path=path.substring(1, path.length()); return path; } }
加密解密类,有两个,一个是依据系统自带算法生成key,还有一个是可以自己输入密码生成key
依据系统自带算法生成key
package zeke.security; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.KeyGenerator; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; /** * 对称加密密钥的生成,获取 * * @author Administrator */ public class SymmetricCipherWithKey { public static void createKey(String keyName) { try { /** * Key generators for generating keys suitable for the DES, Triple * DES, Blowfish, HMAC-MD5, and HMAC-SHA1 algorithms. * 大概的意思可以用这些算法生成keyGenerator实例 */ KeyGenerator key = KeyGenerator.getInstance("DES"); // 初始化key;如果客户端没有显式地初始化 KeyGenerator(通过调用 init 方法) // ,每个提供程序必须提供(和记录)默认初始化。 // DES-56位 // Blowfish-32~448位 // HMAC-MD5, and HMAC-SHA1-64位 key.init(56); // 获得密钥 SecretKey sk = key.generateKey(); saveObject(sk, PathUtil.getPath() + "/" + keyName); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } } /** * 序列化 * @param obj * @param fileURL */ public static void saveObject(Object obj, String fileURL) { try { FileOutputStream os = new FileOutputStream(fileURL); ObjectOutput oos = new ObjectOutputStream(os); oos.writeObject(obj); oos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } // 从序列化对象中获得key对象 public static SecretKey getKey(String keyName) { return (SecretKey) readerObject(PathUtil.getPath() + "/" + keyName); } /** *反 序列化 * @param fileURL * @return */ public static Object readerObject(String fileURL) { Object obj = null; try { FileInputStream fis = new FileInputStream(fileURL); ObjectInputStream is = new ObjectInputStream(fis); obj = is.readObject(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return obj; } // 对给定文本加密 public static byte[] enCipherContent(String content, String keyName) { byte[] b = null; try { Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, getKey(keyName)); b = cipher.doFinal(content.getBytes()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return b; } // 解密 public static String deCipherContent(byte[] b, String keyName) { String str = null; try { Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, getKey(keyName)); str = new String(cipher.doFinal(b)); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return str; } }
自己输入密码生成key
package zeke.security; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; 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; /** * 对称加密密钥的生成,获取 * * @author Administrator */ public class SymmetricCipherWithPassword { public static SecretKey createPassword(String password) { String pass=password; //需要判断pass长度,并需要长度大于8 if(pass.length()<8){ pass+="abcdefghijklmn"; } SecretKey sk=null; try { DESKeySpec desKeySpec=new DESKeySpec(pass.getBytes()); SecretKeyFactory skf=SecretKeyFactory.getInstance("DES"); sk=skf.generateSecret(desKeySpec); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeySpecException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } return sk; } /** * 序列化 * @param obj * @param fileURL */ public static void saveObject(Object obj, String fileURL) { try { FileOutputStream os = new FileOutputStream(fileURL); ObjectOutput oos = new ObjectOutputStream(os); oos.writeObject(obj); oos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** *反 序列化 * @param fileURL * @return */ public static Object readerObject(String fileURL) { Object obj = null; try { FileInputStream fis = new FileInputStream(fileURL); ObjectInputStream is = new ObjectInputStream(fis); obj = is.readObject(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return obj; } // 对给定文本加密 public static byte[] enCipherContent(String content, SecretKey key) { byte[] b = null; try { Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE,key ); b = cipher.doFinal(content.getBytes()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return b; } // 解密 public static String deCipherContent(byte[] b, SecretKey key) { String str = null; try { Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, key); str = new String(cipher.doFinal(b)); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return str; } }
测试类
package zeke.security; public class Test { public static void main(String[] args) { /* //................................带文件key...................................................... //生成key文件 //SymmetricCipherKey.createKey("key"); //加密获得加密后的文本 byte[] b=SymmetricCipherWithKey.enCipherContent("I like youfdf合法安家费哈斯勒", "key"); //序列化加密文件 SymmetricCipherWithKey.saveObject(b, PathUtil.getPath()+"/jiami.txt"); //System.out.println(new String(b)); //解密 byte[] s=(byte[]) SymmetricCipherWithKey.readerObject(PathUtil.getPath()+"/jiami.txt"); String str=SymmetricCipherWithKey.deCipherContent(s, "key"); System.out.println(str); */ //.........................................口令..................................................... byte[] bs=SymmetricCipherWithPassword.enCipherContent("dsadadassault斤斤计较", SymmetricCipherWithPassword.createPassword("11")); String bsde=SymmetricCipherWithPassword.deCipherContent(bs, SymmetricCipherWithPassword.createPassword("11")); System.out.println(new String(bsde)); } }
后面还有对流进行加密,什么的,有时间也把他们封装出来。上面两个对于算法,我都是用的"DES“还有其他的,只要你再生产key和加密解密对象时重新指定就可以了,就是换个算法去算他们生成key的依据。