本文介绍了phpseclib 仅使用公钥解密和加密数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么 phpseclib 中只有一个公钥就可以执行(加密/解密)RSA?

Hi I would like to know why RSA can be performed (encrypt/decrypt) with only one public key in phpseclib?

$rsa 是 phpseclib/Crypt/RSA.php 的一个实例(链接:https://github.com/phpseclib/phpseclib/blob/master/phpseclib/Crypt/RSA.php)这里的 $publicKey 键是一样的.

$rsa is an instance of phpseclib/Crypt/RSA.php (link: https://github.com/phpseclib/phpseclib/blob/master/phpseclib/Crypt/RSA.php)$publicKey keys here are the same.

function encryptData($data, $publicKey) {
    $rsa = new Crypt_RSA();
    $rsa->loadKey($publicKey); // public key
    $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
    $output = $rsa->encrypt($data);
    return base64_encode($output);
}

function decryptData($data, $publicKey) {
    $rsa = new Crypt_RSA();
    $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
    $ciphertext = base64_decode($data);
    $rsa->loadKey($publicKey); // public key
    $output = $rsa->decrypt($ciphertext);
    // $output = $rsa->decrypt($data);

我不明白为什么上面的代码有效.出于同样的原因,我无法在需要密钥对进行此类操作的 Ruby 中实现它.任何帮助将不胜感激.

I don't understand why the above code works.And for the same reason, I can't implement it in Ruby which requires a key pair for such operation. Any help would be appreciate.

推荐答案

在最基本的形式中,RSA 密钥所需的一切,无论是公共的还是私有的,都是指数和模数.在实践中,私钥通常有额外的参数来通过中国剩余定理加速计算,但在实践中,他们不需要.

In it's most basic form all you need for an RSA key, be it public or private, is an exponent and a modulo. In practice, private keys often have additional parameters to speed up computation via the Chinese Remainder Theorem, but in practice, they don't need that.

因此,在 RSA 的最基本形式中,公钥和私钥是不可区分的.

So in RSA's most basic form, a public and private key are indistinguishable from one another.

也就是说,如果你想加密/解密,你仍然必须同时拥有.

That said, you still gotta have both if you want to encrypt / decrypt.

我不明白为什么上面的代码有效.

它不起作用.不像您所期望的那样.

It doesn't work. Not like you seem to be expecting it to.

好吧,现在,它实际上什至没有做任何事情.您只有一个加密和解密函数,但没有向它传递任何参数,只是假设它对两者使用相同的密钥.但不是如下所示:

Well, right now, it doesn't actually even do anything. You just have an encrypt and decrypt function but aren't passing it any parameters and are just presupposing it works with the same key for both. But it doesn't as the following demonstrates:

<?php

include('Crypt/RSA.php');


function encryptData($data, $publicKey) {
    $rsa = new Crypt_RSA();
    $rsa->loadKey($publicKey); // public key
    $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
    $output = $rsa->encrypt($data);
    return base64_encode($output);
}

function decryptData($data, $publicKey) {
    $rsa = new Crypt_RSA();
    $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
    $ciphertext = base64_decode($data);
    $rsa->loadKey($publicKey); // public key
    $output = $rsa->decrypt($ciphertext);
    // $output = $rsa->decrypt($data);
    return $output;
}

$rsa = new Crypt_RSA();
extract($rsa->createKey());

$ciphertext = encryptData('zzz', $publickey);
echo decryptData($ciphertext, $publickey);

这会产生解密错误.

现在,如果你用这个替换最后一行:

Now, if you replace that last line with this:

echo decryptData($ciphertext, $privatekey);

这样做,您将获得原始文本.但是您必须使用私钥才能取回该文本 - 而不是公钥.

Do that and you'll get the original text back. But you have to use the private key to get that text back - not the public key.

这篇关于phpseclib 仅使用公钥解密和加密数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 08:48