
加解密+签名验签.png
import javax.crypto.Cipher;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
/**
* RSA工具类
* 提供rsa加密,ras解密,加签,验签;生成密钥对;
*
* @author zxq
*/
public class RSAUtil {
/**
* @param content 【未加密的】字符串
* @param pubStr 【base64编码的】公钥字符串
* @return 【base64编码的】【Rsa加密的】字符串
* @throws Exception 异常
*/
public static String pubStrEncrypt(String content, String pubStr) throws Exception {
// 公钥对象
PublicKey publicKey = string2PublicKey(pubStr);
// 用公钥加密
byte[] publicEncrypt = publicEncrypt(content.getBytes(), publicKey);
// 加密后的内容Base64编码
return byte2Base64(publicEncrypt);
}
/**
* @param content 【加密的】【base64编码的】字符串
* @param priStr 【base64编码的】私钥字符串
* @return 解密的字符串
* @throws Exception 异常
*/
public static String priStrDecrypt(String content, String priStr) throws Exception {
// 私钥对象
PrivateKey privateKey = string2PrivateKey(priStr);
// 加密后的内容Base64解码
byte[] base642Byte = base642Byte(content);
// 用私钥解密
byte[] privateDecrypt = privateDecrypt(base642Byte, privateKey);
//解密后的明文
return new String(privateDecrypt);
}
/**
* 加签
*/
public static String priStrAutograph(String unSignedData, String priStr) throws Exception {
PrivateKey privateKey = string2PrivateKey(priStr);
Signature signature = Signature.getInstance("Sha1WithRSA");
signature.initSign(privateKey);
signature.update(unSignedData.getBytes("UTF-8"));
return byte2Base64(signature.sign());
}
/**
* 验签
*/
public static boolean pubStrCheckSign(String unSignedData, String signedData, String pubStr) throws Exception {
PublicKey publicKey = string2PublicKey(pubStr);
Signature signature = Signature.getInstance("Sha1WithRSA");
signature.initVerify(publicKey);
signature.update(unSignedData.getBytes("UTF-8"));
return signature.verify(base642Byte(signedData));
}
/**
* 生成秘钥对
*/
public static KeyPair getKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(1024);
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编码的公钥字符串 -> 公钥对象
*/
private 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编码的私钥字符串 -> 私钥对象
*/
private 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;
}
/**
* 公钥加密:根据 【未加密的】字节数组+公钥对象 -> 【加密的】字节组数
*/
private 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;
}
/**
* 私钥解密:根据【加密的】字节数组+私钥对象 -> 【未加密的】字节数组
*/
private 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编码的】字符串
*/
private static String byte2Base64(byte[] bytes) {
// base64编码
byte[] encode = Base64.getEncoder().encode(bytes);
return new String(encode);
}
/**
* 【Base64编码的】字符串 -> 字节数组
*/
private static byte[] base642Byte(String base64Key) throws Exception {
// 解码
return Base64.getDecoder().decode(base64Key);
}
}
import com.wonders.eshimin.utils.RSAUtil;
import org.junit.Test;
import java.security.KeyPair;
public class Test1 {
@Test
public void testRSA() {
try {
// ===============生成公钥和私钥,公钥传给客户端,私钥服务端保留==================
// 生成【Base64编码】RSA公钥和私钥
KeyPair keyPair = RSAUtil.getKeyPair();
String publicKeyStr = RSAUtil.getPublicKey(keyPair);
String privateKeyStr = RSAUtil.getPrivateKey(keyPair);
System.out.println("【Base64编码】RSA公钥字符串-加解密:" + publicKeyStr);
System.out.println("【Base64编码】RSA私钥字符串-加解密:" + privateKeyStr);
KeyPair keyPair1 = RSAUtil.getKeyPair();
String publicKeyStr1 = RSAUtil.getPublicKey(keyPair1);
String privateKeyStr1 = RSAUtil.getPrivateKey(keyPair1);
System.out.println("【Base64编码】RSA公钥字符串-签名验签:" + publicKeyStr1);
System.out.println("【Base64编码】RSA私钥字符串-签名验签:" + privateKeyStr1);
//=================客户端=================
// hello, my name is 孙悟空, 1+1=2!加密
String message = "hello, my name is 孙悟空, 1+1=2";
String sign = RSAUtil.priStrAutograph(message, privateKeyStr1);
System.out.println("签名:" + sign);
//加密后的内容
String res = RSAUtil.pubStrEncrypt(message, publicKeyStr);
System.out.println("公钥加密的Base64编码的结果:" + res);
//############## 网络上传输的内容有Base64编码后的公钥(2) 和 Base64编码后的公钥加密的内容 和 签名 #################
//===================服务端================
//解密后的明文
String mw = RSAUtil.priStrDecrypt(res, privateKeyStr);
System.out.println("解密后的明文: " + mw);
boolean check = RSAUtil.pubStrCheckSign(mw, sign, publicKeyStr1);
System.out.println("验签:" + check);
} catch (Exception e) {
e.printStackTrace();
}
}
}
网友评论