使用私钥在PHP中数字签名,在C#中验证 [英] Digitally sign in PHP using private key, verify in C#

查看:544
本文介绍了使用私钥在PHP中数字签名,在C#中验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在那个需要我进行数字签名在PHP中的短字符串,并验证字符串在C#签名功能的工作。

I am working on a feature that needs me to digitally sign a short string in PHP, and verify the string's signature in C#.

我真的很想用 openssl_sign 在PHP中,因为它的简单,但所有我能找到在谷歌的信息表明,这将不会的工作。

I would really like to use openssl_sign in PHP, because of its simplicity, but all the information I can find on Google indicates that this will not work.

有的一些外部库,声称能做好这一点,但因为这是一个爱好的项目,我宁可不要购买这样的库。

There are some external libraries that claim to do this well, however as this is a hobby project I would rather not purchase such a library.

那么,什么是这里的选择吗? C#和PHP之间完全的互操作性是必需的。 。除了OpenSSL库可用于

So what are the alternatives here? Full interoperability between C# and PHP is required. Libraries besides OpenSSL can be used.

推荐答案

我已经做了一些非常相似的使用的充气城堡加密的API 。看来PHP openssl_sign使用SHA1默认。如果你正在使用的不是缺省任何你需要改变GetSigner算法参数。

I've done something very similar using Bouncy Castle Crypto APIs. It appears PHP openssl_sign uses SHA1 by default. If you are using anything other than the default you'll need to change the algorithm parameter for 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!");
}

这篇关于使用私钥在PHP中数字签名,在C#中验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆