Question

Unfortunately I haven't found anything that works for me yet so I'll create a new question.
My PHP Code (using phpseclib's RSA) that signs the string, as you might notice the code is to verify a license code.

<?php
include('Crypt/RSA.php');

$rsa = new Crypt_RSA();
//$rsa->setPassword('password');
$rsa->loadKey('-----BEGIN RSA PRIVATE KEY-----
...
-----END RSA PRIVATE KEY-----
'); // private key

$plaintext = 'AAAAA-AAAAA-AAAAA-AAAAA';

$rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
$signature = $rsa->sign($plaintext);
echo base64_encode($signature);

The C# code that should verify the response:

            bool success = false;
            using (var rsa = new RSACryptoServiceProvider())
            {
                StreamReader reader = new StreamReader(dataStream);
                String signedString = reader.ReadToEnd();
                byte[] signedBytes = Convert.FromBase64String(signedString);
                byte[] bytesToVerify = Encoding.UTF8.GetBytes(value);
                try
                {
                    RSAParameters parameters = new RSAParameters();
                    parameters.Exponent = new byte[] { 0x01, 0x10, 0x01 };
                    parameters.Modulus = OtherClass.StringToByteArray(Program.Modulus);
                    rsa.ImportParameters(parameters);

                    success = rsa.VerifyData(bytesToVerify, new SHA1CryptoServiceProvider(), signedBytes);
                }
                catch (CryptographicException e)
                {
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    rsa.PersistKeyInCsp = false;
                }
            }

dataStream is the webrequest's response stream.

Was it helpful?

Solution

I've solved this now by using another way to load the key.
I've used this online converter to convert my PEM key to a XML key: https://superdry.apphb.com/tools/online-rsa-key-converter

So I changed the try { ... } part to:

rsa.FromXmlString("{XML_KEY}");
success = rsa.VerifyData(bytesToVerify, new SHA1CryptoServiceProvider(), signedBytes);

Now it works just fine. I guess I didn't really understand how to load a key via modulus and exponent.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top