因爲工做需求,須要對一個字符串進行加密與解密的操做,本身在網上查了一下,發現AES對稱加密算法很是符合個人需求。它的簡單思路就是事先設置一個祕鑰(好比放在配置文件中),而後根據這個祕鑰對字符串進行加密與解密。具體實現代碼以下:java
import java.security.InvalidKeyException; import java.security.Key; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; 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.SecretKeySpec; public class AESEncryptUtil { /** * 加密 * @param encryptString 待加密內容 * @param password 解密密鑰 * @return */ public static String encrypt(String encryptString,String passWord){ String result =null; try { // 生成Key KeyGenerator keyGenerator; keyGenerator = KeyGenerator.getInstance("AES"); // 使用這種初始化方法能夠特定種子來生成密鑰,這樣加密後的密文是惟一固定的。 //keyGenerator.init(128, new SecureRandom(passWord.getBytes())); SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" ); secureRandom.setSeed(passWord.getBytes()); keyGenerator.init(128,secureRandom); SecretKey secretKey = keyGenerator.generateKey(); byte[] keyBytes = secretKey.getEncoded(); // Key轉換 Key key = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] encodeResult = cipher.doFinal(encryptString.getBytes()); result = parseByte2HexStr(encodeResult); ; } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } return result; } /** * 解密 * @param decryptString 待解密內容 * @param password 解密密鑰 * @return */ public static String decrypt(String decryptString,String passWord){ String result = null; try { byte[] decryptBytes = parseHexStr2Byte(decryptString); // 生成Key KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); // 使用這種初始化方法能夠特定種子來生成密鑰,這樣加密後的密文是惟一固定的。 //keyGenerator.init(128, new SecureRandom(passWord.getBytes())); SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" ); secureRandom.setSeed(passWord.getBytes()); keyGenerator.init(128,secureRandom); SecretKey secretKey = keyGenerator.generateKey(); byte[] keyBytes = secretKey.getEncoded(); // Key轉換 Key key = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, key); byte[] decodeResult = cipher.doFinal(decryptBytes); result = new String(decodeResult); }catch(NumberFormatException e){ e.printStackTrace(); System.out.println("AES解密參數非法!"); return ""; } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } return result; } /**將二進制轉換成16進制 * @param buf * @return */ 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(); } /**將16進制轉換爲二進制 * @param hexStr * @return */ 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; } }
上述代碼中標註紅色的部分是我最初使用的版本,在本身本地的windows系統中是徹底沒有問題的,但當把項目部署到Linux服務器上時,在解密的過程當中出現錯誤:javax.crypto.BadPaddingException: Given final block not properly padded
在網上查找後,發現是在生成key的時候出現的問題,因而將程序中解密部分的紅色代碼改爲藍色代碼。最初覺得只有解密過程出現錯誤,因此就沒有修改加密過程的key值,所以錯誤依舊 ,花費很長時間才發現是加密沒有修改的緣由,細心很重要啊。
--------------------- 算法