支付宝和微信接口加密规范调研
支付宝(ISV)
参考文档:https://docs.open.alipay.com/291/106115
签名方式:RSA2
具体实现
初始化 AlipayClient
1 | AlipayClient alipayClient = new DefaultAlipayClient(gateway,app_id,private_key,"json",charset,alipay_public_key,sign_type); |
具体签名过程
获取encryptor,加密关键参数部分
1
2String encryptContent = getEncryptor().encrypt(
appParams.get(AlipayConstants.BIZ_CONTENT_KEY), this.encryptType, this.charset)加密算法
1
2
3
4
5
6
7
8Cipher cipher = Cipher.getInstance(AES_CBC_PCK_ALG);
IvParameterSpec iv = new IvParameterSpec(AES_IV);
cipher.init(Cipher.ENCRYPT_MODE,
new SecretKeySpec(Base64.decodeBase64(aesKey.getBytes()), AES_ALG), iv);
byte[] encryptBytes = cipher.doFinal(content.getBytes(charset));
return new String(Base64.encodeBase64(encryptBytes));签名
1
2
3String signContent = AlipaySignature.getSignatureContent(requestHolder);
protocalMustParams.put(AlipayConstants.SIGN,
getSigner().sign(signContent, this.signType, charset));签名算法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17PrivateKey priKey = getPrivateKeyFromPKCS8(AlipayConstants.SIGN_TYPE_RSA,
new ByteArrayInputStream(privateKey.getBytes()));
java.security.Signature signature = java.security.Signature
.getInstance(AlipayConstants.SIGN_SHA256RSA_ALGORITHMS);
signature.initSign(priKey);
if (StringUtils.isEmpty(charset)) {
signature.update(content.getBytes());
} else {
signature.update(content.getBytes(charset));
}
byte[] signed = signature.sign();
return new String(Base64.encodeBase64(signed));增加accessToken等参数发起访问
微信(ISV)
主要依靠token机制,支持post,get,消息体明文传输
消息内返回需要加密:
加密机制:aes
主要加密逻辑
1 | public String encrypt(String plainText) { |