如何从文件中读取 public 键
How to read a public key from a file
我有这个问题,我不知道如何阅读我的 public 密钥文件,我已经寻找了一段时间的解决方案,但他们往往不回答我的问题。我有一个生成 public 和私钥的程序,我有另一种方法将它们分别写在自己的文件中。这听起来可能很傻,但我真的不知道如何从文件中读取密钥。这是必需的,因为我想进行 asymetrec 加密,因此将传输密钥文件。我将在下面 link 我的代码,非常感谢任何帮助:)
如果您需要我解释的更多细节,请随时询问。
谢谢!
密钥生成和存储方法:
public class Encryption {
public static PrivateKey privateKey;
public static PublicKey publicKey;
public void KeyGeneration() throws NoSuchAlgorithmException{
KeyPairGenerator Generator = KeyPairGenerator.getInstance("RSA"); // Creat the Generator object with RSA
Generator.initialize(1024); // Set the generator to make the 2048 bit key
KeyPair Pair = Generator.genKeyPair(); // Generate the key pair
publicKey = Pair.getPublic(); // Set the Public Key
privateKey = Pair.getPrivate(); // Set the private Key
System.out.println(Base64.getEncoder().encodeToString(publicKey.getEncoded()));
System.out.println(Base64.getEncoder().encodeToString(privateKey.getEncoded()));
}
public void writeToFile(String filePath, byte[] key) throws IOException{
File fileToWriteto = new File(filePath);
fileToWriteto.getParentFile().mkdirs();
FileOutputStream FoutputStream = new FileOutputStream(fileToWriteto);
FoutputStream.write(key);
FoutputStream.flush();
FoutputStream.close();
}
}
保存和加载 RSA 密钥文件的最简单方法是使用编码版本。下面的代码以编码形式将私钥和 public 密钥保存到文件中。
简单输出:
Save and load RSA Keys encoded
private key equals: true
public key equals: true
警告:请注意以下代码没有异常处理,仅供学习使用。
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;
public class SaveLoadRsaKeysSo {
public static void main(String[] args) throws GeneralSecurityException, IOException {
System.out.println("Save and load RSA Keys encoded");
KeyPairGenerator rsaGenerator = KeyPairGenerator.getInstance("RSA");
SecureRandom random = new SecureRandom();
rsaGenerator.initialize(2048, random);
KeyPair rsaKeyPair = rsaGenerator.generateKeyPair();
PublicKey rsaPublicKey = rsaKeyPair.getPublic();
PrivateKey rsaPrivateKey = rsaKeyPair.getPrivate();
// save private & public key
Files.write(Paths.get("rsaPrivateKey.encoded"), rsaPrivateKey.getEncoded());
Files.write(Paths.get("rsaPublicKey.encoded"), rsaPublicKey.getEncoded());
// load private & public key
byte[] privateKeyBytesLoad = Files.readAllBytes(Paths.get("rsaPrivateKey.encoded"));
PrivateKey privateKeyLoad = getPrivateKeyFromEncoded(privateKeyBytesLoad);
byte[] publicKeyBytesLoad = Files.readAllBytes(Paths.get("rsaPublicKey.encoded"));
PublicKey publicKeyLoad = getPublicKeyFromEncoded(publicKeyBytesLoad);
System.out.println("private key equals: " + Arrays.equals(rsaPrivateKey.getEncoded(), privateKeyLoad.getEncoded()));
System.out.println("public key equals: " + Arrays.equals(rsaPublicKey.getEncoded(), publicKeyLoad.getEncoded()));
}
public static PrivateKey getPrivateKeyFromEncoded(byte[] key) throws GeneralSecurityException {
KeyFactory kf = KeyFactory.getInstance("RSA");
return (PrivateKey) kf.generatePrivate(new PKCS8EncodedKeySpec(key));
}
public static PublicKey getPublicKeyFromEncoded(byte[] key) throws GeneralSecurityException {
KeyFactory kf = KeyFactory.getInstance("RSA");
return (PublicKey) kf.generatePublic(new X509EncodedKeySpec(key));
}
}
我有这个问题,我不知道如何阅读我的 public 密钥文件,我已经寻找了一段时间的解决方案,但他们往往不回答我的问题。我有一个生成 public 和私钥的程序,我有另一种方法将它们分别写在自己的文件中。这听起来可能很傻,但我真的不知道如何从文件中读取密钥。这是必需的,因为我想进行 asymetrec 加密,因此将传输密钥文件。我将在下面 link 我的代码,非常感谢任何帮助:)
如果您需要我解释的更多细节,请随时询问。
谢谢!
密钥生成和存储方法:
public class Encryption {
public static PrivateKey privateKey;
public static PublicKey publicKey;
public void KeyGeneration() throws NoSuchAlgorithmException{
KeyPairGenerator Generator = KeyPairGenerator.getInstance("RSA"); // Creat the Generator object with RSA
Generator.initialize(1024); // Set the generator to make the 2048 bit key
KeyPair Pair = Generator.genKeyPair(); // Generate the key pair
publicKey = Pair.getPublic(); // Set the Public Key
privateKey = Pair.getPrivate(); // Set the private Key
System.out.println(Base64.getEncoder().encodeToString(publicKey.getEncoded()));
System.out.println(Base64.getEncoder().encodeToString(privateKey.getEncoded()));
}
public void writeToFile(String filePath, byte[] key) throws IOException{
File fileToWriteto = new File(filePath);
fileToWriteto.getParentFile().mkdirs();
FileOutputStream FoutputStream = new FileOutputStream(fileToWriteto);
FoutputStream.write(key);
FoutputStream.flush();
FoutputStream.close();
}
}
保存和加载 RSA 密钥文件的最简单方法是使用编码版本。下面的代码以编码形式将私钥和 public 密钥保存到文件中。
简单输出:
Save and load RSA Keys encoded
private key equals: true
public key equals: true
警告:请注意以下代码没有异常处理,仅供学习使用。
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;
public class SaveLoadRsaKeysSo {
public static void main(String[] args) throws GeneralSecurityException, IOException {
System.out.println("Save and load RSA Keys encoded");
KeyPairGenerator rsaGenerator = KeyPairGenerator.getInstance("RSA");
SecureRandom random = new SecureRandom();
rsaGenerator.initialize(2048, random);
KeyPair rsaKeyPair = rsaGenerator.generateKeyPair();
PublicKey rsaPublicKey = rsaKeyPair.getPublic();
PrivateKey rsaPrivateKey = rsaKeyPair.getPrivate();
// save private & public key
Files.write(Paths.get("rsaPrivateKey.encoded"), rsaPrivateKey.getEncoded());
Files.write(Paths.get("rsaPublicKey.encoded"), rsaPublicKey.getEncoded());
// load private & public key
byte[] privateKeyBytesLoad = Files.readAllBytes(Paths.get("rsaPrivateKey.encoded"));
PrivateKey privateKeyLoad = getPrivateKeyFromEncoded(privateKeyBytesLoad);
byte[] publicKeyBytesLoad = Files.readAllBytes(Paths.get("rsaPublicKey.encoded"));
PublicKey publicKeyLoad = getPublicKeyFromEncoded(publicKeyBytesLoad);
System.out.println("private key equals: " + Arrays.equals(rsaPrivateKey.getEncoded(), privateKeyLoad.getEncoded()));
System.out.println("public key equals: " + Arrays.equals(rsaPublicKey.getEncoded(), publicKeyLoad.getEncoded()));
}
public static PrivateKey getPrivateKeyFromEncoded(byte[] key) throws GeneralSecurityException {
KeyFactory kf = KeyFactory.getInstance("RSA");
return (PrivateKey) kf.generatePrivate(new PKCS8EncodedKeySpec(key));
}
public static PublicKey getPublicKeyFromEncoded(byte[] key) throws GeneralSecurityException {
KeyFactory kf = KeyFactory.getInstance("RSA");
return (PublicKey) kf.generatePublic(new X509EncodedKeySpec(key));
}
}