非对称加密算法
RSA是一种公钥密码算法,RSA算法属于非对称加密算法。它由三位数学家Rivest、Shamir和Adleman共同发明,并以他们名字的首字母命名。与非对称加密算法不同,对称加密算法使用相同的密钥进行加密和解密。RSA算法的特点是使用一对密钥,即公钥和私钥,其中公钥用于加密数据,而私钥用于解密数据。由于RSA算法在加密运算时速度较慢,它通常用于信息量较小的加密场合。
java实现RSA的简单加密解密
RSAUtil
package com.zhuyun.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 {
//生成秘钥对
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编码
public static String byte2Base64(byte[] bytes){
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(bytes);
}
//Base64编码转字节数组
public static byte[] base642Byte(String base64Key) throws IOException{
BASE64Decoder decoder = new BASE64Decoder();
return decoder.decodeBuffer(base64Key);
}
}
TestRSA
package com.zhuyun.test;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import org.junit.Test;
import com.zhuyun.rsa.RSAUtil;
public class TestRSA {
@Test
public void testRSA(){
try {
//===============生成公钥和私钥,公钥传给客户端,私钥服务端保留==================
//生成RSA公钥和私钥,并Base64编码
KeyPair keyPair = RSAUtil.getKeyPair();
String publicKeyStr = RSAUtil.getPublicKey(keyPair);
String privateKeyStr = RSAUtil.getPrivateKey(keyPair);
System.out.println("RSA公钥Base64编码:" + publicKeyStr);
System.out.println("RSA私钥Base64编码:" + privateKeyStr);
//=================客户端=================
//hello, i am infi, good night!加密
String message = "hello, i am infi, good night!";
//将Base64编码后的公钥转换成PublicKey对象
PublicKey publicKey = RSAUtil.string2PublicKey(publicKeyStr);
//用公钥加密
byte[] publicEncrypt = RSAUtil.publicEncrypt(message.getBytes(), publicKey);
//加密后的内容Base64编码
String byte2Base64 = RSAUtil.byte2Base64(publicEncrypt);
System.out.println("公钥加密并Base64编码的结果:" + byte2Base64);
//############## 网络上传输的内容有Base64编码后的公钥 和 Base64编码后的公钥加密的内容 #################
//===================服务端================
//将Base64编码后的私钥转换成PrivateKey对象
PrivateKey privateKey = RSAUtil.string2PrivateKey(privateKeyStr);
//加密后的内容Base64解码
byte[] base642Byte = RSAUtil.base642Byte(byte2Base64);
//用私钥解密
byte[] privateDecrypt = RSAUtil.privateDecrypt(base642Byte, privateKey);
//解密后的明文
System.out.println("解密后的明文: " + new String(privateDecrypt));
} catch (Exception e) {
e.printStackTrace();
}
}
}
测试结果如下:
RSA公钥Base64编码:MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA20p3Q88SmVUPpA+r/0hWY/LbMCsllwXK
UrXY13pG26BKsgtMm6IAGr6psS8cQ+AU75RpH3lTtt81wIfCHm9+vo4kVt66y8G0kfYSJ9Qlw2oH
5lwab8knPPb+NQNwE7WvceRczKh4XIEfNcgXZyKrcDUIpIsP6rNbZ70JYRhYiagqyLsb39ib4eO6
Zf6nmnrl3NPGeW4qFoivNudAld5E04gt2vCEEpuVt3x1/W3lVzD2gzz9rfkzjFvWwfGYjHMcZZNH
Z1h1G60ssSHh7WKV8f6EglPRQssimUKXX4De6g5SVABPs6MEOj0RYYyunJ8unA448u7/TCcMjV/b
vnndDwIDAQAB
RSA私钥Base64编码:MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDbSndDzxKZVQ+kD6v/SFZj8tsw
KyWXBcpStdjXekbboEqyC0ybogAavqmxLxxD4BTvlGkfeVO23zXAh8Ieb36+jiRW3rrLwbSR9hIn
1CXDagfmXBpvySc89v41A3ATta9x5FzMqHhcgR81yBdnIqtwNQikiw/qs1tnvQlhGFiJqCrIuxvf
2Jvh47pl/qeaeuXc08Z5bioWiK8250CV3kTTiC3a8IQSm5W3fHX9beVXMPaDPP2t+TOMW9bB8ZiM
cxxlk0dnWHUbrSyxIeHtYpXx/oSCU9FCyyKZQpdfgN7qDlJUAE+zowQ6PRFhjK6cny6cDjjy7v9M
JwyNX9u+ed0PAgMBAAECggEBALu/X6kQqw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