1 package com.example.loadurlcode; 2 3 4 import java.io.UnsupportedEncodingException; 5 import java.security.InvalidKeyException; 6 import java.security.NoSuchAlgorithmException; 7 import java.security.SecureRandom; 8 9 import javax.crypto.BadPaddingException; 10 import javax.crypto.Cipher; 11 import javax.crypto.IllegalBlockSizeException; 12 import javax.crypto.KeyGenerator; 13 import javax.crypto.NoSuchPaddingException; 14 import javax.crypto.SecretKey; 15 import javax.crypto.spec.SecretKeySpec; 16 17 18 public class AES { 19 /** 20 * 加密 21 * 22 * @param content 須要加密的內容 23 * @param password 加密密碼 24 * @return 25 */ 26 public static byte[] encrypt(String content, String password) { 27 try { 28 KeyGenerator kgen = KeyGenerator.getInstance("AES"); 29 kgen.init(128, new SecureRandom(password.getBytes())); 30 SecretKey secretKey = kgen.generateKey(); 31 byte[] enCodeFormat = secretKey.getEncoded(); 32 SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); 33 Cipher cipher = Cipher.getInstance("AES");// 建立密碼器 34 byte[] byteContent = content.getBytes("utf-8"); 35 cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化 36 byte[] result = cipher.doFinal(byteContent); 37 return result; // 加密 38 } catch (NoSuchAlgorithmException e) { 39 e.printStackTrace(); 40 } catch (NoSuchPaddingException e) { 41 e.printStackTrace(); 42 } catch (InvalidKeyException e) { 43 e.printStackTrace(); 44 } catch (UnsupportedEncodingException e) { 45 e.printStackTrace(); 46 } catch (IllegalBlockSizeException e) { 47 e.printStackTrace(); 48 } catch (BadPaddingException e) { 49 e.printStackTrace(); 50 } 51 return null; 52 } 53 54 /**解密 55 * @param content 待解密內容 56 * @param password 解密密鑰 57 * @return 58 */ 59 public static byte[] decrypt(byte[] content, String password) { 60 try { 61 KeyGenerator kgen = KeyGenerator.getInstance("AES"); 62 kgen.init(128, new SecureRandom(password.getBytes())); 63 SecretKey secretKey = kgen.generateKey(); 64 byte[] enCodeFormat = secretKey.getEncoded(); 65 SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); 66 Cipher cipher = Cipher.getInstance("AES");// 建立密碼器 67 cipher.init(Cipher.DECRYPT_MODE, key);// 初始化 68 byte[] result = cipher.doFinal(content); 69 return result; // 加密 70 } catch (NoSuchAlgorithmException e) { 71 e.printStackTrace(); 72 } catch (NoSuchPaddingException e) { 73 e.printStackTrace(); 74 } catch (InvalidKeyException e) { 75 e.printStackTrace(); 76 } catch (IllegalBlockSizeException e) { 77 e.printStackTrace(); 78 } catch (BadPaddingException e) { 79 e.printStackTrace(); 80 } 81 return null; 82 } 83 84 85 /**將二進制轉換成16進制 86 * @param buf 87 * @return 88 */ 89 public static String parseByte2HexStr(byte buf[]) { 90 StringBuffer sb = new StringBuffer(); 91 for (int i = 0; i < buf.length; i++) { 92 String hex = Integer.toHexString(buf[i] & 0xFF); 93 if (hex.length() == 1) { 94 hex = '0' + hex; 95 } 96 sb.append(hex.toUpperCase()); 97 } 98 return sb.toString(); 99 } 100 101 /**將16進制轉換爲二進制 102 * @param hexStr 103 * @return 104 */ 105 public static byte[] parseHexStr2Byte(String hexStr) { 106 if (hexStr.length() < 1) 107 return null; 108 byte[] result = new byte[hexStr.length()/2]; 109 for (int i = 0;i< hexStr.length()/2; i++) { 110 int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16); 111 int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16); 112 result[i] = (byte) (high * 16 + low); 113 } 114 return result; 115 } 116 117 /** 118 * 加密 119 * 120 * @param content 須要加密的內容 121 * @param password 加密密碼 122 * @return 123 */ 124 public static byte[] encrypt2(String content, String password) { 125 try { 126 SecretKeySpec key = new SecretKeySpec(password.getBytes(), "AES"); 127 Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding"); 128 byte[] byteContent = content.getBytes("utf-8"); 129 cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化 130 byte[] result = cipher.doFinal(byteContent); 131 return result; // 加密 132 } catch (NoSuchAlgorithmException e) { 133 e.printStackTrace(); 134 } catch (NoSuchPaddingException e) { 135 e.printStackTrace(); 136 } catch (InvalidKeyException e) { 137 e.printStackTrace(); 138 } catch (UnsupportedEncodingException e) { 139 e.printStackTrace(); 140 } catch (IllegalBlockSizeException e) { 141 e.printStackTrace(); 142 } catch (BadPaddingException e) { 143 e.printStackTrace(); 144 } 145 return null; 146 } 147 148 public static void main(String[] args) { 149 String content = "test"; 150 String password = "12345678"; 151 //加密 152 System.out.println("加密前:" + content); 153 byte[] encryptResult = encrypt(content, password); 154 System.out.println(new String(encryptResult)); 155 156 //解密 157 byte[] decryptResult = decrypt(encryptResult,password); 158 System.out.println("解密後:" + new String(decryptResult)); 159 } 160 }
本文轉自http://duanfei.iteye.com/blog/1725613java