Java---RSA私钥加密解密,公钥加密解密--方法
package com.my.demo.utils; import com.my.demo.common.Constants; import com.my.experiment.exception.OptException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.apache.commons.cod
·
package com.my.demo.utils;
import com.my.demo.common.Constants;
import com.my.experiment.exception.OptException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Component;
import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;
@Component
public class RsaUtil {
/**
* 日志记录器
*/
private static final Logger LOGGER = LoggerFactory.getLogger(RsaUtil .class);
private static final String KEY_ALGORITHM = "RSA";
private static final String PK = Constants.getPublicKey();
private static final String SK = Constants.getPrivateKey();
/**
* 使用RSA公钥加密
*/
public static String encryptByPublicKey(String str) {
return Base64.encodeBase64String(getMaxResultEncrypt(str, getPublicKey(Cipher.ENCRYPT_MODE)));
}
/**
* 使用公钥解密
*/
public static String decryptByPublicKey(String data) {
try {
return new String(getMaxResultDecrypt(data, getPublicKey(Cipher.DECRYPT_MODE)));
}catch (Exception e){
throw new OptException("解密失败|"+e.getMessage());
}
}
/**
* 使用RSA私钥解密
*
* @param str 加密字符串
*/
public static String decryptByPrivateKey(String str) {
return new String(getMaxResultDecrypt(str, getPrivateKey(Cipher.DECRYPT_MODE)));
}
/**
* 使用RSA私钥加密
* @param data 加密数据
*/
public static String encryptByPrivateKey(String data) {
try {
return Base64.encodeBase64String(getMaxResultEncrypt(data, getPrivateKey(Cipher.ENCRYPT_MODE)));
} catch (Exception e) {
throw new OptException("加密失败|"+e.getMessage());
}
}
/**
* 当长度过长的时候,需要分割后加密 117个字节
*/
private static byte[] getMaxResultEncrypt(String str, Cipher cipher){
try {
byte[] inputArray = str.getBytes(StandardCharsets.UTF_8.name());
int inputLength = inputArray.length;
// 最大加密字节数,超出最大字节数需要分组加密
int maxEncryptBlock = 117;
// 标识
int offSet = 0;
byte[] resultBytes = {};
byte[] cache;
while (inputLength - offSet > 0) {
if (inputLength - offSet > maxEncryptBlock) {
cache = cipher.doFinal(inputArray, offSet, maxEncryptBlock);
offSet += maxEncryptBlock;
} 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;
}catch (Exception e){
e.printStackTrace();
throw new OptException("加密处理失败,"+e.getMessage());
}
}
/**
* 当长度过长的时候,需要分割后解密 128个字节
*/
private static byte[] getMaxResultDecrypt(String str, Cipher cipher) {
try {
byte[] inputArray = Base64.decodeBase64(str.getBytes(StandardCharsets.UTF_8.name()));
int inputLength = inputArray.length;
// 最大解密字节数,超出最大字节数需要分组加密
int maxEncryptBlock = 128;
int offSet = 0;
byte[] resultBytes = {};
byte[] cache;
while (inputLength - offSet > 0) {
if (inputLength - offSet > maxEncryptBlock) {
cache = cipher.doFinal(inputArray, offSet, maxEncryptBlock);
offSet += maxEncryptBlock;
} 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;
}catch (Exception e){
e.printStackTrace();
throw new OptException("解密数据处理异常,"+e.getMessage());
}
}
/**
* 根据加解密类型处理公钥
*
*/
public static Cipher getPublicKey(int mode) {
try {
String publicKey = formatString(PK);
byte[] decoded = Base64.decodeBase64(publicKey);
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(decoded);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
PublicKey key = keyFactory.generatePublic(x509EncodedKeySpec);
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(mode, key);
return cipher;
} catch (Exception e) {
throw new OptException("转换公钥异常====>>" + e.getMessage());
}
}
/**
* 根据加解密类型处理私钥
*/
public static Cipher getPrivateKey(int mode) {
try {
//mode 加解密模式 ENCRYPT_MODE = 1 DECRYPT_MODE = 2
String privateKey = formatString(SK);
byte[] decoded = Base64.decodeBase64(privateKey);
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(decoded);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
PrivateKey key = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(mode, key);
return cipher;
} catch (Exception e) {
throw new OptException("转换私钥异常====>>" + e.getMessage());
}
}
public static String formatString(String source) {
if (source == null) {
return null;
}
return source.replace("\\r", "").replace("\\n", "").trim().replace(" ","");
}
public static void main(String[] args) throws Exception {
String xxStr = "的发挥很大的22@@@";
//加密
String bytes1 = RsaUtil.encryptByPrivateKey(xxStr);
String bytes2 = RsaUtil.encryptByPublicKey(xxStr);
log.info("公钥加密2:{}",bytes2);
log.info("私钥加密1:{}",bytes1);
//解密
log.info("私钥解密密2:{}",RsaUtil.decryptByPrivateKey(bytes2));
log.info("公钥解密密1:{}",RsaUtil.decryptByPublicKey(bytes1));
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)