用公钥加密

时间:2014-01-06 11:52:04

标签: c# bouncycastle

我尝试在C#和PHP服务器之间使用RSA加密 我有php服务器生成的公钥/私钥

我用C#

写这个
protected void Button1_Click(object sender, EventArgs e)
{

    StreamReader reader = new StreamReader("C:\\Users\\user.ofPC\\Desktop\\priv1024.key");
    PemReader pemReader = new PemReader(reader);
    AsymmetricCipherKeyPair keyPair = (AsymmetricCipherKeyPair)pemReader.ReadObject();
    AsymmetricKeyParameter privateKey = keyPair.Private;
    RSA rsa2 = DotNetUtilities.ToRSA((RsaPrivateCrtKeyParameters)privateKey);
    string xmlRsa = rsa2.ToXmlString(true);
    Debug.WriteLine(xmlRsa);
    Debug.WriteLine("------------------fin");


    var RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
    RSA.FromXmlString(xmlRsa);

    byte[] machainechiffre = RSA.Encrypt(Encoding.Unicode.GetBytes(@"Texte secret Provenant de C#"), false);
    string EncryptedString = Convert.ToBase64String(machainechiffre);
    Debug.WriteLine(EncryptedString);
    Label1.Text = EncryptedString;
}

这是工作 当我在php中使用EncryptedString时,我管理解密的这个字符串

<?PHP
$privateKey = openssl_pkey_get_private(file_get_contents("./priv1024.key"));
$publicKey = openssl_pkey_get_public(file_get_contents("./public1024.key"));
echo"<font red color='red'>";
$encryptedData=base64_decode("pj6y+qbCZM6arWYecOXGlnhziQg0w2BxzrmVJx4SV/28REzAV8hoQk/YtpWps7CNThgHdOhiEKYw8rHdP1TOjA0PsId2a2RPPx8MfywtGMQLUzj46gjTryvgGbuGb2EvNmrGvVaA/zgGCOPRDsbjW0S4fqQc2G2yldnKLfbACc0=");
openssl_private_decrypt($encryptedData, $plainTextData, $privateKey);
echo "<br /><br />Decrypted data using private key are : <br /> ";
echo $plainTextData;
echo "<br /><br />";
echo"</font>";
?>

但是当我在C#中使用我的公钥时,它不起作用

我明白我做了

我用php服务器的私钥加密,我使用相同的私钥来解密它 这很疯狂,我知道...... 如何将公钥用于C#?

0 个答案:

没有答案