前端传给我密文,我在后端进行解密,然后再进行业务处理,但目前我不会解密,目前我知道加密的方法,如下
public static String rsaEncrypt(String data, String publicKey) {
String result = "";
try {
// 将Base64编码后的公钥转换成PublicKey对象
byte[] buffer = Base64.decode(publicKey);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
PublicKey pubKey = keyFactory.generatePublic(keySpec);
// 加密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] inputArray = data.getBytes();
int inputLength = inputArray.length;
System.out.println("加密字节数:" + inputLength);
// 最大加密字节数,超出最大字节数需要分组加密
int MAX_ENCRYPT_BLOCK = 117;
int real = 117;
// 标识
int offSet = 0;
byte[] resultBytes = {};
byte[] cache = {};
while (inputLength - offSet > 0) {
if (inputLength - offSet > MAX_ENCRYPT_BLOCK) {
if(!(inputArray[real + offSet] >= 0 && inputArray[real + offSet] <=127)){
real = real - 1;
continue;
}
cache = cipher.doFinal(inputArray, offSet, real);
offSet += real;
} 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);
}
result = Base64.encode(resultBytes);
} catch (Exception e) {
System.out.println("rsaEncrypt error:" + e.getMessage());
}
return result;
}
请问解密方法该如何写,谢谢