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
what am i missing?