看了几个别人写的,不好用,还是自动总结一下,不BB直接上代码,
缺依赖自己根据 import 找
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
public class RSAUtil {
private static final String RSA = "RSA";
public static void main(String[] args) throws Exception {
RsaKeyPair keyPair = generateKeyPair();
String str = "曾特喵穷,哦豁,芭比Q了";
System.out.println("公钥加密");
String encodePublic = encryptByPublicKey(keyPair.publicKey, str);
System.out.println("私钥解密");
String decodePrivate = decryptByPrivateKey(keyPair.privateKey, encodePublic);
System.out.println("私钥加密");
String encodePrivate = encryptByPrivateKey(keyPair.privateKey, str);
System.out.println("公钥解密");
String decodePublic = decryptByPublicKey(keyPair.publicKey, encodePrivate);
}
/**
* 公钥加密
*
* @param publicKey
* @param data
* @return
* @throws Exception
*/
public static String encryptByPublicKey(String publicKey, String data) throws Exception {
Cipher cipher = cipherInstance(Cipher.ENCRYPT_MODE, true, publicKey);
byte[] bytes = cipher.doFinal(data.getBytes());
String encrypt = Base64.encodeBase64URLSafeString(bytes);
print(true, true, publicKey, data, encrypt);
return encrypt;
}
/**
* 私钥加密
*
* @param privateKey
* @param data
* @return
* @throws Exception
*/
public static String encryptByPrivateKey(String privateKey, String data) throws Exception {
Cipher cipher = cipherInstance(Cipher.ENCRYPT_MODE, false, privateKey);
byte[] bytes = cipher.doFinal(data.getBytes());
String encrypt = Base64.encodeBase64URLSafeString(bytes);
print(false, true, privateKey, data, encrypt);
return encrypt;
}
/**
* 公钥解密
*
* @param publicKey
* @param data
* @return
* @throws Exception
*/
public static String decryptByPublicKey(String publicKey, String data) throws Exception {
Cipher cipher = cipherInstance(Cipher.DECRYPT_MODE, true, publicKey);
byte[] bytes = cipher.doFinal(Base64.decodeBase64(data));
String decrypt = new String(bytes, StandardCharsets.UTF_8);
print(true, false, publicKey, data, decrypt);
return decrypt;
}
/**
* 私钥解密
*
* @param privateKey
* @param data
* @return
* @throws Exception
*/
public static String decryptByPrivateKey(String privateKey, String data) throws Exception {
Cipher cipher = cipherInstance(Cipher.DECRYPT_MODE, false, privateKey);
byte[] bytes = cipher.doFinal(Base64.decodeBase64(data));
String decrypt = new String(bytes, StandardCharsets.UTF_8);
print(false, false, privateKey, data, decrypt);
return decrypt;
}
public static Cipher cipherInstance(int mode, boolean isPublic, String input) throws Exception {
byte[] bytes = Base64.decodeBase64(input);
KeyFactory factory = KeyFactory.getInstance(RSA);
Key key;
if (isPublic) {
key = factory.generatePublic(new X509EncodedKeySpec(bytes));
} else {
key = factory.generatePrivate(new PKCS8EncodedKeySpec(bytes));
}
Cipher cipher = Cipher.getInstance(RSA);
cipher.init(mode, key);
return cipher;
}
public static RsaKeyPair generateKeyPair() throws NoSuchAlgorithmException {
return generateKeyPair(1024);
}
public static RsaKeyPair generateKeyPair(int keySize) throws NoSuchAlgorithmException {
KeyPairGenerator kpg = KeyPairGenerator.getInstance(RSA);
// 密钥长度
kpg.initialize(keySize);
// 生成密钥对
KeyPair keyPair = kpg.generateKeyPair();
// 公钥
Key publicKey = keyPair.getPublic();
String publicKeyStr = Base64.encodeBase64URLSafeString(publicKey.getEncoded());
// 私钥
Key privateKey = keyPair.getPrivate();
String privateKeyStr = Base64.encodeBase64URLSafeString(privateKey.getEncoded());
System.out.println("公钥: " + publicKeyStr);
System.out.println("私钥: " + privateKeyStr);
System.out.println("--------------------------------------------------");
return new RsaKeyPair(publicKeyStr, privateKeyStr);
}
private static void print(boolean isPublic, boolean isEncrypt, String key, String data, String result) {
System.out.println((isEncrypt ? "明" : "密") + "文: " + data);
System.out.println((isPublic ? "公" : "私") + "钥: " + key);
System.out.println((isEncrypt ? "加" : "解") + "密: " + result);
System.out.println("--------------------------------------------------");
}
public static class RsaKeyPair {
private final String publicKey;
private final String privateKey;
public RsaKeyPair(String publicKey, String privateKey) {
this.publicKey = publicKey;
this.privateKey = privateKey;
}
public String getPublicKey() {
return publicKey;
}
public String getPrivateKey() {
return privateKey;
}
}
}