mirror of
https://gitee.com/egzosn/pay-java-parent.git
synced 2026-05-23 10:31:52 +08:00
1.新增支付验签方式
2.订单参数构造类 3.REA证书验签新增Certificate支持
This commit is contained in:
@@ -12,6 +12,7 @@ import java.security.GeneralSecurityException;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
import java.security.cert.Certificate;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
|
||||
@@ -136,10 +137,7 @@ public class RSA {
|
||||
public static boolean verify(String content, String sign, String publicKey, String signAlgorithms, String characterEncoding) {
|
||||
try {
|
||||
PublicKey pubKey = getPublicKey(publicKey, ALGORITHM);
|
||||
java.security.Signature signature = java.security.Signature.getInstance(signAlgorithms);
|
||||
signature.initVerify(pubKey);
|
||||
signature.update(content.getBytes(characterEncoding));
|
||||
return signature.verify(Base64.decode(sign));
|
||||
return verify(content, sign, pubKey, signAlgorithms, characterEncoding);
|
||||
}
|
||||
catch (GeneralSecurityException e) {
|
||||
LOG.error("", e);
|
||||
@@ -176,6 +174,7 @@ public class RSA {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* RSA验签名检查
|
||||
*
|
||||
@@ -204,6 +203,20 @@ public class RSA {
|
||||
return verify(content, sign, publicKey, SIGN_ALGORITHMS, characterEncoding);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* RSA验签名检查
|
||||
*
|
||||
* @param content 待签名数据
|
||||
* @param sign 签名值
|
||||
* @param publicKey 公钥
|
||||
* @param characterEncoding 编码格式
|
||||
* @return 布尔值
|
||||
*/
|
||||
public static boolean verify(String content, String sign, Certificate publicKey, String characterEncoding) {
|
||||
final PublicKey pubKey = publicKey.getPublicKey();
|
||||
return verify(content, sign, pubKey, SIGN_ALGORITHMS, characterEncoding);
|
||||
}
|
||||
/**
|
||||
* 解密
|
||||
*
|
||||
|
||||
@@ -5,93 +5,109 @@ import java.io.IOException;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
import java.security.cert.Certificate;
|
||||
|
||||
public class RSA2 {
|
||||
|
||||
private static final String SIGN_SHA256RSA_ALGORITHMS = "SHA256WithRSA";
|
||||
private static final String SIGN_SHA256RSA_ALGORITHMS = "SHA256WithRSA";
|
||||
|
||||
|
||||
public static String sign(String content, String privateKey, String characterEncoding) {
|
||||
|
||||
public static String sign(String content, String privateKey, String characterEncoding) {
|
||||
|
||||
return RSA.sign(content, privateKey, SIGN_SHA256RSA_ALGORITHMS, characterEncoding);
|
||||
}
|
||||
return RSA.sign(content, privateKey, SIGN_SHA256RSA_ALGORITHMS, characterEncoding);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* RSA签名
|
||||
*
|
||||
* @param content 待签名数据
|
||||
* @param privateKey 私钥
|
||||
* @param characterEncoding 编码格式
|
||||
* @return 签名值
|
||||
*/
|
||||
public static String sign(String content, PrivateKey privateKey, String characterEncoding) {
|
||||
return RSA.sign(content, privateKey, SIGN_SHA256RSA_ALGORITHMS, characterEncoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* RSA签名
|
||||
* @param content 待签名数据
|
||||
* @param privateKey 私钥
|
||||
* @param characterEncoding 编码格式
|
||||
* @return 签名值
|
||||
*/
|
||||
public static String sign(String content, PrivateKey privateKey ,String characterEncoding){
|
||||
return RSA.sign(content, privateKey, SIGN_SHA256RSA_ALGORITHMS, characterEncoding);
|
||||
}
|
||||
/**
|
||||
* RSA验签名检查
|
||||
*
|
||||
* @param content 待签名数据
|
||||
* @param sign 签名值
|
||||
* @param publicKey 公钥
|
||||
* @param characterEncoding 编码格式
|
||||
* @return 布尔值
|
||||
*/
|
||||
public static boolean verify(String content, String sign, String publicKey, String characterEncoding) {
|
||||
|
||||
/**
|
||||
* RSA验签名检查
|
||||
* @param content 待签名数据
|
||||
* @param sign 签名值
|
||||
* @param publicKey 公钥
|
||||
* @param characterEncoding 编码格式
|
||||
* @return 布尔值
|
||||
*/
|
||||
public static boolean verify(String content, String sign, String publicKey, String characterEncoding){
|
||||
|
||||
return RSA.verify(content, sign, publicKey, SIGN_SHA256RSA_ALGORITHMS, characterEncoding );
|
||||
}
|
||||
return RSA.verify(content, sign, publicKey, SIGN_SHA256RSA_ALGORITHMS, characterEncoding);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* RSA验签名检查
|
||||
*
|
||||
* @param content 待签名数据
|
||||
* @param sign 签名值
|
||||
* @param publicKey 公钥
|
||||
* @param characterEncoding 编码格式
|
||||
* @return 布尔值
|
||||
*/
|
||||
public static boolean verify(String content, String sign, PublicKey publicKey, String characterEncoding) {
|
||||
return RSA.verify(content, sign, publicKey, SIGN_SHA256RSA_ALGORITHMS, characterEncoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* RSA验签名检查
|
||||
* @param content 待签名数据
|
||||
* @param sign 签名值
|
||||
* @param publicKey 公钥
|
||||
* @param characterEncoding 编码格式
|
||||
* @return 布尔值
|
||||
*/
|
||||
public static boolean verify(String content, String sign, PublicKey publicKey, String characterEncoding){
|
||||
return RSA.verify(content, sign, publicKey, SIGN_SHA256RSA_ALGORITHMS, characterEncoding);
|
||||
}
|
||||
/**
|
||||
* RSA验签名检查
|
||||
*
|
||||
* @param content 待签名数据
|
||||
* @param sign 签名值
|
||||
* @param publicKey 公钥
|
||||
* @param characterEncoding 编码格式
|
||||
* @return 布尔值
|
||||
*/
|
||||
public static boolean verify(String content, String sign, Certificate publicKey, String characterEncoding) {
|
||||
PublicKey pubKey = publicKey.getPublicKey();
|
||||
return verify(content, sign, pubKey, characterEncoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密
|
||||
* @param content 密文
|
||||
* @param privateKey 商户私钥
|
||||
* @param characterEncoding 编码格式
|
||||
* @return 解密后的字符串
|
||||
* @throws GeneralSecurityException 解密异常
|
||||
* @throws IOException 解密异常
|
||||
*/
|
||||
public static String decrypt(String content, String privateKey, String characterEncoding) throws GeneralSecurityException, IOException {
|
||||
/**
|
||||
* 解密
|
||||
*
|
||||
* @param content 密文
|
||||
* @param privateKey 商户私钥
|
||||
* @param characterEncoding 编码格式
|
||||
* @return 解密后的字符串
|
||||
* @throws GeneralSecurityException 解密异常
|
||||
* @throws IOException 解密异常
|
||||
*/
|
||||
public static String decrypt(String content, String privateKey, String characterEncoding) throws GeneralSecurityException, IOException {
|
||||
return RSA.decrypt(content, privateKey, characterEncoding);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 得到私钥
|
||||
* @param key 密钥字符串(经过base64编码)
|
||||
* @throws GeneralSecurityException 加密异常
|
||||
* @return 私钥
|
||||
*/
|
||||
public static PrivateKey getPrivateKey(String key) throws GeneralSecurityException {
|
||||
return RSA.getPrivateKey(key);
|
||||
}
|
||||
/**
|
||||
* 得到私钥
|
||||
*
|
||||
* @param key 密钥字符串(经过base64编码)
|
||||
* @return 私钥
|
||||
* @throws GeneralSecurityException 加密异常
|
||||
*/
|
||||
public static PrivateKey getPrivateKey(String key) throws GeneralSecurityException {
|
||||
return RSA.getPrivateKey(key);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param content 加密文本
|
||||
* @param publicKey 公钥
|
||||
* @param cipherAlgorithm 算法
|
||||
* @param characterEncoding 编码类型
|
||||
* @return 加密后文本
|
||||
* @throws GeneralSecurityException 加密异常
|
||||
* @throws IOException IOException
|
||||
*/
|
||||
public static String encrypt(String content, String publicKey, String cipherAlgorithm, String characterEncoding ) throws GeneralSecurityException, IOException {
|
||||
return Base64.encode(RSA.encrypt(content.getBytes(characterEncoding), RSA.getPublicKey(publicKey), 2048, 11, cipherAlgorithm));
|
||||
}
|
||||
/**
|
||||
* @param content 加密文本
|
||||
* @param publicKey 公钥
|
||||
* @param cipherAlgorithm 算法
|
||||
* @param characterEncoding 编码类型
|
||||
* @return 加密后文本
|
||||
* @throws GeneralSecurityException 加密异常
|
||||
* @throws IOException IOException
|
||||
*/
|
||||
public static String encrypt(String content, String publicKey, String cipherAlgorithm, String characterEncoding) throws GeneralSecurityException, IOException {
|
||||
return Base64.encode(RSA.encrypt(content.getBytes(characterEncoding), RSA.getPublicKey(publicKey), 2048, 11, cipherAlgorithm));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user