AES共有ECB、CBC、CFB、OFB、CTR五种模式
AES-128-CBC可以自己定义“密钥”和“偏移量“;
1、AES-CBC-128
public class RiskUtil { public static String certify(JSONObject json,String RISK_CONTROL_KEY,String IV,String DEFAULT_ENCODING,String AES,String PADDING) { json.put("name", UnicodeUtils.native2ascii(json.get("name")+"")); String jsonStr = json.toString(); jsonStr = jsonStr.replace("\\\\", "\\"); String encryptData = encrypt(jsonStr, RISK_CONTROL_KEY,IV,DEFAULT_ENCODING,AES,PADDING); System.out.println("encryptData="+encryptData); return encryptData; } private static String encrypt(String code, String key,String Iv,String DEFAULT_ENCODING,String AES,String PADDING) { code = padding(code); try { return new Base64() .encodeToString(encrypt(code.getBytes(DEFAULT_ENCODING), key.getBytes(DEFAULT_ENCODING),Iv.getBytes(DEFAULT_ENCODING),AES,PADDING)); } catch (Exception e) { e.printStackTrace(); } return null; } private static byte[] encrypt(byte[] code, byte[] key,byte[] Iv,String AES,String PADDING) throws Exception { SecretKeySpec keySpec = new SecretKeySpec(key, AES); Cipher cipher = Cipher.getInstance(PADDING);// "算法/模式/补码方式" // 使用CBC模式,需要一个向量iv,可增加加密算法的强度 IvParameterSpec iv = new IvParameterSpec(Iv); cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv); return cipher.doFinal(code); } public static String decrypt(String data, String key,String IV,String DEFAULT_ENCODING,String AES,String PADDING) { data = padding(data); try { return new String(decrypt(data.getBytes(DEFAULT_ENCODING), key.getBytes(DEFAULT_ENCODING), IV.getBytes(DEFAULT_ENCODING),DEFAULT_ENCODING,AES,PADDING)); } catch (Exception e) { e.printStackTrace(); } return null; } private static byte[] decrypt(byte[] src, byte[] key,byte[] Iv,String DEFAULT_ENCODING,String AES,String PADDING) throws Exception { SecretKeySpec keySpec = new SecretKeySpec(key, AES); Cipher cipher = Cipher.getInstance(PADDING); IvParameterSpec iv = new IvParameterSpec(Iv); cipher.init(Cipher.DECRYPT_MODE, keySpec, iv); byte[] _src = new Base64().decode(src); return cipher.doFinal(_src); } private static String padding(String code) { for (int i = 0; i < code.length(); i++) { code += " "; if (code.length() % 16 == 0) { break; } } return code; } public static void main(String[] args) { final String AES = "AES"; final String PADDING = "AES/CBC/NoPadding";// "算法/模式/补码方式" final String DEFAULT_ENCODING = "utf-8"; final String IV = "1234567812345678"; // 身份认证 final String RISK_CONTROL_URL = "http://fengkong.haodai.com/api/data/index"; final String RISK_CONTROL_KEY = "UMorc6ecjMgKANVLaoO4VUP7T8DxRdgv"; final String RISK_CONTROL_ID = "00000000-0"; Request request = JsonUtil.fromJson("{'type':'202','name':'小米','idcard':'110105198307077715'}", Request.class); JSONObject jo = JSONObject.fromObject(JsonUtil.toJson(request)); RiskUtil rt = new RiskUtil(); System.out.println("encryptData="+rt.certify(jo,RISK_CONTROL_KEY,IV,DEFAULT_ENCODING,AES,PADDING)); //JSONObject jo2 = JSONObject.fromObject(rt.certify(jo,RISK_CONTROL_KEY,IV,DEFAULT_ENCODING,AES,PADDING)); //System.out.println("加密编码后:" +decrypt(jo2.get("data")+"",RISK_CONTROL_KEY)); } }
public class UnicodeUtils { public static String native2ascii(String code) { char[] chars = code.toCharArray(); int charValue = 0; String result = ""; for(int i = 0; i < chars.length; i++){ charValue = (int) code.charAt(i); if (charValue <= 256) { // result += "& "+Integer.toHexString(charValue)+";"; result += "\\"+Integer.toHexString(charValue); }else{ // result += "&#x"+Integer.toHexString(charValue)+";"; result += "\\u"+Integer.toHexString(charValue); } } return result; } public static String ascii2native(String code) { char aChar; int len = code.length(); StringBuffer outBuffer = new StringBuffer(len); for (int x = 0; x < len;) { aChar = code.charAt(x++); if (aChar == '\\') { aChar = code.charAt(x++); if (aChar == 'u') { // Read the xxxx int value = 0; for (int i = 0; i < 4; i++) { aChar = code.charAt(x++); switch (aChar) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': value = (value << 4) + aChar - '0'; break; case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': value = (value << 4) + 10 + aChar - 'a'; break; case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': value = (value << 4) + 10 + aChar - 'A'; break; default: throw new IllegalArgumentException( "Malformed \\uxxxx encoding."); } } outBuffer.append((char) value); } else { if (aChar == 't') aChar = '\t'; else if (aChar == 'r') aChar = '\r'; else if (aChar == 'n') aChar = '\n'; else if (aChar == 'f') aChar = '\f'; outBuffer.append(aChar); } } else outBuffer.append(aChar); } return outBuffer.toString(); } public static void main(String[] args) { String result = native2ascii("中国"); String _result = ascii2native(result); System.out.println(result); System.out.println(_result); } }