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

2023-09-03 00:09:27 作者:我走位像风

在PHP中,我RSA加密消息由.NET应用程序解密...但我不断收到来自.NET中的坏键异常....

有关RSA加密的,我使用的公钥加密(这是一个模数,指数对),我从工作的加密系统在.NET ... >

我想最简单的问题将是─>做坏键表示它无法将任何消息解密? IE浏览器,它是不加密是否正确?

更难的问题是 - >有没有具体的关于RSA加密,导致.NET和PHP的怪癖什么?

解决方案 php的rsa加解密返回失败的问题

如果你想使用不需要OpenSSL的扩展的解决方案,尽量phpseclib的CRYPT_RSA。例子如下:

解密与PKCS#1填充:

OpenSSL的rsautl -inkey privatekey.txt -encrypt -in plaintext.txt退房手续ciphertext.txt

 < PHP
包括(地穴/ RSA.php');

$ RSA =新CRYPT_RSA();
$ rsa-> LOADKEY(的file_get_contents('privatekey.txt'));
$ rsa-> setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
回声$ rsa->解密(的file_get_contents('ciphertext.txt'));
?>
 

加密与PKCS#1填充:

 < PHP
包括(地穴/ RSA.php');

$ RSA =新CRYPT_RSA();
$ rsa-> LOADKEY(的file_get_contents('privatekey.txt'));
$ rsa-> LOADKEY($ rsa->的getpublickey());
$ rsa-> setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
回声$ rsa->加密('1234567890');
?>
 

OpenSSL的rsautl -inkey privatekey.txt -decrypt -in ciphertext.txt退房手续plaintext.txt

解密与OAEP填充:

OpenSSL的rsautl -inkey privatekey.txt -encrypt -oaep -in plaintext.txt退房手续ciphertext.txt

 < PHP
包括(地穴/ RSA.php');

$ RSA =新CRYPT_RSA();
$ rsa-> LOADKEY(的file_get_contents('privatekey.txt'));
回声$ rsa->解密(的file_get_contents('ciphertext.txt'));
?>
 

加密与OAEP填充:

 < PHP
包括(地穴/ RSA.php');

$ RSA =新CRYPT_RSA();
$ rsa-> LOADKEY(的file_get_contents('privatekey.txt'));
$ rsa-> LOADKEY($ rsa->的getpublickey());
回声$ rsa->加密('1234567890');
?>
 

OpenSSL的rsautl -inkey privatekey.txt -decrypt -oaep -in ciphertext.txt退房手续plaintext.txt

phpseclib可以从 http://phpseclib.sourceforge.net/

下载

祝你好运!

In PHP I am RSA encrypting a message to be decrypted by .NET application... but I keep getting a "Bad Key" exception from .NET....

For RSA encryption, I am using PEAR class Crypt_RSA-> encrypting with the public key (which is a modulus, exponent pair) I get from working encryption system in .NET...

I guess the easiest question would be-> does "Bad Key" mean it is not able to decrypt the message whatsoever? IE, it is not encrypted correctly?

The harder question is-> Is there anything specific about RSA encryption that causes quirks between .NET and PHP?

解决方案

If you want to use a solution that doesn't require the openssl extension, try phpseclib's Crypt_RSA. Examples follow:

Decryption with PKCS#1 padding:

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'));
?>

Encryption with PKCS#1 padding:

<?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');
?>

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

Decryption with OAEP padding:

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'));
echo $rsa->decrypt(file_get_contents('ciphertext.txt'));
?>

Encryption with OAEP padding:

<?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 can be downloaded from http://phpseclib.sourceforge.net/

Good luck!