RSA在PHP中加密以在.NET中解密

RSA在PHP中加密以在.NET中解密,php,.net,rsa,pear,Php,.net,Rsa,Pear,在PHP中,我使用RSA加密要由.NET应用程序解密的消息。。。但我一直从.NET收到一个“坏键”异常 对于RSA加密,我使用PEAR类Crypt_RSA->使用公钥加密(这是一个模,指数对),我从.NET中的工作加密系统中获得 我想最简单的问题是->是否“坏密钥”意味着它无法解密消息?也就是说,它没有正确加密 更难的问题是->RSA加密是否有什么特殊之处会导致.NET和PHP之间出现问题?PEAR上的Crypt_RSA没有使用PKCS#1编码。我怀疑这就是.NET向您发送错误消息的原因 作为破

在PHP中,我使用RSA加密要由.NET应用程序解密的消息。。。但我一直从.NET收到一个“坏键”异常

对于RSA加密,我使用PEAR类Crypt_RSA->使用公钥加密(这是一个模,指数对),我从.NET中的工作加密系统中获得

我想最简单的问题是->是否“坏密钥”意味着它无法解密消息?也就是说,它没有正确加密


更难的问题是->RSA加密是否有什么特殊之处会导致.NET和PHP之间出现问题?

PEAR上的Crypt_RSA没有使用PKCS#1编码。我怀疑这就是.NET向您发送错误消息的原因

作为破坏它的一个例子,我使用Crypt_RSA创建了一个php脚本来加密字符串“1234567”(我将跳过显示密钥加载):

将其输出通过openssl命令行工具进行管道传输会产生以下错误:

$ ./crypt | openssl rsautl -inkey privkey.pem -decrypt
RSA operation error
18437:error:04065084:rsa routines:RSA_EAY_PRIVATE_DECRYPT:data too large for modulus:fips_rsa_eay.c:558:
openssl在默认情况下需要PKCS#1填充,但是将-raw(无填充)标志添加到openssl也没有帮助

在php中使用openssl扩展提供了适当的填充(默认为PKCS#1,其他可用):

以及php中的解密代码:

$pem = file_get_contents("privkey.pem");
$key = openssl_pkey_get_private($pem);

$enc_data = file_get_contents("openssl.crypted");
$decrypted = "";
if(openssl_private_decrypt($enc_data, $decrypted, $key)) {
  print "$decrypted\n";
} else {
  print "failed\n";
}
RSA上下文中的证书是X.509证书,它们是RSA密钥加上关于这些密钥的数据。SSL中使用了X.509证书,但使用RSA不需要这些证书

安全警告:使用OAEP

如果您想使用不需要openssl扩展的解决方案,请尝试phpseclib的Crypt_RSA。例子如下:

使用PKCS#1填充进行解密:

<?php
include('Crypt/RSA.php');

$rsa = new Crypt_RSA();
$rsa->loadKey(file_get_contents('privatekey.txt'));
$rsa->loadKey($rsa->getPublicKey());
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
echo $rsa->encrypt('1234567890');
?>
<?php
include('Crypt/RSA.php');

$rsa = new Crypt_RSA();
$rsa->loadKey(file_get_contents('privatekey.txt'));
$rsa->loadKey($rsa->getPublicKey());
echo $rsa->encrypt('1234567890');
?>
openssl rsautl-inkey privatekey.txt-encrypt-in plaintext.txt-out ciphertext.txt

<?php
include('Crypt/RSA.php');

$rsa = new Crypt_RSA();
$rsa->loadKey(file_get_contents('privatekey.txt'));
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
echo $rsa->decrypt(file_get_contents('ciphertext.txt'));
?>
<?php
include('Crypt/RSA.php');

$rsa = new Crypt_RSA();
$rsa->loadKey(file_get_contents('privatekey.txt'));
echo $rsa->decrypt(file_get_contents('ciphertext.txt'));
?>

使用PKCS加密#1填充:

<?php
include('Crypt/RSA.php');

$rsa = new Crypt_RSA();
$rsa->loadKey(file_get_contents('privatekey.txt'));
$rsa->loadKey($rsa->getPublicKey());
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
echo $rsa->encrypt('1234567890');
?>
<?php
include('Crypt/RSA.php');

$rsa = new Crypt_RSA();
$rsa->loadKey(file_get_contents('privatekey.txt'));
$rsa->loadKey($rsa->getPublicKey());
echo $rsa->encrypt('1234567890');
?>

openssl rsautl-inkey privatekey.txt-decrypt-in-ciphertext.txt-out-plaintext.txt

使用OAEP填充进行解密:

<?php
include('Crypt/RSA.php');

$rsa = new Crypt_RSA();
$rsa->loadKey(file_get_contents('privatekey.txt'));
$rsa->loadKey($rsa->getPublicKey());
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
echo $rsa->encrypt('1234567890');
?>
<?php
include('Crypt/RSA.php');

$rsa = new Crypt_RSA();
$rsa->loadKey(file_get_contents('privatekey.txt'));
$rsa->loadKey($rsa->getPublicKey());
echo $rsa->encrypt('1234567890');
?>
openssl rsautl-inkey privatekey.txt-encrypt-oaep-in plaintext.txt-out ciphertext.txt

<?php
include('Crypt/RSA.php');

$rsa = new Crypt_RSA();
$rsa->loadKey(file_get_contents('privatekey.txt'));
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
echo $rsa->decrypt(file_get_contents('ciphertext.txt'));
?>
<?php
include('Crypt/RSA.php');

$rsa = new Crypt_RSA();
$rsa->loadKey(file_get_contents('privatekey.txt'));
echo $rsa->decrypt(file_get_contents('ciphertext.txt'));
?>

使用OAEP填充进行加密:

<?php
include('Crypt/RSA.php');

$rsa = new Crypt_RSA();
$rsa->loadKey(file_get_contents('privatekey.txt'));
$rsa->loadKey($rsa->getPublicKey());
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
echo $rsa->encrypt('1234567890');
?>
<?php
include('Crypt/RSA.php');

$rsa = new Crypt_RSA();
$rsa->loadKey(file_get_contents('privatekey.txt'));
$rsa->loadKey($rsa->getPublicKey());
echo $rsa->encrypt('1234567890');
?>

openssl rsautl-inkey privatekey.txt-decrypt-oaep-in-ciphertext.txt-out-plaintext.txt

phpseclib可从以下网站下载:


祝你好运

钥匙本身可能有问题吗?奇数字符,UNICODE和UTF-8之间存在问题?您可能使用了不同的填充。RE:Padding:是否与字符串本身有关?在本例中,它正好是32个字节-可以被8整除。。。那我有这个保险吗?钥匙有问题吗?好问题。。。我想钥匙没问题。相同的密钥用于解码.NET应用程序后端的消息(我只是尝试在PHP中复制后端版本)。后端系统工作正常。为了确保我不是完全不懂->公开RSA密钥只是RSA类用来加密消息的模和指数,对吗?我看到了一些对证书的引用,我没有使用这些证书,因为我假设我所需要的只是公钥。这很好,但我强烈建议使用OAEP,不要使用PKCS1填充,因为填充oracle攻击。