RSA 密钥转换为 PEM 文件

发布于 2024-10-08 08:31:28 字数 358 浏览 0 评论 0原文

如何转换此 RSA 公钥: 109120132967399429278860960508995541528237502902798129123468757937266291492576446330739696001110 6039072308886100726558188253585034290 57592827629436413108566029093628 2126359538366865626758497206207862794310902180176810615217550567108238764764442605581471797071 19674283982419152118103759076030616683978566631413

to *.pem file?

How can I convert this RSA public key:
109120132967399429278860960508995541528237502902798129123468757937266291492576446330739696001110 6039072308886100726558188253585034290 57592827629436413108566029093628 2126359538366865626758497206207862794310902180176810615217550567108238764764442605581471797071 19674283982419152118103759076030616683978566631413

to *.pem file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

阳光的暖冬 2024-10-15 08:31:28

“pem”文件实际上并不是一种格式,不同类型的对象可以存储在称为“pem”文件的文件中。大多数情况下,这些文件包含 Base64 编码的 X509 证书或 Base64 编码的私钥对象。

假设您想要一个 X509 证书,接下来您应该意识到证书由许多字段组成,其中只有一个字段是公钥。因此需要决定其他字段的值。最后,必须使用私钥签署证书。

附言。 RSA 公钥由模数和公共指数组成。你的公众指数是多少?

A "pem" file is not really a format, and different kinds of objects may be be stored in things called "pem" files. Most often these files contain either a base64-encoded X509 certificate or else a base64-encoded private key object.

Assuming you want an X509 certificate, you should next realize that a certificate consists of many fields, only one of which is the public key. So would need to decide on the values of the other fields. Finally, a certificate must be signed, with a private key.

PS. An RSA public key consists of a modulus and a public exponent. What is your public exponent?

甜扑 2024-10-15 08:31:28

如果您的十六进制字符串只是从 base64 到十六进制的简单转换。然后你就可以逆转它。这是我为解决我的情况而编写的脚本(在 PHP 中):

<?php

$list = array_slice($argv, 1);

foreach ($list as $file) {
    $hex = str_replace("\n", "", file_get_contents($file));
    $pem = str_replace(".hex", ".pem", $file);
    $b64 = base64_encode(hex2bin($hex));
    $fd = fopen($pem, 'w');
    fprintf($fd, "-----BEGIN PUBLIC KEY-----\n%s\n-----END PUBLIC KEY----\n", implode("\n", str_split($b64, 64)));
    fclose($fd);
}

给定 .hex 文件列表,它会转换回“.pem”。你可以像这样运行它:

php script.php *.hex

If your hex string is just a simple convert from base64 to hex. Then you can reverse it. Here is the script I did (in PHP) to solve my case:

<?php

$list = array_slice($argv, 1);

foreach ($list as $file) {
    $hex = str_replace("\n", "", file_get_contents($file));
    $pem = str_replace(".hex", ".pem", $file);
    $b64 = base64_encode(hex2bin($hex));
    $fd = fopen($pem, 'w');
    fprintf($fd, "-----BEGIN PUBLIC KEY-----\n%s\n-----END PUBLIC KEY----\n", implode("\n", str_split($b64, 64)));
    fclose($fd);
}

Given a list of .hex files, it convert back to ".pem". You can run it like this:

php script.php *.hex
蓝海 2024-10-15 08:31:28

我会使用 OpenSSL。假设您拥有 DER 格式的密钥,则可以使用此命令从 DER 转换为 PEM。

openssl x509 -inform der -in input.der -out output.pem

如果您不确定是否拥有格式正确的 DER (ASN.1) 编码密钥,请尝试此 工具来解析您的输入文件。

I would use OpenSSL. Assuming that you have the key in DER format, you can use this command to convert from DER to PEM.

openssl x509 -inform der -in input.der -out output.pem

If you are not sure whether you have the correctly formatted DER (ASN.1) encoded key, try this tool to parse your input file.

隔纱相望 2024-10-15 08:31:28

首先,詹姆斯·K·波尔克总统是对的。完整的 RSA 密钥由模数和公共指数组成。
在 Java 中,java.security.interfaces 包中的 RSAPublicKey 接口可以保存格式正确的 RSA 密钥。假设您有这样的对象,转换为 PEM 格式的过程将涉及对其进行编码,如下所示:

String pemFormatKey = Base64.getEncoder().encodeToString(rsaPublicKey.getEncoded());

First of all President James K. Polk is right. A complete RSA Key is formed by a modulus and a public exponent.
In Java, the interface RSAPublicKey from the package java.security.interfaces can hold well-formed RSA Keys. Assuming you have such object, the process of converting to a PEM format would involve encoding it, as follows:

String pemFormatKey = Base64.getEncoder().encodeToString(rsaPublicKey.getEncoded());
ま柒月 2024-10-15 08:31:28

使用 putty 程序,PuTTYgen 会进行此转换

use putty programs, PuTTYgen does this conversion

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文