public static String encryptString(String input, String channel) throws Exception {
if (StringUtils.isBlank(channel)) {
throw new BusinessException("缺少channel");
}
Cipher cipher = Cipher.getInstance("RSA");
RSAPublicKey pubKey = (RSAPublicKey) getPublicKey(RSAEncrypt.class.getResourceAsStream("/security/" + channel + "/publicKey"));
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] cipherText = cipher.doFinal(input.getBytes());
// 加密后的东西
if (logger.isDebugEnabled()) {
logger.debug("cipher: " + new String(cipherText));
}
return Base64.encodeBase64String(cipherText);
}
public static PublicKey getPublicKey(InputStream inputStream) throws Exception {
DataInputStream dis = new DataInputStream(inputStream);
byte[] keyBytes = new byte[inputStream.available()];
dis.readFully(keyBytes);
dis.close();
X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePublic(spec);
}
/**
* 通过私有密匙加密
*
* @param input 需要解密的内容
* @param privateKeyStr 私有密匙
* @author wangxy2
* @since 2021-06-18
*/
public static String encryptStringByPrivateKey(String input, String privateKeyStr, String charsetName) throws Exception {
if (StringUtils.isBlank(privateKeyStr)) {
throw new BusinessException("缺少密匙");
}
Cipher cipher = Cipher.getInstance("RSA");
RSAPrivateKey privateKey = (RSAPrivateKey) getPrivateKey(privateKeyStr);
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] cipherText;
if (StringUtils.isNotEmpty(charsetName)) {
cipherText = cipher.doFinal(input.getBytes());
} else {
cipherText = cipher.doFinal(input.getBytes(charsetName));
}
// 加密后的东西
if (logger.isDebugEnabled()) {
logger.debug("private_encrypt_cipher: " + new String(cipherText));
}
return Base64.encodeBase64String(cipherText);
}
public static PrivateKey getPrivateKey(String privateKeyStr) throws Exception {
PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(new Base64().decode(privateKeyStr.getBytes()));
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePrivate(priPKCS8);
}
/**
* @param input 需要解密的内容
* @param channel 渠道
* @return
* @throws Exception
*/
public static String decryptString(String input, String channel) {
if (StringUtils.isBlank(channel)) {
throw new BusinessException("缺少channel");
}
Cipher cipher;
try {
cipher = Cipher.getInstance("RSA");
RSAPrivateKey privKey;
privKey = (RSAPrivateKey) getPrivateKey(RSADecrypt.class.getResourceAsStream("/security/" + channel + "/privateKey"));
// 开始解密
cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] plainText = cipher.doFinal(Base64.decodeBase64(input));
if (logger.isDebugEnabled()) {
logger.debug("cipher: " + new String(plainText));
}
return new String(plainText);
} catch (Exception e) {
logger.error(ExceptionCollect.collectExceptionStackMsg(e));
}
return null;
}
public static PrivateKey getPrivateKey(InputStream inputStream) throws Exception {
DataInputStream dis = new DataInputStream(inputStream);
byte[] keyBytes = new byte[inputStream.available()];
dis.readFully(keyBytes);
dis.close();
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePrivate(spec);
}
/**
* 通过私有密匙解密
*
* @param input 需要解密的内容
* @param privateKeyStr 私有密匙
* @author wangxy2
* @since 2021-06-18
*/
public static String decryptStringByPrivateKey(String input, String privateKeyStr, String charsetName) {
if (StringUtils.isBlank(privateKeyStr)) {
throw new BusinessException("缺少密匙");
}
Cipher cipher;
try {
cipher = Cipher.getInstance("RSA");
RSAPrivateKey privateKey = (RSAPrivateKey) getPrivateKey(privateKeyStr);
// 开始解密
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] plainText = cipher.doFinal(Base64.decodeBase64(input));
if (logger.isDebugEnabled()) {
logger.debug("private_decrypt_cipher: " + new String(plainText));
}
return new String(plainText);
} catch (Exception e) {
logger.error(ExceptionCollect.collectExceptionStackMsg(e));
}
return null;
}
public static PrivateKey getPrivateKey(String privateKeyStr) throws Exception {
PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(new Base64().decode(privateKeyStr.getBytes()));
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePrivate(priPKCS8);
}
AlipaySignature.rsaEncrypt()
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.13.50.ALL</version>
</dependency>