RSA
是一种非对称加密算法(即加密和解密使用的是不同的密钥,我们将它们称为公钥和私钥)非对称加密的速度慢但安全性高。
package com.test.RSA;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
public class RSATest {
public static void main(String[] args) throws Exception {
//生成密钥
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(512);
KeyPair keyPair = kpg.generateKeyPair();
//获取公钥
byte[] publicKey = keyPair.getPublic().getEncoded();
//获取私钥
byte[] privateKey = keyPair.getPrivate().getEncoded();
//定义明文
String clearText = "hello world";
//使用公钥加密
byte[] cipherPublicText = encryptPublic(publicKey, clearText);
System.out.println("公钥加密后:"+Base64.getEncoder().encodeToString(cipherPublicText));
//使用私钥解密
byte[] clearPrivateText = decryptPrivate(privateKey,cipherPublicText);
System.out.println("私钥解密后:"+new String(clearPrivateText));
//使用私钥加密
byte[] cipherPrivateText = encryptPrivate(privateKey,clearText);
System.out.println("私钥加密后:"+Base64.getEncoder().encodeToString(cipherPrivateText));
//使用公钥解密
byte[] clearPublicText = decryptPublic(publicKey,cipherPrivateText);
System.out.println("公钥解密后:"+new String(clearPublicText));
}
private static byte[] decryptPublic(byte[] publicKey, byte[] cipherPrivateText) throws Exception {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey key = keyFactory.generatePublic(new X509EncodedKeySpec(publicKey));
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE,key);
byte[] clearPublicText = cipher.doFinal(cipherPrivateText);
return clearPublicText;
}
private static byte[] encryptPrivate(byte[] privateKey, String clearText) throws Exception {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey key = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKey));
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE,key);
byte[] cipherPrivateText = cipher.doFinal(clearText.getBytes());
return cipherPrivateText;
}
private static byte[] decryptPrivate(byte[] privateKey, byte[] cipherPublicText) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey key = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKey));
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE,key);
byte[] clearPrivateText = cipher.doFinal(cipherPublicText);
return clearPrivateText;
}
//使用公钥对明文进行加密
private static byte[] encryptPublic(byte[] publicKey, String clearText) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey key = keyFactory.generatePublic(new X509EncodedKeySpec(publicKey));
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE,key);
byte[] cipherPublicText = cipher.doFinal(clearText.getBytes());
return cipherPublicText;
}
}