package a.b.c;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import javax.crypto.Cipher;
public class JieJieMI {
public static String data = "RSA-java-代码123";
// 公钥
private static String publicKeyStr = "";
// 私钥
private static String privateKeyStr = "";
private static String qianmin = "SHA1withRSA";
private static String RSA = "RSA";
public static String getString(byte[] bytes) {
return Base64.getEncoder().encodeToString(bytes);
}
public static byte[] getByte(String s) {
return Base64.getDecoder().decode(s);
}
// 生成密钥对
private static void into() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGenerator;
keyPairGenerator = KeyPairGenerator.getInstance(RSA);
keyPairGenerator.initialize(1024);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
// 获取公钥,并以base64格式打印出来
PublicKey publicKey = keyPair.getPublic();
publicKeyStr = getString(publicKey.getEncoded());
// 获取私钥,并以base64格式打印出来
PrivateKey privateKey = keyPair.getPrivate();
privateKeyStr = getString(privateKey.getEncoded());
}
// 将base64编码后的公钥字符串转成PublicKey实例
private static PublicKey getPublicKey(String publicKey) throws Exception {
byte[] keyBytes = getByte(publicKey);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
return keyFactory.generatePublic(keySpec);
}
// 将base64编码后的私钥字符串转成PrivateKey实例
private static PrivateKey getPrivateKey(String privateKey) throws Exception {
byte[] keyBytes = getByte(privateKey);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
return keyFactory.generatePrivate(keySpec);
}
// 公钥加密
public static String encryptByPublicKey(String content) throws Exception {
// 获取公钥
PublicKey publicKey = getPublicKey(publicKeyStr);
Cipher cipher = Cipher.getInstance(RSA);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] cipherText = cipher.doFinal(content.getBytes());
return getString(cipherText);
}
// 私钥加密
public static String encryptByPrivateKey(String content) throws Exception {
// 获取私钥
PrivateKey privateKey = getPrivateKey(privateKeyStr);
Cipher cipher = Cipher.getInstance(RSA);
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] cipherText = cipher.doFinal(content.getBytes());
return getString(cipherText);
}
// 私钥解密
public static String decryptByPrivateKey(String content) throws Exception {
// 获取私钥
PrivateKey privateKey = getPrivateKey(privateKeyStr);
Cipher cipher = Cipher.getInstance(RSA);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] cipherText = getByte(content);
byte[] decryptText = cipher.doFinal(cipherText);
return new String(decryptText);
}
// 公钥解密
public static String decryptByPublicKey(String content) throws Exception {
// 获取公钥
PublicKey publicKey = getPublicKey(publicKeyStr);
Cipher cipher = Cipher.getInstance(RSA);
cipher.init(Cipher.DECRYPT_MODE, publicKey);
byte[] cipherText = getByte(content);
byte[] decryptText = cipher.doFinal(cipherText);
return new String(decryptText);
}
/**
* 签名
*
* @return 签名后经过base64处理的字符串
* @throws Exception
*/
public static String sign(String str) {
String base64Sign = "";
try {
PrivateKey priKey = getPrivateKey(privateKeyStr);
// 签名
Signature sign = Signature.getInstance(qianmin);
sign.initSign(priKey);
byte[] bysData = str.getBytes("UTF-8");
sign.update(bysData);
byte[] signByte = sign.sign();
base64Sign = getString(signByte);
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return base64Sign;
}
/**
* 数据验证
*
* @param signStr 加密后的数据
* @param verStr 原始字符
* @return
*/
public static boolean verify(String signStr, String verStr)
throws Exception {
boolean verfy = false;
try {
PublicKey pubKey = getPublicKey(publicKeyStr);
byte[] signed = getByte(signStr);
Signature sign = Signature.getInstance(qianmin);
sign.initVerify(pubKey);
sign.update(verStr.getBytes("UTF-8"));
verfy = sign.verify(signed);
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return verfy;
}
public static void main(String[] args) throws Exception {
into();
System.out.println(data);
// 公钥加密
String encryptedBytes = encryptByPublicKey(data);
System.out.println(encryptedBytes);
// 私钥解密
String decryptedBytes = decryptByPrivateKey(encryptedBytes);
System.out.println(decryptedBytes);
// 私钥加密
String encryptedBytes2 = encryptByPrivateKey(data);
System.out.println(encryptedBytes2);
// 公钥解密
String decryptedBytes2 = decryptByPublicKey(encryptedBytes2);
System.out.println(decryptedBytes2);
//签名
String s = sign(data);
System.out.println(s);
boolean rsa = verify(s, data);
System.out.println(rsa);
//解签
}
}
注意事项:
编码工具要统一
推荐使用Base64JDK 自带的
前段
import JSEncrypt from 'jsencrypt/bin/jsencrypt'
const publicKey = ''
// 加密
export function encryptPassword(txt) {
const encryptor = new JSEncrypt()
encryptor.setPublicKey(publicKey) // 设置公钥
return encryptor.encrypt(txt) // 对数据进行加密
}