RSA C# encryption with public key to use with PHP openssl_private_decrypt(): Chilkat, BouncyCastle, RSACryptoServiceProvider [closed]

别来无恙 提交于 2019-12-04 22:06:07

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.

so i got next:

  1. encryption of BouncyCastle just doing it not right

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

  3. 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!