java实现RSA的加密、解密




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 javax.crypto.Cipher;
 
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
 
public class RSAUtil {
	
	//生成秘钥对密钥大小为2048位  
	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编码
	@SuppressWarnings("restriction")
	public static String byte2Base64(byte[] bytes){
		BASE64Encoder encoder = new BASE64Encoder();
		return encoder.encode(bytes);
	}
	
	//Base64编码转字节数组
	@SuppressWarnings("restriction")
	public static byte[] base642Byte(String base64Key) throws IOException{
		BASE64Decoder decoder = new BASE64Decoder();
		return decoder.decodeBuffer(base64Key);
	}
	
	
	public static void main(String[] args) throws Exception {
		//1.获取密匙对
		KeyPair keyPair =RSAUtil.getKeyPair();
		//2.获取公密匙
		String publicKeyStr = RSAUtil.getPublicKey(keyPair);
		//3.获取私密匙
		String privateKeyStr = RSAUtil.getPrivateKey(keyPair);
		
		String message = "RSA加解密编码测试!!!!";
		//4.将Base64编码后的公钥 转换成PublicKey对象
		PublicKey publicKey = RSAUtil.string2PublicKey(publicKeyStr);
		//5.用公钥加密传输内容
		byte[] publicEncrypt = RSAUtil.publicEncrypt(message.getBytes(), publicKey);
		//6.对公钥加密后的内容进行Base64编码
		String byte2Base64 = RSAUtil.byte2Base64(publicEncrypt);
		
		//7.网络上传输的内容有Base64编码后的公钥 和 Base64编码后的公钥加密的内容 
		System.out.println("公钥加密内容进行Base64编码的结果:\n" + byte2Base64);
		
		
		
		//===================服务端解密================
		//将Base64编码后的私钥转换成PrivateKey对象
		PrivateKey privateKey = RSAUtil.string2PrivateKey(privateKeyStr);
		//对公钥加密后的内容Base64解码
		byte[] base642Byte = RSAUtil.base642Byte(byte2Base64);
		
		//用私钥解密
		byte[] privateDecrypt = RSAUtil.privateDecrypt(base642Byte, privateKey);
		//解密后的明文
		System.out.println("解密后的明文: \n" + new String(privateDecrypt));
	}
}

测试结果 

https://blog.csdn.net/qq_32523587/article/details/79092364 

猜你喜欢

转载自blog.csdn.net/qq_20957669/article/details/88026808