項目中使用aes加密算法,密鑰只有16位能夠經過,32位報錯java.security.InvalidKeyException Illegal key size。html
解決方案:http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html 下載兩個jar包,替換D:\Java\jre7\lib\security下同名的文件便可。java
import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.Security; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.KeyGenerator; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class AesEncodeUtil { private final static Logger logger = LoggerFactory.getLogger(AesEncodeUtil.class.getName()); public final String VIPARA = "0200010700020204"; // 偏移量 public final String bm = "UTF-8"; public String ASE_PASSWORD = ""; public AesEncodeUtil(String key) { ASE_PASSWORD = key; } public static String parseByte2HexStr(byte buf[]) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < buf.length; i++) { String hex = Integer.toHexString(buf[i] & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } sb.append(hex.toUpperCase()); } return sb.toString(); } public static byte[] parseHexStr2Byte(String hexStr) { if (hexStr.length() < 1) return null; byte[] result = new byte[hexStr.length() / 2]; for (int i = 0; i < hexStr.length() / 2; i++) { int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16); int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16); result[i] = (byte) (high * 16 + low); } return result; } /** * 加密 * * @param cleartext * @return * @throws NoSuchPaddingException * @throws NoSuchAlgorithmException * @throws UnsupportedEncodingException * @throws InvalidAlgorithmParameterException * @throws InvalidKeyException * @throws BadPaddingException * @throws IllegalBlockSizeException */ public String encrypt(String cleartext) throws Exception { /** * 實例化 使用 PKCS7PADDING 填充方式,按以下方式實現,就是調用bouncycastle組件實現 * Cipher.getInstance(CIPHER_ALGORITHM,"BC") */ Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); KeyGenerator kgen = KeyGenerator.getInstance("AES"); // 防止linux下 隨機生成key SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); secureRandom.setSeed(ASE_PASSWORD.getBytes()); kgen.init(128, secureRandom); SecretKey secretKey = kgen.generateKey(); byte[] enCodeFormat = secretKey.getEncoded(); // SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); SecretKeySpec key = new SecretKeySpec(ASE_PASSWORD.getBytes("utf-8"), "AES"); IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes()); // SecretKeySpec key = new SecretKeySpec( // ASE_PASSWORD.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding"); cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv); byte[] encryptedData = cipher.doFinal(cleartext.getBytes(bm)); return parseByte2HexStr(Base64.encodeBase64(encryptedData)); } /** * 解密 * * @param encrypted * @return * @throws NoSuchAlgorithmException * @throws UnsupportedEncodingException * @throws NoSuchPaddingException * @throws InvalidAlgorithmParameterException * @throws InvalidKeyException * @throws BadPaddingException * @throws IllegalBlockSizeException */ public String decrypt(String encrypted) throws Exception { /** * 實例化 使用 PKCS7PADDING 填充方式,按以下方式實現,就是調用bouncycastle組件實現 * Cipher.getInstance(CIPHER_ALGORITHM,"BC") */ Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); KeyGenerator kgen = KeyGenerator.getInstance("AES"); // 防止linux下 隨機生成key SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); secureRandom.setSeed(ASE_PASSWORD.getBytes()); kgen.init(128, secureRandom); SecretKey secretKey = kgen.generateKey(); byte[] enCodeFormat = secretKey.getEncoded(); encrypted = URLDecoder.decode(encrypted, "utf-8"); byte[] byteMi = Base64.decodeBase64(parseHexStr2Byte(encrypted)); logger.debug("aes decrypt base64:{}", new String(byteMi)); IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes("utf-8")); // SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); SecretKeySpec key = new SecretKeySpec(ASE_PASSWORD.getBytes("utf-8"), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding"); cipher.init(Cipher.DECRYPT_MODE, key, zeroIv); byte[] decryptedData = cipher.doFinal(byteMi); return new String(decryptedData, bm); } }