简体   繁体   English

在OpenSSL上使用Crypto ++生成的RSA密钥

[英]Using Crypto++ generated RSA keys on OpenSSL

Is there a way to use the RSA keys I've generated with the Crypto++ API in OpenSSL? 有没有办法使用我在OpenSSL中使用Crypto ++ API生成的RSA密钥? What I am looking for is a way to store the keys in a format that both Crypto++ and OpenSSL can easily open them. 我正在寻找一种方法来存储密钥的格式,Crypto ++和OpenSSL都可以轻松打开它们。

I'm writing a licensing scheme and would want to verify signatures and decrypt files using the Crypto++ API, but to generate the license files I would want to use a web interface (probably using PHP, which only supports OpenSSL) to generate and encrypt/sign the licenses. 我正在编写许可方案,并希望使用Crypto ++ API验证签名和解密文件,但是要生成许可证文件,我想使用Web界面(可能使用PHP,它只支持OpenSSL)来生成和加密/签署许可证。

I would write both applications using Crypto++ and call it from the PHP, but since the private key will be stored in a encrypted form, a password must be passed to the application and passing it on the command line doesn't seems to be a good idea to me. 我会使用Crypto ++编写这两个应用程序并从PHP调用它,但由于私钥将以加密形式存储,因此必须将密码传递给应用程序并在命令行上传递它似乎不太好我的想法。

Both Crypto++ and OpenSSL can handle PKCS#8 encoded keys. Crypto ++和OpenSSL都可以处理PKCS#8编码密钥。 In crypto++, you can generate keys and convert to PKCS#8 buffer like this, 在crypto ++中,您可以生成密钥并转换为PKCS#8缓冲区,如下所示,

AutoSeededRandomPool rng;
RSAES_OAEP_SHA_Decryptor priv(rng, 2048);
string der;
StringSink der_sink(der);
priv.DEREncode(der_sink);
der_sink.MessageEnd();

// der.data() is the bytes you need

Now you just need to pass the bytes to PHP. 现在你只需要将字节传递给PHP。 You can save it in a file, send in a message. 您可以将其保存在文件中,然后发送消息。

The only gotcha is that PHP's OpenSSL interface only accepts PEM encoded PKCS#8. 唯一的问题是PHP的OpenSSL接口只接受PEM编码的PKCS#8。 You can easily convert DER-encoded buffer into PEM like this in PHP, 您可以在PHP中轻松地将DER编码的缓冲区转换为PEM,

<?php
function pkcs8_to_pem($der) {

    static $BEGIN_MARKER = "-----BEGIN PRIVATE KEY-----";
    static $END_MARKER = "-----END PRIVATE KEY-----";

    $value = base64_encode($der);

    $pem = $BEGIN_MARKER . "\n";
    $pem .= chunk_split($value, 64, "\n");
    $pem .= $END_MARKER . "\n";

    return $pem;
}
?>

You can also convert PKCS#8 to PEM in C++ if you prefer. 如果您愿意,也可以使用C ++将PKCS#8转换为PEM。 The algorithm is very simple as you can see from the PHP code. 从PHP代码可以看出,该算法非常简单。

OpenSSL is so prevalent nowadays. OpenSSL如今非常流行。 I don't see any reason to use Crypto++ for common crypto applications like this. 我没有看到任何理由将Crypto ++用于这样的常见加密应用程序。

Is there a way to use the RSA keys I've generated with the Crypto++ API in OpenSSL? 有没有办法使用我在OpenSSL中使用Crypto ++ API生成的RSA密钥? What I am looking for is a way to store the keys in a format that both Crypto++ and OpenSSL can easily open them. 我正在寻找一种方法来存储密钥的格式,Crypto ++和OpenSSL都可以轻松打开它们。

Yes. 是。 In addition to X.509 and PKCS #8 encoded keys (ZZ Coder's answer), you can also use PEM encoded keys including encrypted keys. 除了X.509和PKCS#8编码密钥(ZZ Coder的答案)之外,您还可以使用包含加密密钥的PEM编码密钥。 Support for PEM encoded keys was donated to the project in July, 2014 for OpenSSL interop. 2014年7月,为OpenSSL互操作向该项目捐赠了对PEM编码密钥的支持。

To use the PEM encoded keys, you need to fetch the Crypto++ PEM Pack and recompile the library. 要使用PEM编码密钥,您需要获取Crypto ++ PEM包并重新编译库。 The PEM Pack is not part of the Crypto++ library as provided by Wei Dai at the Crypto++ website . PEM Pack不是由Wei Dai在Crypto ++网站上提供的Crypto ++库的一部分。

Once you install and recompile, its as simple as: 安装并重新编译后,其简单如下:

// Load a RSA public key
FileSource fs1("rsa-pub.pem", true);
RSA::PublicKey k1;
PEM_Load(fs1, k1);

// Load a encrypted RSA private key
FileSource fs2("rsa-enc-priv.pem", true);
RSA::PrivateKey k2;
PEM_Load(fs2, k2, "test", 4);

// Save an EC public key
DL_PublicKey_EC<ECP> k16 = ...;
FileSink fs16("ec-pub-xxx.pem", true);
PEM_Save(fs16, k16);

// Save an encrypted EC private key
DL_PrivateKey_EC<ECP> k18 = ...;
FileSink fs18("ec-enc-priv-xxx.pem", true);
PEM_Save(fs18, k18, "AES-128-CBC", "test", 4);

The keys look like so on-disk: 磁盘上的键看起来像这样:

$ cat rsa-pub.pem
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCg7ovcljEjZCFOdLWENBKE6FSk
Nke6OP79SMJABJw+JoEBpNddK6/v99IvA1qU76V0V4k8qLvhkVUtk9FArhhRsxeF
1fd8UVqgsT8j0YCVFcJ/ZA372ogpXyvc5aK9mZEiKE5TIF8qnDFFZiMWPrad1buk
hg+eFdo78QRLA5plEQIDAQAB
-----END PUBLIC KEY-----
$  
$ cat rsa-enc-priv.pem
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC,E1A759E11CA515CE34B6E8CE5278C919

slMx02TMblahTedEKsfS+qYYo4nZFaqI3PhCRYmE5zUa9clHm7yo36wIk3oo52OB
f4AhOaJwiPQAbLe/kDHeP77iHd/4+hFNq/Haj6ahWRpXilLVOETLtefbzSGO8va3
ORnwQpPThs2V0EetPU3LB3QcA/XRjWDzyNa7+LydOjKwbQdZnF/jND5NCkEkncNM
iQJ1VWubN+Xs3Rx0CfLu5Chl1n7WnmCNMtLL/LtYeaR1SlRJa6BaF7hNHJJJ3+Jc
8curCKlpobs+XnlDfjyqgTXolkiepn95TnT7KSqi3BqVEpq/5LKMnkDJg6nwUR7A
w0jLNr1f8adWyBEj2Dp0D/jy8eDh65eHdJw4s8G5FZfBud1zWbvRQ3Ah70ISUKa3
4q/6z2vervPgoc+rMVYDvRf/mqa4LMXYhuygsyx50OgPldCC2d0cVVFCg/ljdEzO
UV5rSkK1Qczv8Nc1ZdY3fJA+qYIV8JqPPY+dJ2312R+myPi5Av0/69k8lZN5eIJk
SkiiFQmabhc+o6z4RFA52a3lOud3eGM9L5nbFQGc5COzQVZ6y8t06tLIp9Y5zjA4
KTgNncV5eq3Bau+cWXjP6pJRixFVfwIoy95mAur7B2P1iE4FXyZbvCovPL6vilT5
kSqAo7Znu0RpTjE36tWY6tFt+GU7k8EBrjA3Qi+8xxqyYtr57Ns+H/j+hhJTN8L7
IXoevwS81OPiB0Dmg6wLLXATG1+gCNXb8sd5U2eJhy4LOJA3y54CTgRnPXtM38CH
K+JvnDstyUl9IGTsgUz51ZzyJNZGU9Ro3pt/a3Cs5IJumaygZ0LQ44WBw9m/vja9
-----END RSA PRIVATE KEY-----
$  
$ cat ec-pub.pem
-----BEGIN PUBLIC KEY-----
MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEVwXjdIb2yy25QbIO0XiIHpySXwSpIAcz
v0Wdyq+fZ6BdJjs2jKvbs9pcRJn8yxlASWoz2R4NoHTZ2YokKsDfEg==
-----END PUBLIC KEY-----
$  
$ cat ec-enc-priv.pem
-----BEGIN EC PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC,F1DBC73E26DCD310888932C2762B3512

nikex48SFvtNOIrOEDipwmxaghjn4jtrvwI3d1H/VNq9yp26WqFZxBJCUPFBFLjH
auA+AHeUo3BVkNQPs0VO4FD5xR50mtc2tCJizzhyTTTypLc3lRkxmD1MpeZnWRy2
70foVtNSvLL/QLJqNJGm/G9kl0xPN4zAfOq7Txoscnk=
-----END EC PRIVATE KEY-----

Related: for other useful Crypto++ patches, see the Category:Patch page on the Crypto++ wiki. 相关:对于其他有用的Crypto ++补丁,请参阅Crypto ++ wiki上的Category:Patch页面。

Try this link: http://www.cryptopp.com/fom-serve/cache/62.html 试试这个链接: http//www.cryptopp.com/fom-serve/cache/62.html

It looks like you'll need to use PKCS#8 and convert from DER to PEM format to be able to use the keys in OpenSSL. 看起来你需要使用PKCS#8并从DER转换为PEM格式才能使用OpenSSL中的密钥。 I'm not sure if you'll be able to use a single file for both. 我不确定你是否能够使用单个文件。

I've only used OpenSSL so I'm not sure what options you have with Crypto++. 我只使用OpenSSL,所以我不确定你使用Crypto ++有什么选择。 I found the link above by searching Google for these terms: Crypto++ RSA OpenSSL. 我在Google上搜索了以下链接:Crypto ++ RSA OpenSSL。

DER is OpenSSL's binary format for keys and certificates. DER是OpenSSL的密钥和证书的二进制格式。

PEM is OpenSSL's text format. PEM是OpenSSL的文本格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM