rsautil .java文件代码
时间: 2023-11-15 20:05:46 浏览: 151
以下是 RSAUtil.java 文件的代码,你可以在自己的项目中使用该代码文件,实现 RSA 加解密功能。
```java
package com.github.yeecode.rsautil;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Security;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
/**
* RSA 加解密工具类
*/
public class RSAUtil {
/**
* 生成公钥和私钥文件
*
* @param publicKeyFilePath 公钥文件路径
* @param privateKeyFilePath 私钥文件路径
*/
public static void generateKeyPair(String publicKeyFilePath, String privateKeyFilePath) {
try {
Security.addProvider(new BouncyCastleProvider());
// 创建密钥对生成器
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC");
// 初始化密钥对生成器
keyPairGenerator.initialize(1024, new SecureRandom());
// 生成密钥对
KeyPair keyPair = keyPairGenerator.generateKeyPair();
// 获取公钥和私钥
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
// 将公钥和私钥写入文件
FileOutputStream publicKeyOutputStream = new FileOutputStream(publicKeyFilePath);
publicKeyOutputStream.write(publicKey.getEncoded());
publicKeyOutputStream.close();
FileOutputStream privateKeyOutputStream = new FileOutputStream(privateKeyFilePath);
privateKeyOutputStream.write(privateKey.getEncoded());
privateKeyOutputStream.close();
} catch (NoSuchAlgorithmException | NoSuchProviderException | IOException e) {
e.printStackTrace();
}
}
/**
* 加密
*
* @param plainText 明文
* @param publicKeyFilePath 公钥文件路径
* @return 密文
*/
public static String encrypt(String plainText, String publicKeyFilePath) {
try {
Security.addProvider(new BouncyCastleProvider());
// 读取公钥文件
FileInputStream publicKeyInputStream = new FileInputStream(publicKeyFilePath);
ByteArrayOutputStream publicKeyByteArrayOutputStream = new ByteArrayOutputStream();
byte[] publicKeyBytes = new byte[1024];
int publicKeyLength = 0;
while ((publicKeyLength = publicKeyInputStream.read(publicKeyBytes)) != -1) {
publicKeyByteArrayOutputStream.write(publicKeyBytes, 0, publicKeyLength);
}
publicKeyInputStream.close();
publicKeyByteArrayOutputStream.close();
byte[] publicKeyByteArray = publicKeyByteArrayOutputStream.toByteArray();
// 将公钥字节数组转换为 PublicKey 对象
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKeyByteArray);
KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
// 加密
Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] cipherTextBytes = cipher.doFinal(plainText.getBytes());
return Base64Util.encode(cipherTextBytes);
} catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidKeySpecException | IOException
| NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
e.printStackTrace();
return null;
}
}
/**
* 解密
*
* @param cipherText 密文
* @param privateKeyFilePath 私钥文件路径
* @return 明文
*/
public static String decrypt(String cipherText, String privateKeyFilePath) {
try {
Security.addProvider(new BouncyCastleProvider());
// 读取私钥文件
FileInputStream privateKeyInputStream = new FileInputStream(privateKeyFilePath);
ByteArrayOutputStream privateKeyByteArrayOutputStream = new ByteArrayOutputStream();
byte[] privateKeyBytes = new byte[1024];
int privateKeyLength = 0;
while ((privateKeyLength = privateKeyInputStream.read(privateKeyBytes)) != -1) {
privateKeyByteArrayOutputStream.write(privateKeyBytes, 0, privateKeyLength);
}
privateKeyInputStream.close();
privateKeyByteArrayOutputStream.close();
byte[] privateKeyByteArray = privateKeyByteArrayOutputStream.toByteArray();
// 将私钥字节数组转换为 PrivateKey 对象
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKeyByteArray);
KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
// 解密
Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] plainTextBytes = cipher.doFinal(Base64Util.decode(cipherText));
return new String(plainTextBytes);
} catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidKeySpecException | IOException
| NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
e.printStackTrace();
return null;
}
}
}
```
该代码文件中的 `Base64Util` 类是一个 Base64 编码工具类,用于将二进制数据转换为可读的字符串。您需要将其导入到您的项目中,或者使用 Java 内置的 `Base64` 类代替。
阅读全文