我正在开发一项功能,需要我在 PHP 中对一个短字符串进行数字签名,并在 C# 中验证该字符串的签名。
我真的很想用 openssl_sign在 PHP 中,因为它很简单,但我在 Google 上找到的所有信息都表明这行不通。
有some external libraries声称做得很好,但是由于这是一个业余项目,我宁愿不购买这样的图书馆。
那么这里有哪些选择呢?需要 C# 和 PHP 之间的完全互操作性。可以使用 OpenSSL 以外的库。
最佳答案
我使用 Bouncy Castle Crypto APIs 做了非常相似的事情. PHP openssl_sign 似乎默认使用 SHA1。如果您使用的不是默认值,则需要更改 GetSigner 的算法参数。
string base64pubkey = "<!-- BASE64 representation of your pubkey from open ssl -->";
RsaKeyParameters pubKey = PublicKeyFactory.CreateKey(Convert.FromBase64String(base64pubkey)) as RsaKeyParameters;
byte[] signature = Convert.FromBase64String("<!-- BASE64 representation of your sig -->");
byte[] message = Encoding.ASCII.GetBytes("Something that has been signed");
ISigner sig = SignerUtilities.GetSigner("SHA1WithRSAEncryption");
sig.Init(false, pubKey);
sig.BlockUpdate(message, 0, message.Length);
if (sig.VerifySignature(signature))
{
Console.WriteLine("all good!");
}
关于c# - 使用私钥对 PHP 进行数字签名,在 C# 中进行验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/856302/