I'm creating a license key generator that RSA signs base64 encoded license key data and appends signature to license data.On the end user side signature is verified using the public key that is bundled with application.We have applications running on .net, php, java, and other platforms.License model should be same for all. Now to the trouble:I have created 2048bit public/private key in C# using the built in cryptography provider and exported the keys in XML format to get the key BigInteger components transparent in b64 encoding.License key generator should work in php and I'm using the phpseclib to import the XML keys and sign the data.Php code:
function sign($data)
{
$RSA_PRIVATE_KEY = "<RSAKeyValue>
<Modulus>oPmCaLewqKTkRPpKLHz+9hEbrUbVdKNkIxzm4h5/wmuId6PSx6ntV2T44/NSzfx56LffyXvzx27GYEKk4zffpPYKdRIGReZVqfl2U5K3jcuv0h4657ge0nZ5W9vrYMDSFjgtZOSw8t2opWC8IZVUI3X9W8mp1Z3xqtwREpLC/kV0YE1Kmem8k8B4Tv1+qj5C6oPJjhy+T3JipYOQIyVRUoCdo3EHpDQ6K7twKD+Si/rASHyvoCJZTPvWI1w9y9FD8CxstkY7+ap8J9DMczyiFsE4kJtziBKZFph4ZRqz4kSA0TXj0lXhGEAcn1IDjhV5W37PdsWOkUTxBH7Bm7mpuw==</Modulus>
<Exponent>AQAB</Exponent>
<P>xU8K0Z1Mn0cLOHrwMIujXvtJbXpmjFfyOkm+WkeNV2hjDmdgMibJJvhqPEYPSabEHF4us8H4IeEqeInGK04LrYmbe3IVgmuGdpacR01WISbqXiarsKbcHkR65mfotQmxay/n3Swi8hN9yTanSJyhoHFBiJ0qYM0VNdamu7LDeGk=</P>
<Q>0Nukxy2o/o7z6NeR9g+dUcOQzXfWS2Bu4vAXyHGpJArGcIzl4Z2jNuUztGj5sO5b+9SQs4tgijsjbsEQUNN7R1+PB7Zwc50PvGgEvGaIJkPphGoJMNCw4Q2I8tNPb86cEd4WLJ8E6ad93vX0tyZAnn3LrjPg3Bvvxtq157jZLIM=</Q>
<DP>tGRV0dtsyFrdyV+s5dVlIlvAgFVeGIX3so7leAjfEsEff3XIH1ISqoyIJF8xbvcHaaA6NqLqx57jg50DD2iliJ29B5oATGMeZqHAc/gi/OBlensEkdecfBfD/Y+W1J3uFb+Qz0ehE436fNJ5EwwRQW0Kq2p16lbWQ4jim80OpbE=</DP>
<DQ>vML2bumuhbrvaK6EBa5hEce9dGXtcJyMO2ChLhDDvIZciNZe4YUWQQPvsgr6OFWFHtojmZHLQ8NlJ7EnrNUl4wDThTX29haqZS5hsWC9hk/0mi83dT33zr7r2gLvFW7XETL2OYfS6dXt5ffHH0xcNKIe1qeef3BkSgXbR72B0j8=</DQ>
<InverseQ>TffUf2HXQOmVEUo6rinASIOJOfwfucd+MjwDaK61bISFY5J2n3MYRX4wEiFxXlcYgXnR9CsbwRVhUDIDzLuB+ykAG62LkbqCKUYjzyN3RDbc/MSj/sXSw60pTc4DesVhQlumd0zGUOxsFkjOlUowxwIDENiSVFew1IfAOZiakpk=</InverseQ>
<D>AKHPgpxrXn4nQfi+9HsZKoYurE4sOw+ug6TIE03jWolpjm606SvK+XOKtqUXR3pyUBjzZqsh7eqKxN3+H8DxvogTdRo5BBU/c4dokN4b8maWWNDdkliwEPYoy9Tf5mVbbdLn+rlwfcOjtzfbWpZnhNbLGTfVfuKRNwaI2qB7kNwGB1fd1t4xMLWNozgoxFuiiKFbJKmLEj9zHP/KqjEnOH/zEuUBnXiqGMnCMmD7KnD5WqcqSDOahn0TBFlMxV+9Ul2447bu5LTGWhm3RBPEGgtjMboKC3PlgqwYDpC2gbzX5ZsBNCiuGgumxBHgOfAIOVzI01MZTFEpTcTt3BUrwQ==</D>
</RSAKeyValue>";
$RSA = new Crypt_RSA();
$RSA->loadKey($RSA_PRIVATE_KEY, CRYPT_RSA_PRIVATE_FORMAT_XML);
$RSA->setHash(CRYPT_SHA512);
$RSA->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
return $RSA->sign($data);
}
C# Code:
private String signData(String data) {
byte[] private_key_modulus_array = Convert.FromBase64String(B64_MODULUS);
byte[] private_key_exponent_array = Convert.FromBase64String(B64_EXPONENT);
byte[] private_key_p_array = Convert.FromBase64String(B64_P);
byte[] private_key_q_array = Convert.FromBase64String(B64_Q);
byte[] private_key_dp_array = Convert.FromBase64String(B64_DP);
byte[] private_key_dq_array = Convert.FromBase64String(B64_DQ);
byte[] private_key_inverseQ_array = Convert.FromBase64String(B64_INVERSE_Q);
byte[] private_key_d_array = Convert.FromBase64String(B64_D);
//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSAParameters RSAKeyInfo = new RSAParameters();
//Set RSAKeyInfo to the private key values.
RSAKeyInfo.Modulus = private_key_modulus_array;
RSAKeyInfo.P = private_key_p_array;
RSAKeyInfo.Q = private_key_q_array;
RSAKeyInfo.DP = private_key_dp_array;
RSAKeyInfo.DQ = private_key_dq_array;
RSAKeyInfo.D = private_key_d_array;
RSAKeyInfo.InverseQ = private_key_inverseQ_array;
RSAKeyInfo.Exponent = private_key_exponent_array;
RSA.ImportParameters(RSAKeyInfo);
return Convert.ToBase64String(RSA.SignData(Encoding.ASCII.GetBytes(data), new SHA512CryptoServiceProvider()));
}
Generated signature is not same when generated with C# and PHP. I'm thinking that maybe the phpseclib did not parse the private key correctly??
On the C# side signature is same when created with built in cryptography and bouncycastle lib.
What should I do? Do I need any other key format to achieve consistency??
Thanx for help.