RSA+Base64加解密Java工具类RSAUtil(公钥和私钥请自行生成)
import java.io.IOException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Properties;
import javax.crypto.Cipher;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class RSAUtil {
/**
* RSA+BASE64加密(先RSA加密,然后Base64加密)
* @param secretContent:secretContent要加密的内容
* @return
*/
public static String encrypt(String secretContent) {
String byte2Base64 = "";
try {
//===============生成公钥和私钥,公钥传给客户端,私钥服务端保留==================
//生成RSA公钥和私钥,并Base64编码
String filePath = "key.properties";
Properties prop = new Properties();
String PRIVATEKEY = null;
String PUBLICKEY = null;
prop.load(RSAUtil.class.getClassLoader().getResourceAsStream(filePath));
PRIVATEKEY = prop.getProperty("PRIVATEKEY");
PUBLICKEY = prop.getProperty("PUBLICKEY");
//=================客户端================
//将Base64编码后的公钥转换成PublicKey对象
PublicKey publicKey = string2PublicKey(PUBLICKEY);
//用公钥加密
byte[] publicEncrypt = publicEncrypt(secretContent.getBytes(), publicKey);
//加密后的内容Base64编码
byte2Base64 = byte2Base64(publicEncrypt);
System.out.println(byte2Base64);
} catch (Exception e) {
e.printStackTrace();
}
return byte2Base64;
}
/**
* BASE64+RAS解密(先Base64解密,然后RSA解密)
* @param decodeContent :解密字符串
*/
public static String decode(String decodeContent) {
byte[] privateDecrypt = new byte[] {};
try {
//===============生成公钥和私钥,公钥传给客户端,私钥服务端保留==================
//生成RSA公钥和私钥,并Base64编码
String filePath = "key.properties";
Properties prop = new Properties();
String PRIVATEKEY = null;
String PUBLICKEY = null;
prop.load(RSAUtil.class.getClassLoader().getResourceAsStream(filePath));
PRIVATEKEY = prop.getProperty("PRIVATEKEY");
PUBLICKEY = prop.getProperty("PUBLICKEY");
//===================服务端================
//将Base64编码后的私钥转换成PrivateKey对象
PrivateKey privateKey = string2PrivateKey(PRIVATEKEY);
//加密后的内容Base64解码
byte[] base642Byte = base642Byte(decodeContent);
//用私钥解密
privateDecrypt = privateDecrypt(base642Byte, privateKey);
}catch(Exception e) {
e.printStackTrace();
}
return new String(privateDecrypt);
}
//生成秘钥对
public static KeyPair getKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
return keyPair;
}
//获取公钥(Base64编码)
public static String getPublicKey(KeyPair keyPair){
PublicKey publicKey = keyPair.getPublic();
byte[] bytes = publicKey.getEncoded();
return byte2Base64(bytes);
}
//获取私钥(Base64编码)
public static String getPrivateKey(KeyPair keyPair){
PrivateKey privateKey = keyPair.getPrivate();
byte[] bytes = privateKey.getEncoded();
return byte2Base64(bytes);
}
//将Base64编码后的公钥转换成PublicKey对象
public static PublicKey string2PublicKey(String pubStr) throws Exception{
byte[] keyBytes = base642Byte(pubStr);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}
//将Base64编码后的私钥转换成PrivateKey对象
public static PrivateKey string2PrivateKey(String priStr) throws Exception{
byte[] keyBytes = base642Byte(priStr);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
//公钥加密
public static byte[] publicEncrypt(byte[] content, PublicKey publicKey) throws Exception{
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] bytes = cipher.doFinal(content);
return bytes;
}
//私钥解密
public static byte[] privateDecrypt(byte[] content, PrivateKey privateKey) throws Exception{
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] bytes = cipher.doFinal(content);
return bytes;
}
//字节数组转Base64编码
public static String byte2Base64(byte[] bytes){
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(bytes);
}
//Base64编码转字节数组
public static byte[] base642Byte(String base64Key) throws IOException{
BASE64Decoder decoder = new BASE64Decoder();
return decoder.decodeBuffer(base64Key);
}
}
key.properties(结尾加上“”是为了换行识别)
PUBLICKEY=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDlukf0JloB6RyjRZRid4JIoQ86
m40o0wI7uHj6zqG/OYjpbu2SZ7FLEFNlzng+in8XUBvHrC+i9hAlho0Giix7SQEQ
u43l05FbHQrIqBQCOgwMbg4h5FZlILXJfcXCLlFXOSC5dcDvXAEfBNpqFNAIdlOW
drNvBpMnjzd9L+U2xQIDAQAB
PRIVATEKEY=MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAOW6R/QmWgHpHKNF
lGJ3gkihDzqbjSjTAju4ePrOob85iOlu7ZJnsUsQU2XOeD6KfxdQG8esL6L2ECWG
jQaKLHtJARC7jeXTkVsdCsioFAI6DAxuDiHkVmUgtcl9xcIuUVc5ILl1wO9cAR8E
2moU0Ah2U5Z2s28GkyePN30v5TbFAgMBAAECgYB87QMqAx0ZQuJe63yY23KO+n7k
mSh3VeA2B8hKY8nTyn7oqnkpD5h7QIKJr3D2WaxpShgBSmKRVQxMy9GvB77NuIB0
swQcyMcVU7FtwfwQ+SwxmcDgl716sjs0D4p9HrSSubpboOo2bHIKgr1XlXnWMJ5s
ElKMj5HTNPTCJWQzoQJBAPfw8/2ikO5KM6rC73GrIYTuLyMf1TqYsCxT0KXThwF+
ksCtIdUZlgJGwscSiUhPmPTbDBMhb9hYaEkOe7VESz0CQQDtMcc5Ig+5BgftqtCI
RvdpZAnFZmYsUqdCKZi9+34e3mEne4jHLZP10HaRsf72T87fgEAUx/jFufKmQ4Dv
hHIpAkEAwxvFpO3shte1WMcVjalRMz4LseQ282l8GSXLpvKR5scnzJ9xZDRCK4Br
fiREC7c3cld9ZQCG9wR67CKNjtMzMQJBAOjA7hQcXn1bc7A+7D6uFN5wcj4oy542
xTZhhyb2gL5dzcxmpsN88DJJC/DQT7b4fbJIMZmy2WV9oKSSnRVsTHECQQDfYQMC
a9tLDZZXln0d8SVzLXlZCecESTFlaoDcyoos8cGQ0jXOuXqt82irKgxK8q2qoKu0
qJDFVLAAlIGcGhbH
大致用法:
String USERNAME = request.getParameter("username");
String PASSWORD =request.getParameter("password");
USERNAME = USERNAME.replace(" ", "+");//替换里面的"+"
PASSWORD = PASSWORD.replace(" ", "+");//替换里面的"+"
//USERNAME=USERNAME.replaceAll(" ","\
\
");//jemeter使用
//PASSWORD=PASSWORD.replaceAll( " ","\
\
");//jemeter使用
...
USERNAME = RSAUtil.decode(USERNAME);
PASSWORD = RSAUtil.decode(PASSWORD);
...