奇尔卡特,BouncyCastle的,的RSACryptoServiceProvider:用公钥RSA C#加密用PHP openssl_private_decrypt()使用 [英] RSA C# encryption with public key to use with PHP openssl_private_decrypt(): Chilkat, BouncyCastle, RSACryptoServiceProvider

查看:582
本文介绍了奇尔卡特,BouncyCastle的,的RSACryptoServiceProvider:用公钥RSA C#加密用PHP openssl_private_decrypt()使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要在字符串中的C#应用​​程序加密,它在PHP侧openssl_private_decrypt解密()。
,我试图用从PHP的openssl_pkey_get_details($ privateKey)内获得解密首次公开密钥;
谈论未来,我能够使用奇尔卡特做到这一点,但它确实在MonoTouch的iOS的下所以它是无用的,我不工作(和它的成本几乎太)。
有代码示例如何我做它:

i'm trying to encrypt the string in C# application for it to be decrypted on PHP side with openssl_private_decrypt(). the initial public key that i'm trying to decrypt with is obtained from within PHP's openssl_pkey_get_details($privateKey); talking ahead, i am able to do it using chilkat, but it doesn't work in monotouch under iOS so it is useless for me (and it costs pretty much too). there is code sample how i'm doing it:

void TestEncryption(string publicKey, string data)
{
        var bytes = Encoding.UTF8.GetBytes(data);

        //by using chilkat
        var key = new Chilkat.PublicKey();
        var result = key.LoadOpenSslPem(publicKey);
        var ck = new Chilkat.Rsa();
        ck.UnlockComponent("blablabla");
        var keyXml = key.GetXml();
        ck.ImportPublicKey(keyXml);
        var ckBytes = ck.EncryptBytes(bytes, false);

        //by using BouncyCastle
        Object obj;
        using (TextReader sr = new StringReader(publicKey))
        {
            PemReader pem = new PemReader(sr);
            obj = pem.ReadObject();               
        }
        var par = obj as Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters;
        RsaEngine e = new RsaEngine();
        e.Init(true, par);           
        var bcBytes = e.ProcessBlock(bytes, 0, bytes.Length);

        //RSACryptoServiceProvider. don't know exectly how to initialize it, one way that must work i think is from chilkat's xml, just to test
        RSACryptoServiceProvider csp = new RSACryptoServiceProvider(1024);
        csp.FromXmlString(keyXml);

        //also tryed to initialize like this:
        //var pp = csp.ExportParameters(false);
        //pp.Modulus = byteKey; //this one is publicKey, stripped from --begin-- and --end-- sections and encoded from base64 to byte array (162 bytes)
        //csp.ImportParameters(pp);
        //
        //or if i do
        //var pp = new RSAParameters();
        //pp.Exponent = Convert.FromBase64String("AQAB");
        //pp.Exponent array is the same: {1, 0, 1};

        var cspBytes = csp.Encrypt(bytes, false); //or (bytes, true), doesn't matter           

        //this function sends request to php and returns result of decryption attempt         
        Debug.WriteLine(ExecRequest(ckBytes));
        Debug.WriteLine(ExecRequest(bcBytes));
        Debug.WriteLine(ExecRequest(cspBytes));
}



输出为:

the output is:

success
decryption failed
decryption failed

我失去了什么?

PS哦,这个计算器因此通过拥挤的问题,并有我会回答
,但我在想解决方案会在写一个问题的过程中一点点机会。可惜不是

p.s. oh, this stackoverflow so crowded by questions and there is a little chance my will be answered but i was thinking solution will come in process of writing a question. unfortunately not

推荐答案

OK,做一些萨满教舞蹈击鼓后,我已经到一定程度,满意的解决方案。
,请持有记住,它是所有与PHP的openssl_private_decrypt()函数工作的情况下完成的。

ok, after doing some shamanic dances with tambourine i've come to somewhat-satisfying solution. please hold in mind, that it is all done in context of working with PHP's openssl_private_decrypt() function.

所以我就下一:


  1. BouncyCastle的加密只是做不对

  1. encryption of BouncyCastle just doing it not right

我不知道怎么弄模出私钥用的RSACryptoServiceProvider使用它

i don't know how to get modulus out of private key to use it with RSACryptoServiceProvider

为什么不尝试使用BouncyCastle的重点数据并进行初始化的RSACryptoServiceProvider

why not try to use BouncyCastle's key data and initialize RSACryptoServiceProvider

Object obj;
using (TextReader sr = new StringReader(publicKey))
{
    PemReader pem = new PemReader(sr);
    obj = pem.ReadObject();               
}
var par = obj as Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters;

RSACryptoServiceProvider csp = new RSACryptoServiceProvider(1024);        
//var pp = csp.ExportParameters(false); //works on native .NET, doesn't work on monotouch
var pp = new RSAParameters();
pp.Modulus = par.Modulus.ToByteArrayUnsigned(); //doesn't work with ToByteArray()
pp.Exponent = par.Exponent.ToByteArrayUnsigned();                          
csp.ImportParameters(pp);
var cspBytes = csp.Encrypt(bytes, false); 


其实,令人奇怪的是我与解决方案想出了在所有的辩论,因为这些细微的。
我希望这将是一个人一天有帮助的。

actually, it is strange that i've came up with solution at all, cuz of these niceties. i hope it would be helpful for someone someday.

这篇关于奇尔卡特,BouncyCastle的,的RSACryptoServiceProvider:用公钥RSA C#加密用PHP openssl_private_decrypt()使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆