static String privateKey = "";express
static String publicKey = "";json
static {app
// 從文件中獲取私密和公鑰
BufferedReader mBufferedReader = null;
try {
FileReader mFileReader = new FileReader(new File(
"rsa_private_key_pkcs82.pem"));
mBufferedReader = new BufferedReader(mFileReader);
StringBuilder sb = new StringBuilder();
String line = "";
while (null != (line = mBufferedReader.readLine())) {
if (!line.startsWith("-----")) {
sb.append(line);
}
}
privateKey = sb.toString();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != mBufferedReader) {
try {
mBufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
try {
FileReader mFileReader = new FileReader(new File(
"rsa_public_key.pem"));
mBufferedReader = new BufferedReader(mFileReader);
StringBuilder sb = new StringBuilder();
String line = "";
while (null != (line = mBufferedReader.readLine())) {
if (!line.startsWith("-----")) {
sb.append(line);
}
}
publicKey = sb.toString();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != mBufferedReader) {
try {
mBufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}ui
{
Map<String, String> orderInfoM = new HashMap<>();
orderInfoM.put("app_id", "2016092601974191");
orderInfoM.put("charset", "utf-8");
orderInfoM.put("format", "json");
orderInfoM.put("method", "alipay.trade.app.pay");
orderInfoM.put("notify_url", "http://www.mytian.com.cn/myt_pay/payAction_alipayNotify.do");
orderInfoM.put("sign_type", "RSA");
orderInfoM.put("timestamp", "2016-10-19 10:29:10");
orderInfoM.put("version", "1.0");
Map<String, String> bizContent = new LinkedHashMap<>();
bizContent.put("timeout_express", "30m");
bizContent.put("seller_id", "xiaosong.xu@mytian.com.cn");
bizContent.put("product_code", "QUICK_MSECURITY_PAY");
bizContent.put("total_amount", "0.01");
bizContent.put("subject", "進階套餐54元");
bizContent.put("body", "原價120(4.5折)6000麥粒");
bizContent.put("out_trade_no", "2961476844150427");
orderInfoM.put("biz_content", JSONObject.toJSONString(bizContent));
String content = AlipaySignature.getSignContent(orderInfoM);
String sign = "";
try {
sign = AlipaySignature.rsaSign(content, privateKey, "utf-8");
} catch (AlipayApiException e) {
e.printStackTrace();
}
System.out.println(content+"&sign="+sign);編碼
// 簽名以後要將全部的value進行URLEncoder編碼url
try {
System.out.println(getSignContent(orderInfoM)+"&sign="+URLEncoder.encode(sign, "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}code
public static String getSignContent(Map<String, String> sortedParams) {
StringBuffer content = new StringBuffer();
List<String> keys = new ArrayList<String>(sortedParams.keySet());
Collections.sort(keys);
int index = 0;
for (int i = 0; i < keys.size(); ++i) {
String key = (String) keys.get(i);
String value = (String) sortedParams.get(key);
if (StringUtils.areNotEmpty(new String[] { key, value })) {
try {
content.append(((index == 0) ? "" : "&") + key + "=" + URLEncoder.encode(value, "UTF-8"));
} catch (UnsupportedEncodingException e) {
content.append(((index == 0) ? "" : "&") + key + "=" + value);
}
++index;
}
}
return content.toString();
}orm