如何从模数、指数和私有指数创建 Crypt::RSA 对象?

发布于 2024-09-11 22:06:59 字数 837 浏览 2 评论 0原文

我正在尝试将以下 php 功能移植到 perl:

public function loadKey($mod, $exp, $type = 'public')
{
    $rsa = new Crypt_RSA();
    $rsa->signatureMode = CRYPT_RSA_SIGNATURE_PKCS1;
    $rsa->setHash('sha256');
    $rsa->modulus = new Math_BigInteger(Magicsig::base64_url_decode($mod), 256);
    $rsa->k = strlen($rsa->modulus->toBytes());
    $rsa->exponent = new Math_BigInteger(Magicsig::base64_url_decode($exp), 256);

    // snip...
}

我需要将 ("RSA.$mod.$exp.$private_exp"):

RSA.mVgY8RN6URBTstndvmUUPb4UZTdwvwmddSKE5z_jvKUEK6yk1u3rrC9yN8k6FilGj9K0eeUPe2hf4Pj-5CmHww==.AQAB.Lgy_yL3hsLBngkFdDw1Jy9TmSRMiH6yihYetQ8jy-jZXdsZXd8V5ub3kuBHHk4M39i3TduIkcrjcsiWQb77D8Q==

... 形式的字符串转换为 Crypt::RSA 对象。我已经拆分了组件,因此我有 $mod、$exp 和 $private_exp,但 perl Crypt::RSA API 似乎没有办法显式设置。

I'm trying to port the following php functionality over to perl:

public function loadKey($mod, $exp, $type = 'public')
{
    $rsa = new Crypt_RSA();
    $rsa->signatureMode = CRYPT_RSA_SIGNATURE_PKCS1;
    $rsa->setHash('sha256');
    $rsa->modulus = new Math_BigInteger(Magicsig::base64_url_decode($mod), 256);
    $rsa->k = strlen($rsa->modulus->toBytes());
    $rsa->exponent = new Math_BigInteger(Magicsig::base64_url_decode($exp), 256);

    // snip...
}

I need to convert a string in the form ("RSA.$mod.$exp.$private_exp"):

RSA.mVgY8RN6URBTstndvmUUPb4UZTdwvwmddSKE5z_jvKUEK6yk1u3rrC9yN8k6FilGj9K0eeUPe2hf4Pj-5CmHww==.AQAB.Lgy_yL3hsLBngkFdDw1Jy9TmSRMiH6yihYetQ8jy-jZXdsZXd8V5ub3kuBHHk4M39i3TduIkcrjcsiWQb77D8Q==

...to a Crypt::RSA object. I've split out the components so I have $mod, $exp, and $private_exp, but the perl Crypt::RSA API doesn't seem to have a way to explicitly set.

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

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

发布评论

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

评论(1

╰ゝ天使的微笑 2024-09-18 22:06:59

在 IRC 上完成,为世界其他地方记录它:它完全没有记录,但是 Crypt::RSA::Key 确实 有名为 n、ed 分别对应模数、公共指数和私有指数。检查函数中的模错误(如果 pq 不可用但 n 可用,则应该起作用,但实际上没有),可以使用这些方法创建工作密钥。

我们通过使用解码 Base64 编码的工厂方法创建 Crypt::RSA::Key::Private 的子类(使用 MIME::Base64::URLSafe)和附加的二进制编码(使用 Math::BigInt->from_hex 和 解压“H*”),然后设置这三个私有成员,并且 Crypt::RSA 模块能够接受它作为密钥。

Worked out on IRC, documenting it here for the rest of the world: it's completely undocumented but Crypt::RSA::Key does have methods called n, e, and d that correspond to the modulus, the public exponent, and the private exponent. Modulo bugs in the check function (which is supposed to work if p and q are unavailable but n is, but actually doesn't), it's possible to create a working key with those methods.

We solved the problem together by creating a subclass of Crypt::RSA::Key::Private with a factory method that decodes the base64 encoding (using MIME::Base64::URLSafe) and the additional binary encoding (using Math::BigInt->from_hex and unpack "H*") and then sets those three private members, and the Crypt::RSA modules were able to accept it as a Key.

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