加密解密说明
采用rsa非对称加密方式,对接方使用各个服务商对应的公钥将data数据进行加密或者解密处理。
测试公钥: MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCZFbg16HjkaLQsdhBmsplnePySQIh/VaEus6gYFE9wW8HZYu+aqrLBjGhFWxw9r6zbLe9eoufMcwnxzzvX/e5RE4mzT62BCYSCcosaoykan1AjUdtEaE+zOLjLqXA3L8/C77n4mXm74y6zPvoWpKzxynAM3iF4YLjSbli7XJUkpQIDAQAB
测试服务商ID: "ispInfoId":10000
JAVA 代码样例如下,供参考:
public class RSAUtils {
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
/**
* 密钥算法RSA
*/
public static final String KEY_ALGORITHM = "RSA";
/**
* 加密算法RSA
*/
public static final String CIPHER_ALGORITHM = "RSA/ECB/PKCS1PADDING";
/**
* RSA最大加密明文大小
*/
private static final int MAX_ENCRYPT_BLOCK = 117;
/**
* RSA最大解密密文大小
*/
private static final int MAX_DECRYPT_BLOCK = 128;
/**
* @Description: 公钥加密
* @Param: [srcData, publicKey]
* @return: java.lang.String
**/
public static String encryptByPublicKey(String srcData, String publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
ByteArrayOutputStream out = null;
try {
byte[] keyBytes = Base64.decodeBase64(publicKey.getBytes());
byte[] data = srcData.getBytes("utf-8");
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key publicK = keyFactory.generatePublic(x509KeySpec);
// 对数据加密
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, publicK);
int inputLen = data.length;
out = new ByteArrayOutputStream();
int offSet = 0;
byte[] cache;
int i = 0;
// 对数据分段加密
while (inputLen - offSet > 0) {
if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
} else {
cache = cipher.doFinal(data, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * MAX_ENCRYPT_BLOCK;
}
byte[] encryptedData = out.toByteArray();
return Base64.encodeBase64String(encryptedData);
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* @Description: 公钥解密
* @Param: [srcData, publicKey]
* @return: java.lang.String
**/
public static String decryptByPublicKey(String srcData, String publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
ByteArrayOutputStream out = null;
try {
byte[] keyBytes = Base64.decodeBase64(publicKey.getBytes());
byte[] encryptedData = Base64.decodeBase64(srcData.getBytes());
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key publicK = keyFactory.generatePublic(x509KeySpec);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, publicK);
int inputLen = encryptedData.length;
out = new ByteArrayOutputStream();
int offSet = 0;
byte[] cache;
int i = 0;
// 对数据分段解密
while (inputLen - offSet > 0) {
if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);
} else {
cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * MAX_DECRYPT_BLOCK;
}
byte[] decryptedData = out.toByteArray();
return new String(decryptedData, "utf-8");
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}