1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | 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/bank/v4" ; //您的appid String appid = "xxxx" ; //您的APPKEY / 32位 String appkey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ; //需要传递的数据 String params = "{\"appid\":\"" +appid+ "\",\"bankcard\" : \"62220211111\",\"realName\" : \"张三\",\"cardNo\" : \"12345678\",\"Mobile\" : \"15618181818\",\"cardtype\" : \"\",\"information\" : \"\"}" ; //加密数据 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)); } } |