RSA在与OpenSSL的在PHP 5.3.2加密C#(.NET 3.5)的数据进行解密 [英] RSA decrypting data in C# (.NET 3.5) which was encrypted with openssl in php 5.3.2
问题描述
也许有人可以让我清醒。我一直在上网对这个有一段时间了。
Maybe someone can clear me up. I have been surfing on this a while now.
1) openssl req -x509 -nodes -days 3650 -newkey rsa:1024 -keyout privatekey.pem -out mycert.pem
2) openssl rsa -in privatekey.pem -pubout -out publickey.pem
3) openssl pkcs12 -export -out mycertprivatekey.pfx -in mycert.pem -inkey privatekey.pem -name "my certificate"
第二步:请问关于PHP根证书的工作:YES
PHP端
我使用的公钥。 PEM读入PHP的:
Step #2: Does root certificate work on php: YES
PHP sideI used the publickey.pem to read it into php:
$publicKey = "file://C:/publickey.pem";
$privateKey = "file://C:/privatekey.pem";
$plaintext = "123";
openssl_public_encrypt($plaintext, $encrypted, $publicKey);
$transfer = base64_encode($encrypted);
openssl_private_decrypt($encrypted, $decrypted, $privateKey);
echo $decrypted; // "123"
或
$server_public_key = openssl_pkey_get_public(file_get_contents("C:\publickey.pem"));
// rsa encrypt
openssl_public_encrypt("123", $encrypted, $server_public_key);
//and the privatekey.pem to check if it works:
openssl_private_decrypt($encrypted, $decrypted, openssl_get_privatekey(file_get_contents("C:\privatekey.pem")));
echo $decrypted; // "123"
即将得出结论,即加密/解密正常工作在PHP端与。这些OpenSSL的根证书文件
Coming to the conclusion, that encryption/decryption works fine on the php side with these openssl root certificate files.
在相同的方式我读的钥匙到.NET的C#控制台程序:
In same manner I read the keys into a .net C# console program:
X509Certificate2 myCert2 = null;
RSACryptoServiceProvider rsa = null;
try
{
myCert2 = new X509Certificate2(@"C:\mycertprivatekey.pfx", "password");
rsa = (RSACryptoServiceProvider)myCert2.PrivateKey;
}
catch (Exception e)
{
Console.writeln(e.message); // because I left a blank catch block, I did not realize there was an exception! I missed the password for the certificate.
}
byte[] test = {Convert.ToByte("123")};
string t = Convert.ToString(rsa.Decrypt(rsa.Encrypt(test, false), false));
即将到一点,那加密/解密正常工作与这些OpenSSL的根证书文件C#端
Coming to the point, that encryption/decryption works fine on the c# side with these openssl root certificate files.
$onett = "123"
....
openssl_public_encrypt($onett, $encrypted, $server_public_key);
$onettbase64 = base64_encode($encrypted);
复制 - 粘贴$ onettbase64(LkU2GOCy4lqwY4vtPI1JcsxgDgS2t05E6kYghuXjrQe7hSsYXETGdlhzEBlp + qhxzTXV3pw + AS5bEg9CPxqHus8fXHOnXYqsd2HL20QSaz + FjZee6Kvva0cGhWkFdWL + ANDSOWRWo / OMhm7JVqU3P / 44c3dLA1eu2UsoDI26OMw = )转换成C#程序:
copy - paste $onettbase64 ("LkU2GOCy4lqwY4vtPI1JcsxgDgS2t05E6kYghuXjrQe7hSsYXETGdlhzEBlp+qhxzTXV3pw+AS5bEg9CPxqHus8fXHOnXYqsd2HL20QSaz+FjZee6Kvva0cGhWkFdWL+ANDSOWRWo/OMhm7JVqU3P/44c3dLA1eu2UsoDI26OMw=") into c# program:
byte[] transfered_onett = rsa.Decrypt(Convert.FromBase64String("LkU2GOCy4lqwY4vtPI1JcsxgDgS2t05E6kYghuXjrQe7hSsYXETGdlhzEBlp+qhxzTXV3pw+AS5bEg9CPxqHus8fXHOnXYqsd2HL20QSaz+FjZee6Kvva0cGhWkFdWL+ANDSOWRWo/OMhm7JVqU3P/44c3dLA1eu2UsoDI26OMw="), false);
string result = System.Text.Encoding.UTF8.GetString(transfered_onett); // "123"
没有问题。
推荐答案
您需要使用而设计的PFX的X509Certificate2构造函数之一(又名PKCS#12)文件。这些都需要一个密码参数。在你原来的例子,你默默吞咽所有的异常,所以你错过了错误。
You need to use one of the X509Certificate2 constructors that are designed for PFX (aka pkcs#12) files. These take a password argument. In your original example, you were silently swallowing all exceptions so you missed the error.
这篇关于RSA在与OpenSSL的在PHP 5.3.2加密C#(.NET 3.5)的数据进行解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!