背景
支付宝开元SDK中提供了RSA工具类,可以方便的对数据进行加解密,经测试演示版本sdk支持分段加解密,并且提供了加签与验签的常用方法。并支持RSA与RSA2两种类的签名类型。
注:生产RSA签名请参考 https://docs.open.alipay.com/291/105971
Maven 支付宝SDK依赖
注:maven中央库直达连接 https://mvnrepository.com/artifact/com.alipay.sdk/alipay-sdk-java
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.7.4.ALL</version>
</dependency>
RSA普通加密解密示例
1.调用 AlipaySignature.rsaEncrypt() 方法加密
2.调用 AlipaySignature.rsaDecrypt() 方法解密
public static void main(String[] args) throws Exception {
//公钥
String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDp9FaWlZc/doHX" +
"il60NcJB56trQOoYSMx5LUgBr+rGANJNBJvUJOI3Jqfs5axf4Lx/JqlhGcmlBuNE" +
"gt16iEwwBLj6GSpHIm+qZ/bT1xHqJqJzS55PUu6JNhZsV6ERz/tvs3dITuTa0zxT" +
"CSX4c9DniIF0VuWCLgEvRVuI2pqBYQIDAQAB";
//私钥
String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAOn0VpaVlz92gdeK" +
"XrQ1wkHnq2tA6hhIzHktSAGv6sYA0k0Em9Qk4jcmp+zlrF/gvH8mqWEZyaUG40SC" +
"3XqITDAEuPoZKkcib6pn9tPXEeomonNLnk9S7ok2FmxXoRHP+2+zd0hO5NrTPFMJ" +
"Jfhz0OeIgXRW5YIuAS9FW4jamoFhAgMBAAECgYBS+Rg4gcdJd7MnCWj7CtHhY9OU" +
"z0n24OK720ZdatGd9a6gKBFIFOix3KDT0MTooCDaC5Eo4ed1AtNZefwot+wksf2R" +
"dXgoAf/uS1GpxbzXhzFHmfDBOf7yKRXm4K/xzVWu7+1DI2siGwuQ6GWfyhZlGCgj" +
"DASHmSYOAidG8t6KQQJBAPeotqqoY0Q7yICWyRbJIVgtABRxSR6xE+4fGjHev219" +
"pYopK7r0KRQsyAgHsFoFwzx9Rzr8YG85LBQ7lAeenzkCQQDx1XchnIRRuAl5f9EL" +
"QuZjz8orT08nKhrM2aNswfmY5UC7amJPvPSZihYGO+V75NBGii6glG1Fskp+o4Hb" +
"kctpAkA/DChUkcGToVSNx9Uxg8qFVA9tJNnkglf8itZ0U6yQByiZX5nOYlIC3NOf" +
"l7WBdW4afBoiHhW+uuNJ01EAD5WhAkANBpx2GQIfjLQl8TaHTsEuGyEemBUBXQk4" +
"g3iEASa6j46fCanl9Z2PL5rkFxHTTI3TXtoIGDaSjHnqvE/gTcgRAkEA92TtlNrc" +
"VMm7ZkXz26YXkJ5dLDAFB7X8GxZU7DBJ3nFU8Ezj3V5y+5ka494A7/8WVYZUpnm9" +
"KspvMmNNK4l/lQ==";
String content = "123456";
//公钥-加密
String rsaEncrypt = AlipaySignature.rsaEncrypt(content, publicKey, "UTF-8");
//私钥-解密
String rsaDecrypt = AlipaySignature.rsaDecrypt(rsaEncrypt, privateKey, "UTF-8");
System.out.println("加密前长度 :" + content.length());
System.out.println("加密后 :" + rsaEncrypt);
System.out.println("解密后 :" + rsaDecrypt);
System.out.println("解密后长度 :" + rsaDecrypt.length());
}
结果
RSA加签验签示例
/**
* 支付宝 sdk 加签验签测试
*
* @author 码农猿
*/
public class SignDemo {
/**
* 公钥
*/
private static final String PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnLbS4NKB4K6lSwAruObksqJpcrs7t8w51Y38I9ix9r7WCBMiAjRxWu8DNnr2t5ya/XXoioHqyMH3oEkiqsUh3FX1pG/4N/9/hRtFDJqpkCo4Z/D76wEehY+y+k8sGEO+dO8m8bNbmfpcIGDwuPl3qVBFeyJQwPguuWd32/xtzLcYk2C/Eec25++ElHobGvLkRTHHlXeZ+v8IYCIPd058bQssnFMw7nn+Lgr2kQK/2gPQ8gXs8mi1712i/dgoo8bzw9mhzkFnWXExRqzgoXH5Df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P2v2YOZEJr0LOpIxJwaNCgea67t055/IlsBqHBu0cBtGQ7i2ykd/3Q1v+q0dEKf3C92lvwcsLRQo01gjnDMRiiSkIf0cYdxs66wgzAxh4xd9BvwNdQKBgAEVrydxNkwudAt/XIzYnykGuCSAVmNZb1yH8J/Oplc06mt28jqlM9O+z6OskKNd9BhzhQSuen9Ufy9zDmKZtpVTitxSxiiQ3RPeximiK6ZJ5QlxarogFs5BrHI5ah0THSN+DEA8OaOYjAPQ4Ygid5wt1fE2yXsXvmT77V5VQ7QXAoGALDRNdl0LY1iYnhIEIK5aV7xdWEg6GlchXMVqh50l6FlQPE+2eW7aYRwuE97uFjhQAkFFMiTsUVI9hAzVHKJ2+cnsdfZsII/xLpEyYEGUAYEvgiVGWfX+md++rITQm9nZhmkNHAdBA4M6ZLHOIAtmuYmFnKQ67RtjWJ2PoEWrS9ECgYBW6QtwiRxoz/Xes9jHRmShVcDfwq210Qubmd81uUtDy/lVOY99qCb4rNsTu3RVrtMubD9syvivDroE6NtFrDBNHAtRaGB77Ivs+WdMdhDzNwGxVfgt6h/qJoY05+NmVPe3ExtC2fs8EWnZkXPbs2qurFxdvhBjxHIQHQwV54GaZQ==";
/**
* 字符集
*/
private static final String CHAR_SET = "UTF-8";
/**
* 签名类型 RSA 或 RSA2
*/
private static final String SIGN_TYPE = "RSA2";
public static void main(String[] args) {
//创建参数
Map<String, String> paramMap = new HashMap<String, String>();
paramMap.put("params1", "value..1..");
paramMap.put("params2", "value..2..");
paramMap.put("params3", "value..3..");
paramMap.put("params4", "value..4..");
try {
//拼接待加签的字符串
String content = AlipaySignature.getSignContent(paramMap);
//私钥-对数据加签
String sign = AlipaySignature.rsaSign(content, PRIVATE_KEY, CHAR_SET, SIGN_TYPE);
//添加签名到参数
paramMap.put("sign", sign);
//公钥-对数据验签
boolean result = AlipaySignature.rsaCheckV1(paramMap, PUBLIC_KEY, CHAR_SET, SIGN_TYPE);
System.out.println("签名类型 : " + SIGN_TYPE + " 编码规则: " + CHAR_SET);
System.out.println("拼接待加签的字符串 : " + content);
System.out.println("当前签名 : " + sign);
System.out.println("验签结果 : " + result);
} catch (AlipayApiException e) {
e.printStackTrace();
}
}
}
RSA与RSA2
以下介绍来自支付宝开放平台