場景:如果項(xiàng)目上生成的秘鑰,公鑰是PKCS8格式,私鑰卻是PKCS1格式。需要在這種場景加解密的話可以直接使用下面工具類。
依賴
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.49</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.49</version>
</dependency>
工具類
特殊說明:私鑰解密的時候必須把私鑰源文件內(nèi)容整個傳入,不能刪除私鑰的文件頭和文件尾,并且不能刪除換行。文章來源:http://www.zghlxwxcb.cn/news/detail-510104.html
package com.XXX.XXX.common.util;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import javax.crypto.Cipher;
import java.io.Reader;
import java.io.StringReader;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Security;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;
/**
* RSAUtils加解密工具類
*
* @author dbz
* @date 2022-9-23
*/
public class RSAUtils {
/**
* PKCS1私鑰解密
*
* @param str str
* @return String
* @throws Exception Exception
*/
public static String decryptRSA(String str, String privateKey) throws Exception {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
Cipher rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC");
rsa.init(Cipher.DECRYPT_MODE, getPrivateKey(privateKey));
byte[] utf8 = rsa.doFinal(Base64.decodeBase64(str));
String result = new String(utf8, "UTF-8");
return result;
}
private static PrivateKey getPrivateKey(String privateKey) throws Exception {
Reader privateKeyReader = new StringReader(privateKey);
PEMParser privatePemParser = new PEMParser(privateKeyReader);
Object privateObject = privatePemParser.readObject();
if (privateObject instanceof PEMKeyPair) {
PEMKeyPair pemKeyPair = (PEMKeyPair) privateObject;
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
PrivateKey privKey = converter.getPrivateKey(pemKeyPair.getPrivateKeyInfo());
return privKey;
}
return null;
}
/**
* PKCS8公鑰加密
*
* @param str 加密字符串
* @return 密文
* @throws Exception 加密過程中的異常信息
*/
public static String publicKeyEncrypt(String str, String publicKey) throws Exception {
//base64編碼的公鑰
byte[] decoded = Base64.decodeBase64(publicKey);
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").
generatePublic(new X509EncodedKeySpec(decoded));
//RSA加密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
//當(dāng)長度過長的時候,需要分割后加密 117個字節(jié)
byte[] resultBytes = getMaxResultEncrypt(str, cipher);
String outStr = Base64.encodeBase64String(resultBytes);
return outStr;
}
private static byte[] getMaxResultEncrypt(String str, Cipher cipher) throws Exception {
byte[] inputArray = str.getBytes();
int inputLength = inputArray.length;
// 最大加密字節(jié)數(shù),超出最大字節(jié)數(shù)需要分組加密
int MAX_ENCRYPT_BLOCK = 117;
// 標(biāo)識
int offSet = 0;
byte[] resultBytes = {};
byte[] cache = {};
while (inputLength - offSet > 0) {
if (inputLength - offSet > MAX_ENCRYPT_BLOCK) {
cache = cipher.doFinal(inputArray, offSet, MAX_ENCRYPT_BLOCK);
offSet += MAX_ENCRYPT_BLOCK;
} else {
cache = cipher.doFinal(inputArray, offSet, inputLength - offSet);
offSet = inputLength;
}
resultBytes = Arrays.copyOf(resultBytes, resultBytes.length + cache.length);
System.arraycopy(cache, 0, resultBytes, resultBytes.length - cache.length, cache.length);
}
return resultBytes;
}
}
調(diào)用
文章來源地址http://www.zghlxwxcb.cn/news/detail-510104.html
到了這里,關(guān)于RSA加解密工具類(PKCS8公鑰加密,PKCS1私鑰解密)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!