此類主要用於加密與解密,采用128位ECB模式,PKCS5Padding填充補位。
可使用方法為加密返回二進制encryptBin(content, key)、加密返回十六進制encryptHex(content, key)、二進制內容解密decryptBin(content, key)、十六進制內容解密decryptHex(content, key)。
content是需要加密的字符串,key是密鑰,隨意一個字符串。
1 package com.test; 2 3 import java.io.UnsupportedEncodingException; 4 import java.security.InvalidKeyException; 5 import java.security.NoSuchAlgorithmException; 6 import java.security.SecureRandom; 7 8 import javax.crypto.BadPaddingException; 9 import javax.crypto.Cipher; 10 import javax.crypto.IllegalBlockSizeException; 11 import javax.crypto.KeyGenerator; 12 import javax.crypto.NoSuchPaddingException; 13 import javax.crypto.SecretKey; 14 import javax.crypto.spec.SecretKeySpec; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 19 public class AESCode { 20 21 private static final String algorithm = "AES"; 22 private static final String transformation = "AES/ECB/PKCS5Padding"; 23 private static final String charset = "utf-8"; 24 25 private static final Log _log = LogFactory.getLog(AESCode.class); 26 27 /** 28 * 獲取key 填充密匙 獲取加密的密匙數據 29 * 30 * @paramString key 31 * @return byte[] enCodeFormat; 32 * @author panjianghong 2016-8-29 33 * */ 34 private static byte[] getEnCodeFormat(String key){ 35 36 try { 37 KeyGenerator kgen = KeyGenerator.getInstance("AES"); 38 kgen.init(128, new SecureRandom(key.getBytes())); 39 SecretKey secretKey = kgen.generateKey(); 40 byte[] enCodeFormat = secretKey.getEncoded(); 41 return enCodeFormat; 42 } catch (NoSuchAlgorithmException e) { 43 _log.error("獲取密匙數據失敗!"); 44 } 45 return null; 46 } 47 48 49 50 /** 51 * 獲取加密數據的二進制字符串數據 52 * 53 * @param content 54 * @param enCodeFormat 55 * @return String 56 * @author panjianghong 2016-8-29 57 * */ 58 public static String encryptBin(String content, String key){ 59 60 try { 61 byte[] byteConten = encrypt(content, key); 62 return byte2BinString(byteConten); 63 } catch (Exception e) { 64 _log.error("獲取二進制加密數據失敗!"); 65 } 66 return null; 67 } 68 69 70 71 /** 72 * 獲取加密數據的十六進制字符串數據 73 * 74 * @param content 75 * @param enCodeFormat 76 * @return String 77 * @author panjianghong 2016-8-29 78 * */ 79 public static String encryptHex(String content, String key){ 80 81 try { 82 byte[] byteConten = encrypt(content, key); 83 return byte2HexString(byteConten); 84 } catch (Exception e) { 85 _log.error("獲取十六進制加密數據失敗!"); 86 } 87 return null; 88 } 89 90 91 /** 92 * 獲取文件的加密數據 93 * 返回加密數據的字節數組 byte[] 94 * 95 * @param content 96 * @param enCodeFormat 97 * @return byte[] byteResoult 98 * @author panjianghong 2016-8-29 99 * */ 100 private static byte[] encrypt(String content, String key){ 101 102 try { 103 SecretKeySpec secretyKey = new SecretKeySpec(getEnCodeFormat(key), algorithm); 104 Cipher cipher = Cipher.getInstance(transformation); 105 byte[] byteContent = content.getBytes(charset); 106 cipher.init(Cipher.ENCRYPT_MODE, secretyKey); 107 byte[] byteResoult = cipher.doFinal(byteContent); 108 return byteResoult; 109 } catch (InvalidKeyException e) { 110 _log.error("獲取加密數據的字節數組失敗!"); 111 } catch (NoSuchAlgorithmException e) { 112 _log.error("獲取加密數據的字節數組失敗!"); 113 } catch (NoSuchPaddingException e) { 114 _log.error("獲取加密數據的字節數組失敗!"); 115 } catch (UnsupportedEncodingException e) { 116 _log.error("獲取加密數據的字節數組失敗!"); 117 } catch (IllegalBlockSizeException e) { 118 _log.error("獲取加密數據的字節數組失敗!"); 119 } catch (BadPaddingException e) { 120 _log.error("獲取加密數據的字節數組失敗!"); 121 } 122 123 return null; 124 } 125 126 /** 127 * 以二進制字符串數據進行解密 128 * 129 * @param content 130 * @param enCodeFormat 131 * @return String 132 * @author panjianghong 2016-8-29 133 * */ 134 135 public static String decryptBin(String binContent, String key){ 136 137 try { 138 SecretKeySpec secretyKey = new SecretKeySpec(getEnCodeFormat(key), algorithm); 139 Cipher cipher = Cipher.getInstance(transformation); 140 cipher.init(Cipher.DECRYPT_MODE, secretyKey); 141 byte[] byteResoult = cipher.doFinal(binString2Byte(binContent)); 142 try { 143 return new String(byteResoult,"utf-8"); 144 } catch (UnsupportedEncodingException e) { 145 _log.error("解密二進制數據失敗!"); 146 return null; 147 } 148 } catch (InvalidKeyException e) { 149 _log.error("解密二進制數據失敗!"); 150 } catch (NoSuchAlgorithmException e) { 151 _log.error("解密二進制數據失敗!"); 152 } catch (NoSuchPaddingException e) { 153 _log.error("解密二進制數據失敗!"); 154 } catch (IllegalBlockSizeException e) { 155 _log.error("解密二進制數據失敗!"); 156 } catch (BadPaddingException e) { 157 _log.error("解密二進制數據失敗!"); 158 } 159 160 return null; 161 } 162 163 /** 164 * 以十六進制字符串數據進行解密 165 * 166 * @param content 167 * @param enCodeFormat 168 * @return String 169 * @author panjianghong 2016-8-29 170 * */ 171 public static String decryptHex(String binContent, String key){ 172 173 try { 174 SecretKeySpec secretyKey = new SecretKeySpec(getEnCodeFormat(key), algorithm); 175 Cipher cipher = Cipher.getInstance(transformation); 176 cipher.init(Cipher.DECRYPT_MODE, secretyKey); 177 byte[] byteResoult = cipher.doFinal(hexString2Byte(binContent)); 178 try { 179 return new String(byteResoult,"utf-8"); 180 } catch (UnsupportedEncodingException e) { 181 _log.error("解密十六進制數據失敗!"); 182 return null; 183 } 184 } catch (InvalidKeyException e) { 185 _log.error("解密十六進制數據失敗!"); 186 } catch (NoSuchAlgorithmException e) { 187 _log.error("解密十六進制數據失敗!"); 188 } catch (NoSuchPaddingException e) { 189 _log.error("解密十六進制數據失敗!"); 190 } catch (IllegalBlockSizeException e) { 191 _log.error("解密十六進制數據失敗!"); 192 } catch (BadPaddingException e) { 193 _log.error("解密十六進制數據失敗!"); 194 } 195 196 return null; 197 } 198 199 200 201 202 /** 203 * 字節數組轉化成二進制數 204 * @param content 205 * @return string 206 * @author panjianghong 2016-8-29 207 * */ 208 private static String byte2BinString(byte[] content){ 209 if(null == content){ 210 _log.error("需要轉換的參數為空!"); 211 return null; 212 } 213 214 return hexString2BinString(byte2HexString(content)); 215 } 216 217 /** 218 * 字節數組轉化成十六進制數的小寫形式 219 * @param content 220 * @return string 221 * @author panjianghong 2016-8-29 222 * */ 223 private static String byte2HexString(byte[] content){ 224 if(null == content){ 225 _log.error("需要轉換的參數為空!"); 226 return null; 227 } 228 229 StringBuffer sb = new StringBuffer(); 230 for (int i = 0; i < content.length; i++) { 231 String hex = Integer.toHexString(content[i] & 0xFF); 232 if (hex.length() == 1) { 233 hex = '0' + hex; 234 } 235 sb.append(hex.toLowerCase()); 236 } 237 238 return sb.toString(); 239 } 240 241 /** 242 * 十六進制數轉化成二進制數 243 * @param content 244 * @return string 245 * @author panjianghong 2016-8-29 246 * */ 247 private static String hexString2BinString(String content){ 248 249 if (null == content || content.length() % 2 != 0) { 250 _log.error("需要轉換的參數為空或者參數長度不是2的倍數!"); 251 return null; 252 } 253 254 StringBuffer bString = new StringBuffer(); 255 StringBuffer tmp = new StringBuffer(); 256 for (int i = 0; i < content.length(); i++) 257 { 258 tmp.append("0000").append(Integer.toBinaryString(Integer.parseInt(content.substring(i, i + 1), 16))); 259 bString.append(tmp.toString().substring(tmp.toString().length() - 4)); 260 tmp.delete(0, tmp.toString().length()); 261 } 262 return bString.toString(); 263 } 264 /** 265 * 二進制數轉化成十六進制數 266 * @param content 267 * @return string 268 * @author panjianghong 2016-8-29 269 * */ 270 private static String BinString2hexString(String content){ 271 272 if (null == content || content.equals("") || content.length() % 8 != 0){ 273 _log.error("需要轉換的參數為空或者參數長度不是8的倍數!"); 274 return null; 275 } 276 277 StringBuffer tmp = new StringBuffer(); 278 int iTmp = 0; 279 for (int i = 0; i < content.length(); i += 4) 280 { 281 iTmp = 0; 282 for (int j = 0; j < 4; j++) 283 { 284 iTmp += Integer.parseInt(content.substring(i + j, i + j + 1)) << (4 - j - 1); 285 } 286 tmp.append(Integer.toHexString(iTmp)); 287 } 288 return tmp.toString(); 289 } 290 291 292 /** 293 * 16進制數轉化成字節數組 294 * @param content 295 * @return string 296 * @author panjianghong 2016-8-29 297 * */ 298 private static byte[] hexString2Byte(String content){ 299 if (content.length() < 1){ 300 _log.error("需要轉換的參數為空或者參數長度<1!"); 301 return null; 302 } 303 304 byte[] byteRresult = new byte[content.length() / 2]; 305 for (int i = 0; i < content.length() / 2; i++) { 306 int high = Integer.parseInt(content.substring(i * 2, i * 2 + 1), 16); 307 int low = Integer.parseInt(content.substring(i * 2 + 1, i * 2 + 2), 16); 308 byteRresult[i] = (byte) (high * 16 + low); 309 } 310 return byteRresult; 311 } 312 313 /** 314 * 二進制數轉化成字節數組 315 * @param content 316 * @return string 317 * @author panjianghong 2016-8-29 318 * */ 319 private static byte[] binString2Byte(String content){ 320 if (content.length() < 1){ 321 _log.error("需要轉換的參數為空或者參數長度<1!"); 322 return null; 323 } 324 325 return hexString2Byte(BinString2hexString(content)); 326 } 327 328 }