package apistore; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.Console; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.Arrays; import java.util.Collection; import java.util.Random; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import sun.misc.BASE64Decoder; //用户请使用UTF-8作为源码文件的保存格式,避免出现乱码问题 public class AES128_APIStore { /** * 远程post * @param strUrl * @param param * @return */ public static String requestPost(String strUrl, String param) { String returnStr = null; // 返回结果定义 URL url = null; HttpURLConnection httpURLConnection = null; try { url = new URL(strUrl); httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestProperty("Accept-Charset", "utf-8"); httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); httpURLConnection.setRequestMethod("POST"); // post方式 httpURLConnection.connect(); //System.out.println("ResponseCode:" + httpURLConnection.getResponseCode()); //POST方法时使用 byte[] byteParam = param.getBytes("UTF-8"); DataOutputStream out = new DataOutputStream(httpURLConnection.getOutputStream()); out.write(byteParam); out.flush(); out.close(); BufferedReader reader = new BufferedReader( new InputStreamReader(httpURLConnection.getInputStream(), "utf-8")); StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = reader.readLine()) != null) { buffer.append(line); } reader.close(); returnStr = buffer.toString(); } catch (Exception e) { e.printStackTrace(); return null; } finally { if (httpURLConnection != null) { httpURLConnection.disconnect(); } } return returnStr; } /** * 加密字符串 * @param key * @param data * @return */ public static String encrypt(String key, String data) { try { String iv = "0000000000000000"; Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); int blockSize = cipher.getBlockSize(); byte[] dataBytes = data.getBytes(); int plaintextLength = dataBytes.length; if (plaintextLength % blockSize != 0) { plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize)); } byte[] plaintext = new byte[plaintextLength]; System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length); SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES"); IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec); byte[] encrypted = cipher.doFinal(plaintext); return new sun.misc.BASE64Encoder().encode(encrypted); } catch (Exception e) { e.printStackTrace(); return null; } } /** * 解密字符串 * @param key * @param data * @return */ public static String decrypt(String key, String data) { try { String iv = "0000000000000000"; byte[] encrypted1 = new BASE64Decoder().decodeBuffer(data); Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES"); IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes()); cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec); byte[] original = cipher.doFinal(encrypted1); String originalString = new String(original); return originalString; } catch (Exception e) { e.printStackTrace(); return null; } } /** * return * @param args */ public static void main(String[] args) { //接口地址 //使用本接口,需要在安全设置页面,设置接入模式为aes128 String url="https://v.1dq.com/api/c43"; //您的appid String appid = "xxxx"; //您的APPKEY / 32位 String appkey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; //需要传递的数据 String params = "{\"appid\":\""+appid+"\",\"bankcard\" : \"123456789\",\"apiversion\" : \"2.0.5\"}"; //加密数据 String sign = encrypt(appkey,params); //远程的请求; String requestData = "appid="+appid+"&sign="+URLEncoder.encode(sign); //执行远程请求 String returnStr = requestPost(url, requestData); System.out.println("sign:"+sign); System.out.println("传递的参数:"+requestData); System.out.println("远程服务器返回:"+returnStr); System.out.println("远程服务器返回:"+decrypt(appkey,returnStr)); } }