AES加解密算法
程序员文章站
2024-03-14 18:36:47
...
/** 算法/模式/填充 **/ private static final String CipherMode = "AES/CBC/PKCS5Padding";
const KEY="625202f9149e061d";
const IV ="5efd3f6060e20330";
用法:
encrypt(content, KEY, IV)
decrypt(content, KEY, IV)/** 创建** **/
//加解密双方约定好的key,随便生成一串字符串,只要两边一致就行 private static SecretKeySpec createKey(String key) { byte[] data = null; if (key == null) { key = ""; } StringBuffer sb = new StringBuffer(16); sb.append(key); while (sb.length() < 16) { sb.append("0"); } if (sb.length() > 16) { sb.setLength(16); } try { data = sb.toString().getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return new SecretKeySpec(data, "AES"); } // private static SecretKeySpec createKey() { // String key = KEY; // byte[] data = null; // if (key == null) { // key = ""; // } // StringBuffer sb = new StringBuffer(16); // sb.append(key); // while (sb.length() < 16) { // sb.append("0"); // } // if (sb.length() > 16) { // sb.setLength(16); // } // try { // data = sb.toString().getBytes("UTF-8"); // } catch (UnsupportedEncodingException e) { // e.printStackTrace(); // } // return new SecretKeySpec(data, "AES"); // } //加解密双方约定好的IV,随便生成一串字符串,只要两边一致就行 private static IvParameterSpec createIV(String password) { byte[] data = null; if (password == null) { password = ""; } StringBuffer sb = new StringBuffer(16); sb.append(password); while (sb.length() < 16) { sb.append("0"); } if (sb.length() > 16) { sb.setLength(16); } try { data = sb.toString().getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return new IvParameterSpec(data); } // private static IvParameterSpec createIV() { // String password = IV; // byte[] data = null; // if (password == null) { // password = ""; // } // StringBuffer sb = new StringBuffer(16); // sb.append(password); // while (sb.length() < 16) { // sb.append("0"); // } // if (sb.length() > 16) { // sb.setLength(16); // } // try { // data = sb.toString().getBytes("UTF-8"); // } catch (UnsupportedEncodingException e) { // e.printStackTrace(); // } // return new IvParameterSpec(data); // } /** 加密字节数据 **/ public static byte[] encrypt(byte[] content, String password, String iv) { try { SecretKeySpec key = createKey(password); Cipher cipher = Cipher.getInstance(CipherMode); cipher.init(Cipher.ENCRYPT_MODE, key, createIV(iv)); byte[] result = cipher.doFinal(content); return result; } catch (Exception e) { e.printStackTrace(); } return null; } // public static byte[] encrypt(byte[] content) { // try { // SecretKeySpec key = createKey(); // Cipher cipher = Cipher.getInstance(CipherMode); // cipher.init(Cipher.ENCRYPT_MODE, key, createIV()); // byte[] result = cipher.doFinal(content); // return result; // } catch (Exception e) { // e.printStackTrace(); // } // return null; // } /** 加密(结果为16进制字符串) **/ public static String encrypt(String content, String password, String iv) { byte[] data = null; try { data = content.getBytes("UTF-8"); } catch (Exception e) { e.printStackTrace(); } data = encrypt(data, password, iv); String result = byte2hex(data); return result; } // public static String encrypt(String content) { // byte[] data = null; // try { // data = content.getBytes("UTF-8"); // } catch (Exception e) { // e.printStackTrace(); // } // data = encrypt(data); // String result = byte2hex(data); // return result; // } /** 解密字节数组 **/ public static byte[] decrypt(byte[] content, String password, String iv) { try { SecretKeySpec key = createKey(password); Cipher cipher = Cipher.getInstance(CipherMode); cipher.init(Cipher.DECRYPT_MODE, key, createIV(iv)); byte[] result = cipher.doFinal(content); return result; } catch (Exception e) { e.printStackTrace(); } return null; } // public static byte[] decrypt(byte[] content) { // try { // SecretKeySpec key = createKey(); // Cipher cipher = Cipher.getInstance(CipherMode); // cipher.init(Cipher.DECRYPT_MODE, key, createIV()); // byte[] result = cipher.doFinal(content); // return result; // } catch (Exception e) { // e.printStackTrace(); // } // return null; // } /** 解密(输出结果为字符串) **/ public static String decrypt(String content, String password, String iv) { byte[] data = null; try { data = hex2byte(content); } catch (Exception e) { e.printStackTrace(); } data = decrypt(data, password, iv); if (data == null) return null; String result = null; try { result = new String(data, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return result; } // public static String decrypt(String content) { // byte[] data = null; // try { // data = hex2byte(content); // } catch (Exception e) { // e.printStackTrace(); // } // data = decrypt(data); // if (data == null) // return null; // String result = null; // try { // result = new String(data, "UTF-8"); // } catch (UnsupportedEncodingException e) { // e.printStackTrace(); // } // return result; // } /** 字节数组转成16进制字符串 **/ public static String byte2hex(byte[] b) { // 一个字节的数, StringBuffer sb = new StringBuffer(b.length * 2); String tmp = ""; for (int n = 0; n < b.length; n++) { // 整数转成十六进制表示 tmp = (java.lang.Integer.toHexString(b[n] & 0XFF)); if (tmp.length() == 1) { sb.append("0"); } sb.append(tmp); } return sb.toString().toUpperCase(); // 转成大写 } /** 将hex字符串转换成字节数组 **/ private static byte[] hex2byte(String inputString) { if (inputString == null || inputString.length() < 2) { return new byte[0]; } inputString = inputString.toLowerCase(); int l = inputString.length() / 2; byte[] result = new byte[l]; for (int i = 0; i < l; ++i) { String tmp = inputString.substring(2 * i, 2 * i + 2); result[i] = (byte) (Integer.parseInt(tmp, 16) & 0xFF); } return result; }
上一篇: 关于import理解的一些事情
下一篇: Java反射获取泛型相关信息