c# - 使用私钥对 PHP 进行数字签名,在 C# 中进行验证

标签 c# php openssl digital-signature

我正在开发一项功能,需要我在 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/

相关文章:

c++ - OpenSSL AES 仅适用于某些机器,即使使用静态链接也是如此

c# - 显示没有断点的当前调用堆栈

c# - 以 C/C++ 作为后端的 WinForms 或 WPF 或 Qt for Windows GUI

php - 用于字符串匹配结束的函数 preg_match()

php - 在数据库中存储用户全名时有什么更好的做法? 1 列或 2 列?

c++ - 如何将 PKCS7_sign 结果转换为 char * 或 std::string

c# - IRC 协议(protocol) - 持续的客户端连接好吗?

c# - 无法将声音文件上传到 IBM Watson 语音转文本

php - 下拉菜单选择不导航到链接

c - C 中的点对点 linux 身份验证