在C#中的服务器和客户端PHP使用RSA密钥 [英] Using RSA KeyPair in C# server and PHP Client
问题描述
我有会生成一个RSA键值对一个C#服务器。公钥将被发送到一个PHP客户端然后将加密的一些数据,并传送给服务器。然后服务器将解密使用它有私钥
I have a C# server that will generate a RSA KeyValue Pair. The public key will be sent to a PHP client which will then encrypt some data and send to server. The server will then decrypt using the private key it has.
我做的,使用下面的代码在C#中 -
I am doing that using the following code in C# -
CspParameters cspParams = new CspParameters { ProviderType = 1 };
RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(1024, cspParams);
string publicKey = Convert.ToBase64String(rsaProvider.ExportCspBlob(false));
string privateKey = Convert.ToBase64String(rsaProvider.ExportCspBlob(true));
现在我需要通过生成一个PHP客户端的公钥。
但问题是,密钥字符串产生在这里的C#,当我在下面的函数中使用它不是由PHP认可 -
Now I need to pass on the public key generated to a PHP Client. But the problem is that Key String generated here in C# is not recognised by PHP when i use it in the function as below -
public function encrypt($data)
{
$pubkey = 'BgIAAACkAABSU0ExAAIAAAEAAQBdZ3klDbVjH8oiBtGzHIMixo/TKPlv492kuau9chnARvkpxaRd8Qa82kIF2AvrEllhzjD07UHkVxoVZA2aYN+t'
$pubKey4 = openssl_get_publickey( $pubkey );
openssl_public_encrypt($data, $encrypted, $pubKey4 )
}
openssl_public_encrypt()
功能显示了这样的警告:
openssl_public_encrypt()
function shows such warning:
警告:openssl_public_encrypt():关键参数是不是一个有效的公共
在C按键:\wamp\www\rsa\index.php
Warning: openssl_public_encrypt(): key parameter is not a valid public key in C:\wamp\www\rsa\index.php
请建议,我应该是由PHP被识别的公钥格式。
Please suggest, what shall be the format of the public Key that shall be recognised by PHP.
X509证书是不是一种选择。
X509 certificates are not an option.
推荐答案
据3v4l , openssl_get_publickey()
将返回假
。你需要给它一个 PEM格式的公钥。
According to 3v4l, openssl_get_publickey()
is returning false
. You need to give it a PEM formatted public key.
如果你想要一个简单的界面,你就不太可能做出有损你的应用程序的安全错误,检查出的 libsodium 。
If you want a simpler interface that you're less likely to make a mistake that undermines the security of your application, check out libsodium.
Libsodium是的氯化钠,一个易于使用的高速软件库进行网络通信,加密,解密,签名等由Daniel J.伯恩斯坦,塔尼娅兰格和彼得·施瓦布(三大世界级加密写入他们的研究椭圆曲线密码算法和边信道密码分析)的知名专家。
Libsodium is a fork of NaCl, an easy-to-use high-speed software library for network communication, encryption, decryption, signatures, etc. written by Daniel J. Bernstein, Tanja Lange, and Peter Schwabe (three world-class cryptography experts known for their research into elliptic curve cryptography and side-channel cryptanalysis).
Libsodium为NaCl的便携式叉子更易于使用,并且有很多有价值的功能(与scrypt例如密码哈希)。
Libsodium is a portable fork of NaCl that is easier to use and has a lot of valuable features (e.g. password hashing with scrypt).
using Sodium;
// snip
var keypair = PublicKeyBox.GenerateKeyPair();
string secretKey = Convert.ToBase64String(keypair.PrivateKey);
string publicKey = Convert.ToBase64String(keypair.PublicKey);
请务必阅读的相关libsodium .NET文档了解如何使用它。
<?php
$decoded = base64_decode($encoded_publickey);
define('YOUR_RAW_BINARY_CSHARP_PUBLIC_KEY', $decoded);
$php_keypair = \Sodium\crypto_box_keypair();
$php_public = \Sodium\crypto_box_publickey($php_keypair);
$php_secret = \Sodium\crypto_box_secretkey($php_keypair);
$nonce = \Sodium\randombytes_buf(\Sodium\CRYPTO_BOX_NONCEBYTES);
$message_keypair = \Sodium\crypto_box_keypair_from_secretkey_and_publickey(
$php_secret,
YOUR_RAW_BINARY_CSHARP_PUBLIC_KEY
);
$encrypted = \Sodium\crypto_box(
$message,
$nonce,
$message_keypair
);
$加密
将原始的二进制;在C#应用程序的需求 $随机数
, $加密
和 $ php_public
(除了自己的秘密密钥)来解密 $加密
来看看 $消息
包含
$encrypted
will be raw binary; the C# app needs $nonce
, $encrypted
, and $php_public
(in addition to its own secret key) to decrypt $encrypted
to see what $message
contains.
请务必阅读的有关PHP libsodium文档如何使用它。
Be sure to read the relevant PHP libsodium documentation for how to use it.
<?php
$anon_msg = \Sodium\crypto_box_seal($message, YOUR_RAW_BINARY_CSHARP_PUBLIC_KEY);
的
的 crypto_box_seal
文档是必读的。
The crypto_box_seal
documentation is a must-read.
这篇关于在C#中的服务器和客户端PHP使用RSA密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!