Java(110):非对称加密RSA的使用(KeyPair生成密钥)
RSA 算法是一种非对称加解密算法。服务方生成一对 RSA 密钥,即公钥 + 私钥,将公钥提供给调用方,调用方使用公钥对数据进行加密后,服务方根据私钥进行解密。
1、RSA生成密钥方法
keyPairGen.initialize(1024);
//生成"密钥对"对象
KeyPair keyPair = keyPairGen.generateKeyPair();
//分别获取私钥和公钥对象
RSAPrivateKey PrivateKey =(RSAPrivateKey) keyPair.getPrivate();
RSAPublicKey publicKey =(RSAPublicKey) keyPair.getPublic();
2、RSA加密和解密方法
/**
* 公钥加密
* @param publicKey 公钥
* @param obj 明文
* @return byte[] 密文
*/
public static byte[] encrypt(RSAPublicKey publicKey, byte[] obj) throws Exception {
Cipher cipher =Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE,publicKey);
//返回加密后的内容
return cipher.doFinal(obj);
}
/**
* 私钥解密
* @param privateKey 公钥
* @param obj 密文
* @return byte[] 密文
*/
public static byte[] decrypt(RSAPrivateKey privateKey, byte[] obj)throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
//返回解密后的数组
return cipher.doFinal(obj);
}
3、Base64编码和解码
maven
<dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.11</version> </dependency>
/**
* 编码
* @param txt byte字节数组
* @return encode Base64编码
*/
public static byte[] encode(byte[] txt) {
return org.apache.commons.codec.binary.Base64.encodeBase64(txt);
}
/**
* 解码
* @param txt 编码后的byte
* @return decode Base64解码
*/
public static byte[] decode(String txt){
return org.apache.commons.codec.binary.Base64.decodeBase64(txt);
}
4、调用加解密
public static void main(String[] args)throws Exception {
//获取RSA算法的密钥生成器对象
KeyPairGenerator keyPairGen =KeyPairGenerator.getInstance("RSA");
//设定密钥长度为1024位
keyPairGen.initialize(1024);
//生成"密钥对"对象
KeyPair keyPair = keyPairGen.generateKeyPair();
//分别获取私钥和公钥对象
RSAPrivateKey PrivateKey =(RSAPrivateKey) keyPair.getPrivate();
RSAPublicKey publicKey =(RSAPublicKey) keyPair.getPublic();
//执行加密和解密过程
String InData="Hello World!";
//得到要加密内容的数组
byte[] byteInData =InData.getBytes("UTF-8");
//用公钥加密
byte[] cipherByte= encrypt(publicKey,byteInData); //RSA加密
String cipher=new String(encode(cipherByte)); //Base64a编码
System.out.println("公钥加密,密文:"+cipher);
//用私钥解密
byte[] plain =decrypt(PrivateKey,decode(cipher)); //Base64a解码
System.out.println("私钥解密,明文:"+new String(plain)); //RSA解密
}
5、RSA加解密代码示例:
package jmj;
import javax.crypto.Cipher;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
/**
* Description :
*
* @author : HMF
* Date : Created in 20:32 2023/3/13
* @version :
*/
public class RSATest {
public static void main(String[] args)throws Exception {
//获取RSA算法的密钥生成器对象
KeyPairGenerator keyPairGen =KeyPairGenerator.getInstance("RSA");
//设定密钥长度为1024位
keyPairGen.initialize(1024);
//生成"密钥对"对象
KeyPair keyPair = keyPairGen.generateKeyPair();
//分别获取私钥和公钥对象
RSAPrivateKey PrivateKey =(RSAPrivateKey) keyPair.getPrivate();
RSAPublicKey publicKey =(RSAPublicKey) keyPair.getPublic();
//执行加密和解密过程
String InData="Hello World!";
//得到要加密内容的数组
byte[] byteInData =InData.getBytes("UTF-8");
//用公钥加密
byte[] cipherByte= encrypt(publicKey,byteInData); //RSA加密
String cipher=new String(encode(cipherByte)); //Base64a编码
System.out.println("公钥加密,密文:"+cipher);
//用私钥解密
byte[] plain =decrypt(PrivateKey,decode(cipher)); //Base64a解码
System.out.println("私钥解密,明文:"+new String(plain)); //RSA解密
}
/**
* 编码
* @param txt byte字节数组
* @return encode Base64编码
*/
public static byte[] encode(byte[] txt) {
return org.apache.commons.codec.binary.Base64.encodeBase64(txt);
}
/**
* 解码
* @param txt 编码后的byte
* @return decode Base64解码
*/
public static byte[] decode(String txt){
return org.apache.commons.codec.binary.Base64.decodeBase64(txt);
}
/**
* 公钥加密
* @param publicKey 公钥
* @param obj 明文
* @return byte[] 密文
*/
public static byte[] encrypt(RSAPublicKey publicKey, byte[] obj) throws Exception {
Cipher cipher =Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE,publicKey);
//返回加密后的内容
return cipher.doFinal(obj);
}
/**
* 私钥解密
* @param privateKey 公钥
* @param obj 密文
* @return byte[] 密文
*/
public static byte[] decrypt(RSAPrivateKey privateKey, byte[] obj)throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
//返回解密后的数组
return cipher.doFinal(obj);
}
}
执行结果:
参考:https://blog.youkuaiyun.com/piaoranyuji/article/details/126140261