java—如何从文件中读取公钥

xqnpmsa8  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(564)

我有这个问题,我不知道如何读取我的公钥文件,我已经寻找了一段时间的解决方案,但他们往往不会回答我的问题,在我的情况下。我有一个生成公钥和私钥的程序,我有另一个方法,分别把它们写在自己的文件中。这听起来可能很傻,但我真的不知道如何从文件中读取密钥。这是必要的,因为我想做asymetec加密,所以将传输密钥文件。我将在下面链接我的代码,任何帮助都非常感谢:)
如果你还需要我解释的细节,请尽管问。
谢谢!
密钥生成和存储方法:

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();

    }
  }
7qhs6swi

7qhs6swi1#

保存和加载rsa密钥文件的最简单方法是使用编码版本。下面的代码以编码形式将私钥和公钥保存到文件中。
简单输出:

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));
    }
}

相关问题