I am signing data from the client in c# and then transmitting the data to a php web server which is supposed to return Verified
if the data has not been tampered with but it always returns Unverified
. I use http://phpseclib.sourceforge.net/rsa/intro.html to verify the RSA data. This is the c# function that signs the data:
public string SignRSA(string privateKey, string rawData)
{
ASCIIEncoding ByteConverter = new ASCIIEncoding();
byte[] originalData = ByteConverter.GetBytes(rawData);
RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();
RSAalg.FromXmlString(privateKey);
return Convert.ToBase64String(RSAalg.SignData(originalData, new SHA256Managed()));
}
Here is the php code that verifies the data:
$key = str_replace("%3c", "<", $_POST['PublicKey']);
$key = str_replace("%3e", ">", $key);
$key = str_replace("%3d", "=", $key);
$key = str_replace("%2f", "/", $key);
$key = str_replace("%2b", "+", $key);
$sig = str_replace("%2f", "/", $_POST['Signature']);
$sig = str_replace("%2b", "+", $sig);
$rsa = new Crypt_RSA();
$rsa->loadKey($key);
$rsa->setPublicKey();
$rsa->setHash(sha256);
$rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
//$publickey = $rsa->getPublicKey();
echo $rsa->verify(unpack("C*", $_POST["Text"]), base64_decode($sig)) ? 'verified' : 'unverified';
All of the str_replace
functions are used because the /'s are replaced by %2f, ='s are replaced by %3d, etc.