如何生成PEM格式的公钥和私钥

问题描述 投票:0回答:4

我需要使用 java 生成 PEM 格式的 RSA 和 DSA 密钥对(公钥和私钥)。 我希望以这种格式打开公钥和私钥文件:

-----开始公钥----- MIIBIjANBgkqhkiG9w0BAQEFAOCAQ8AMIIBCgKCAQEAryQICCl6NZ5gDKrnSztO 3Hy8PEUcuyvg/ikC+VcIo2SFFSf18a3IMYldIugqqqZCs4/4uVW3sbdLs/6PfgdX 7O9D22ZiFWHPYA2k2N744MNiCD1UE+tJyllUhSblK480 亿+v1oZHCM0nYQ2NqUkvS j+hwUU3RiWl7x3D2s9wSdNt7XUtW05a/FXehsPSiJfKvHJJnGOX0BgTvkLnkAOTd OrUZ/wK69Dzu4IvrN4vs9Nes8vbwPa/ddZEzGR0cQMt0JBkhk9kU/qwqUseP1QRJ 5I1jR4g8aYPL/ke9K35PxZWuDp3U0UPAZ3PjFAh+5T+fc7gzCs9dPzSHloruU+gl 食品质量监督检验局 -----结束公钥-----

我的公钥之前已经以这种格式生成,但我不想要它:

0Ÿ0 *†H†÷ 0Ÿ0 *†H†÷

好的,这是我的密钥生成代码:

private static void createKey()
        throws Exception {

            Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Password to encrypt the private key: ");
            String password = in.readLine();
            System.out.println("Generating an RSA keypair...");

            // Create an RSA key
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            keyPairGenerator.initialize(1024);
            KeyPair keyPair = keyPairGenerator.genKeyPair();

            System.out.println("Done generating the keypair.\n");

            // Now we need to write the public key out to a file
            System.out.print("Public key filename: ");
            String publicKeyFilename = "C:/Users/Joe/Desktop/" + in.readLine();

            // Get the encoded form of the public key so we can
            // use it again in the future. This is X.509 by default.
            byte[] publicKeyBytes = keyPair.getPublic().getEncoded();

            // Write the encoded public key out to the filesystem
            FileOutputStream fos = new FileOutputStream(publicKeyFilename);
            fos.write(publicKeyBytes);
            fos.close();

            // Now we need to do the same thing with the private key,
            // but we need to password encrypt it as well.
            System.out.print("Private key filename: ");
            String privateKeyFilename = "C:/Users/Joe/Desktop/" + in.readLine();

            // Get the encoded form. This is PKCS#8 by default.
            byte[] privateKeyBytes = keyPair.getPrivate().getEncoded();

            // Here we actually encrypt the private key
            byte[] encryptedPrivateKeyBytes =
            passwordEncrypt(password.toCharArray(),privateKeyBytes);

            fos = new FileOutputStream(privateKeyFilename);
            fos.write(encryptedPrivateKeyBytes);
            fos.close();
        }

谢谢你的帮助..

java rsa public-key pem dsa
4个回答
8
投票

您可以使用充气城堡来代替手动生成 PEM 字符串:因为它是一个经过测试的库,您可以确定输出。以下代码是 Kotlin 语言,但可以轻松地与 Java 语法一起使用:

        val gen = KeyPairGenerator.getInstance("RSA")
        gen.initialize(2048)
        val pair = gen.generateKeyPair()
        val privateKey: PrivateKey = pair.private
       
  
        val pemObject = PemObject("RSA PRIVATE KEY", privateKey.encoded)
        
        val byteStream = ByteArrayOutputStream()
        val pemWriter = PemWriter(OutputStreamWriter(byteStream))
        pemWriter.writeObject(pemObject)
        pemWriter.close();
        println(String(byteStream.toByteArray()))


4
投票

也许有点晚了,但我有解决方案。希望对其他人有帮助。

byte[] publicKeyBytes = keyPair.getPublic().getEncoded();

在这里,您将获取密钥字节并直接写入文件。这样您就得到了适当的结果 - DER 编码的文件。然而 PEM 是 Base64 编码格式,每 64 个符号和页眉/页脚带有换行符。

有实现此逻辑的代码:

String publicKeyContent = Base64.getEncoder().encodeToString(publicKeyBytes);
String publicKeyFormatted = "-----BEGIN PUBLIC KEY-----" + System.lineSeparator();
for (final String row: 
        Splitter
            .fixedLength(64)
            .split(publicKeyContent)
    ) 
{
    publicKeyFormatted += row + System.lineSeparator();
}
publicKeyFormatted += "-----END PUBLIC KEY-----";

因此 publicKeyFormatted 将包含 PEM 编码的公钥字符串。

附注Splitter 是 Guava lib 中提供的一个类,但您可以通过简单的循环或其他方式拆分字符串。


0
投票

我编写了一个库,其中包含可以执行此操作的方法。它称为 Reasonously Easy Cryptography,您可以使用

PEMHandler
方法来实现此目的。假设您已从我的库中导入了
PEMHandler
,并且
key
的类是
java.security.Key
的实现,例如
PrivateKey
:

,您可以这样做:
String pem = PEMHandler.keyToPem(key);

这适用于任何类型的

Key
,它会弄清楚它是公钥还是私钥以及它自己使用的算法(它并不完美,我仍在努力寻找最好的方法)那个,但它确实可以与
PrivateKey
PublicKey
配合使用)。还有一种方法可以在一次调用中对 KeyPair 中的两个密钥执行此操作,以及将 PEM 字符串转换回密钥的方法。


© www.soinside.com 2019 - 2024. All rights reserved.