对称加密算法
package com.kaishengit.security; 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; import javax.crypto.spec.DESedeKeySpec; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.PBEParameterSpec; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Hex; /** * 对称加密帮助类 * @author kdj * 创建时间:2018年4月12日 */ public class SymmetricUtil { public static void main(String[] args) { try { String pas = "要加密的字符串!!"; //DES String keyVal = getKeyStrByType("DES"); //加密 String desStr = DESEncode(pas, keyVal); System.out.println("DES encode:"+desStr); //解密 System.out.println("DES decode:"+DESDecode(desStr, keyVal)); //DES3 keyVal = getKeyStrByType("DESede"); //加密 desStr = DES3Encode(pas, keyVal); System.out.println("DES3 encode:"+desStr); //解密 System.out.println("DES3 decode:"+DES3Decode(desStr, keyVal)); //AES keyVal = getKeyStrByType("AES"); //加密 desStr = AESEncode(pas, keyVal); System.out.println("AES encode:"+desStr); //解密 System.out.println("AES decode:"+AESDecode(desStr, keyVal)); //PBE SecureRandom random = new SecureRandom(); byte[] salt = random.generateSeed(8); String saltStr = Hex.encodeHexString(salt); String password = "qwertyuiop"; //加密 desStr = PBEEncode(pas, saltStr, password); System.out.println("PBE encode:"+desStr); //解密 System.out.println("PBE decode:"+PBEDecode(desStr, saltStr, password)); } catch (Exception e) { e.printStackTrace(); } } /** * DES加密 * @param pas * @param keyVal * @return * @throws Exception */ public static String DESEncode(String pas,String keyVal) throws Exception{ byte[] keyArr = Hex.decodeHex(keyVal); //key的转换 DESKeySpec desKeySpec = new DESKeySpec(keyArr); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES"); Key converSecretKey = secretKeyFactory.generateSecret(desKeySpec); //进行加密 //加密的格式 Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, converSecretKey); byte[] result = cipher.doFinal(pas.getBytes()); return Hex.encodeHexString(result); } /** * DES解密 * @param pas * @param keyVal * @return * @throws Exception */ public static String DESDecode(String pas,String keyVal) throws Exception{ //解密 byte[] keyArr = Hex.decodeHex(keyVal); //key的转换 DESKeySpec desKeySpec = new DESKeySpec(keyArr); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES"); Key converSecretKey = secretKeyFactory.generateSecret(desKeySpec); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, converSecretKey); byte[] result = cipher.doFinal(Hex.decodeHex(pas)); return new String(result); } /** * DES3加密 * @param pas * @param keyVal * @return * @throws Exception */ public static String DES3Encode(String pas,String keyVal) throws Exception{ byte[] keyArr = Hex.decodeHex(keyVal); //key的转换 DESedeKeySpec desKeySpec = new DESedeKeySpec(keyArr); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DESede"); Key converSecretKey = secretKeyFactory.generateSecret(desKeySpec); //进行加密 //加密的格式 Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, converSecretKey); byte[] result = cipher.doFinal(pas.getBytes()); return Hex.encodeHexString(result); } /** * DES3解密 * @param pas * @param keyVal * @return * @throws Exception */ public static String DES3Decode(String pas,String keyVal) throws Exception{ //解密 byte[] keyArr = Hex.decodeHex(keyVal); //key的转换 DESedeKeySpec desKeySpec = new DESedeKeySpec(keyArr); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DESede"); Key converSecretKey = secretKeyFactory.generateSecret(desKeySpec); Cipher cipher = Cipher.getInstance("DESede"); cipher.init(Cipher.DECRYPT_MODE, converSecretKey); byte[] result = cipher.doFinal(Hex.decodeHex(pas)); return new String(result); } /** * AES的加密 * @param pas * @param keyVal * @return * @throws Exception */ public static String AESEncode(String pas,String keyVal) throws Exception{ Key key = new SecretKeySpec(Hex.decodeHex(keyVal), "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] result = cipher.doFinal(pas.getBytes()); String resultStr = Hex.encodeHexString(result); return resultStr; } /** * AES的解密 * @param pas * @param keyVal * @return * @throws Exception */ public static String AESDecode(String pas,String keyVal) throws Exception{ Key key = new SecretKeySpec(Hex.decodeHex(keyVal), "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, key); byte[] result = cipher.doFinal(Hex.decodeHex(pas)); return new String(result); } /** * PBE加密 * @param val 要加密的字符串 * @param keyVal 密钥 * @param salt 盐 * @param password 口令 * @return * @throws Exception */ public static String PBEEncode(String val,String salt,String password) throws Exception{ PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray()); SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHMD5andDES"); Key key = factory.generateSecret(pbeKeySpec); PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(Hex.decodeHex(salt), 100); Cipher cipher = Cipher.getInstance("PBEWITHMD5andDES"); cipher.init(Cipher.ENCRYPT_MODE, key,pbeParameterSpec); byte[] bytes = cipher.doFinal(val.getBytes()); return Hex.encodeHexString(bytes); } /** * PBE解密 * @param val 要解密的字符串 * @param keyVal 秘钥 * @param salt 盐 * @param password 口令 * @return * @throws Exception */ public static String PBEDecode(String val,String salt,String password) throws Exception{ PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray()); SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHMD5andDES"); Key key = factory.generateSecret(pbeKeySpec); //解密 PBEParameterSpec pbeParameterSpec2 = new PBEParameterSpec(Hex.decodeHex(salt), 100); Cipher cipher2 = Cipher.getInstance("PBEWITHMD5andDES"); cipher2.init(Cipher.DECRYPT_MODE, key,pbeParameterSpec2); byte[] bytes2 = cipher2.doFinal(Hex.decodeHex(val)); return new String(bytes2); } /** * DES和AES生成的KEY * @param type * @return * @throws Exception */ public static String getKeyStrByType(String type) throws Exception{ //生成key KeyGenerator keyGenerator = KeyGenerator.getInstance(type); //生成默认长度的key keyGenerator.init(new SecureRandom()); SecretKey secretKey = keyGenerator.generateKey(); byte[] bytekey = secretKey.getEncoded(); return Hex.encodeHexString(bytekey); } }