import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
public class RsaUtils {
private static final int DEFAULT_KEY_SIZE = 2048;
private static final String ALGORITHM_RSA = "RSA";
/**
* 从文件中读取公钥
*
* @param fileName 公钥保存路径,相对于classpath
* @return 公钥对象
* @throws Exception
*/
public static PublicKey getPublicKey(String fileName) throws Exception {
byte[] bytes = readFile(fileName);
return getPublicKey(bytes);
}
/**
* 从文件中读取密钥
*
* @param fileName 私钥保存路径,相对于classpath
* @return 私钥对象
* @throws Exception
*/
public static PrivateKey getPrivateKey(String fileName) throws Exception {
byte[] bytes = readFile(fileName);
return getPrivateKey(bytes);
}
/**
* 获取公钥
*
* @param bytes 公钥的字节数组
* @return
* @throws Exception
*/
private static PublicKey getPublicKey(byte[] bytes) throws Exception {
bytes = Base64.getDecoder().decode(bytes);
X509EncodedKeySpec spec = new X509EncodedKeySpec(bytes);
KeyFactory factory = KeyFactory.getInstance(ALGORITHM_RSA);
return factory.generatePublic(spec);
}
/**
* 获取密钥
*
* @param bytes 私钥的字节数组
* @return
* @throws Exception
*/
private static PrivateKey getPrivateKey(byte[] bytes) throws NoSuchAlgorithmException, InvalidKeySpecException {
bytes = Base64.getDecoder().decode(bytes);
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(bytes);
KeyFactory factory = KeyFactory.getInstance(ALGORITHM_RSA);
return factory.generatePrivate(spec);
}
/**
* 根据密文,生成rsa公钥和私钥,并写入文件
*
* @param publicKeyFileName 公钥文件路径
* @param privateKeyFileName 私钥文件路径
* @param secret 生成密钥的密文
*/
public static void generateKey(String publicKeyFileName, String privateKeyFileName, String secret, int keySize) throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM_RSA);
SecureRandom secureRandom = new SecureRandom(secret.getBytes());
keyPairGenerator.initialize(Math.max(keySize, DEFAULT_KEY_SIZE), secureRandom);
KeyPair keyPair = keyPairGenerator.genKeyPair();
// 获取公钥并写文件
byte[] publicKeyBytes = keyPair.getPublic().getEncoded();
publicKeyBytes = Base64.getEncoder().encode(publicKeyBytes);
writeFile(publicKeyFileName, publicKeyBytes);
// 获取私钥并写文件
byte[] privateKeyBytes = keyPair.getPrivate().getEncoded();
privateKeyBytes = Base64.getEncoder().encode(privateKeyBytes);
writeFile(privateKeyFileName, privateKeyBytes);
}
private static byte[] readFile(String fileName) throws Exception {
return Files.readAllBytes(new File(fileName).toPath());
}
private static void writeFile(String destPath, byte[] bytes) throws IOException {
File dest = new File(destPath);
if (!dest.exists()) {
dest.createNewFile();
}
Files.write(dest.toPath(), bytes);
}
public static void main(String[] args) throws Exception {
String privateKeyFile = "D:\\var\\temp\\id_key_rsa";
String publicKeyFile = "D:\\var\\temp\\id_key_rsa.pub";
RsaUtils.generateKey(publicKeyFile, privateKeyFile, "test123", 2048);
}
}
RSA公私钥生成工具类
最新推荐文章于 2024-06-24 16:00:48 发布
这个Java类库实现了RSA密钥对的生成、读取和写入文件功能。通过指定密钥大小和密文,可以创建公钥和私钥,并将它们以Base64编码的形式保存到指定文件。此外,还提供了从文件加载公钥和私钥的方法,方便后续加密和解密操作。
摘要由CSDN通过智能技术生成