import sun.misc.BASE64Decoder;
import javax.crypto.Cipher;
import javax.servlet.ServletContext;
import java.io.*;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
public class RSASignatureUtils {
private static final String KEY_ALGORITHM = "RSA"; // 密钥算法
private static final String SIGN_ALGORITHM = "SHA256withRSA";
public static final String CRT_FILE_NAME = "rsaPublic.pem";
public static final String PRI_KEY_NAME = "rsaPrivate.pem";
/**
* @param data 原数据
* @param encoding 数据的编码
* @return 一个base64的签名
* @throws Exception
*/
public static String encryptByPrivateKey(ServletContext servlet, String data, String encoding) throws Exception {
try {
PrivateKey privateKey = getPrivateKey(servlet);
byte[] sign = sign(data.getBytes(encoding), privateKey);
return Base64.getEncoder().encodeToString(sign);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 签名(原数据,私钥) 两要素
*
* @param data
* @param privateKey
* @return
*/
public static byte[] sign(byte[] data, PrivateKey privateKey) {
try {
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded());
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
PrivateKey priKey = keyFactory.generatePrivate(keySpec);
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, priKey);
return cipher.doFinal(data);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 解密签名
*
* @param servlet
* @param encoding
* @param sign
* @return
*/
public static String decryptByPublicKey(ServletContext servlet, String encoding, String sign) {
try {
PublicKey publicKey = getPublicKey(servlet);
byte[] byteSign = Base64.getDecoder().decode(sign);
byte[] result = decrypt(publicKey.getEncoded(), byteSign);
return new String(result, encoding);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 解密签名(公钥,签名)
*
* @param publicKey
* @param dataSign
* @return
*/
public static byte[] decrypt(byte[] publicKey, byte[] dataSign) {
try {
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
PublicKey pubKey = keyFactory.generatePublic(keySpec);
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, pubKey);
return cipher.doFinal(dataSign);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获取私钥
*
* @return
* @throws Exception
*/
private static PrivateKey getPrivateKey(ServletContext servlet) throws Exception {
String TEST_PRIVATE_KEY = readFile(servlet, PRI_KEY_NAME);
TEST_PRIVATE_KEY = TEST_PRIVATE_KEY.replace("-----BEGIN PRIVATE KEY-----", "")
.replace("-----END PRIVATE KEY-----", "");
return loadPrivateKey(TEST_PRIVATE_KEY);
}
/**
* 加载私钥
*
* @param privateKeyStr
* @return
*/
public static RSAPrivateKey loadPrivateKey(String privateKeyStr) {
try {
BASE64Decoder base64Decoder = new BASE64Decoder();
byte[] buffer = base64Decoder.decodeBuffer(privateKeyStr);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获取公钥
*
* @return
*/
private static PublicKey getPublicKey(ServletContext servlet) throws Exception {
String TEST_PUBLIC_KEY = readFile(servlet, CRT_FILE_NAME);
TEST_PUBLIC_KEY = TEST_PUBLIC_KEY.replace("-----BEGIN PUBLIC KEY-----", "")
.replace("-----END PUBLIC KEY-----", "");
return loadPublicKey(TEST_PUBLIC_KEY);
}
/**
* 加载公钥
*
* @param publicKeyStr
* @return
*/
public static RSAPublicKey loadPublicKey(String publicKeyStr) {
try {
BASE64Decoder base64Decoder = new BASE64Decoder();
byte[] buffer = base64Decoder.decodeBuffer(publicKeyStr);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
return (RSAPublicKey) keyFactory.generatePublic(keySpec);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 读取私钥公钥
*
* @param servlet
* @param fileName
* @return
* @throws Exception
*/
public static String readFile(ServletContext servlet, String fileName) throws Exception {
String path = servlet.getRealPath("/WEB-INF/rsa/" + fileName);
File file = new File(path);
FileInputStream fStream = null;
InputStreamReader iReader = null;
BufferedReader bReader = null;
try {
if (file.exists()) {
fStream = new FileInputStream(file);
iReader = new InputStreamReader(fStream);
bReader = new BufferedReader(iReader);
StringBuffer result = new StringBuffer();
String line = null;
while ((line = bReader.readLine()) != null) {
result.append(line);
}
return result.toString();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
bReader.close();
iReader.close();
fStream.close();
}
return "File not found!";
}
}
RSA工具类
猜你喜欢
转载自blog.csdn.net/qq_38618691/article/details/115250973
今日推荐
周排行