Signature and signature verification
Generate pre-sign string
Parameters to sign
In the list of request and response parameters, all of them need to be signed except sign and sign_type. (sign_type also needs to be signed in some cases in the list of request parameters)
Generate pre-sign string
Use the following code to package the data:
//package the request parameters
Map sParaTemp = new HashMap();
sParaTemp.put("service", AlipayConfig.service);
sParaTemp.put("partner", AlipayConfig.partner);
sParaTemp.put("_input_charset", AlipayConfig.input_charset);
sParaTemp.put("notify_url", AlipayConfig.notify_url);
sParaTemp.put("return_url", AlipayConfig.return_url);
sParaTemp.put("out_trade_no", out_trade_no);
sParaTemp.put("subject", subject);
sParaTemp.put("total_fee", total_fee);
sParaTemp.put("body", body);
sParaTemp.put("currency", currency);
sParaTemp.put("product_code", product_code);
split_fund_info = split_fund_info.replaceAll("\"", "'");
sParaTemp.put("split_fund_info", split_fund_info);
sParaTemp.put("secondary_merchant_id",secondary_merchant_id);
sParaTemp.put("secondary_merchant_name",secondary_merchant_name);
sParaTemp.put("secondary_merchant_industry",secondary_merchant_industry);
Rearrange parameters in the data set alphabetically
And connect rearranged parameters with &:
_input_charset=utf-8&app_pay=Y&body=test¤cy=USD¬ify_url=http://d4779318.ngrok.io/new_create_forex_trade_wap_JAVA-UTF-8-RSA/notify_url.jsp&out_trade_no=test20170901162***&partner=2088101122136***&product_code=NEW_WAP_OVERSEAS_SELLER&return_url=http://d4779318.ngrok.io/new_create_forex_trade_wap_JAVA-UTF-8-RSA/return_url.jsp&service=create_forex_trade_wap&subject=test123&total_fee=0.01
This is the pre-sign string.
Note:
- Parameters without a value, can be excluded from sign;
- Charset in sign must be consistent with the charset used previously
- If _input_charset is passed, it also shall be signed.
- According to HTTP protocol, special character like &,@ needs to do URL encoding, therefore the request receiver can get correct value. In this situation, pre-sign string shall be the original value instead of encoded one. For example: calling a particular API need to sign the parameter email, the pre-sign string shall be email=test@msn.com, rather than email=test%40msn.com.
Signature generation
MD5 Signature
Private Key is necessary for MD5 signature. The MD5 private key is the 32-byte string which is composed of English letters and numbers. Partner can log on the Merchant Service Center (https://global.alipay.com) to check the private key.
- Sign for request
After the partner receives the pre-sign string during requesting, the private key should be appended to the pre-sign string to generate the new string. Then this new string would be calculated with the MD5 signature algorithm by the MD5 signature function. Thus, the result 32-byte string is the signature result string. (the value is given to parameter "sign")
- Signature Verification for response
After receiving the pre-sign string during responding from Alipay system, the next step is the same as the procedure of Sign for request. When the 32-byte signature result string is generated, it should be verified whether the value is equal to the value of the parameter "sign". If equal, the verification would be passed.
RSA, RSA2 Signature
Both private key and public key are necessary for RSA signature. Both private key and public key are generated with OPENSSL by partner. Partner and Alipay need to exchange their own public key. Therefore, partner uses Alipay public key and partner private key.
- Sign for request
After the partner receives the pre-sign string during requesting, the partner private key and the pre-sign string are used in the RSA signature algorithm by the RSA signature function to get the result string. (the value is given to parameter "sign")
- Signature Verification for response
After receiving the pre-sign string during responding from Alipay system, the Alipay public key, the pre-sign string and the parameter "sign" are used in the RSA signature asymmetric algorithm by the RSA signature function to accomplish the signature verification.