在 C# 中使用 RSA 算法对消息进行哈希和签名以实现紧凑框架

发布于 2024-10-05 19:20:15 字数 2033 浏览 0 评论 0原文

大家好,

我尝试使用 RSA 算法签署一条消息,我正在努力压缩框架,这是我的代码:

private void Signer_Click(object sender, EventArgs e)
{
    ///cle de string ver byte[]
    string clePriveeFile = @"\Program Files\PrivateKey.pem";

    StreamReader reader = new StreamReader(clePriveeFile);
    string contenuKeyPrivate = reader.ReadToEnd();
    byte[] keyByte = Certification.DecodeOpenSSLPrivateKey(contenuKeyPrivate);

    string Data = "Salut tout le monde";

    byte[] DataBytes = System.Text.Encoding.UTF8.GetBytes(Data);//.UTF7.GetBytes(Data);

    //Hasher le message
    SHA1Managed sha1 = new SHA1Managed();
    byte[] hash = sha1.ComputeHash(DataBytes);

    string hach = Convert.ToBase64String(hash);



    RSACryptoServiceProvider Rsa = Certification.DecodeRSAPrivateKey(keyByte);

    byte[] sign = Rsa.SignHash(hash, CryptoConfig.MapNameToOID("SHA1"));

    string resultat = Convert.ToBase64String(sign);

    this.label1.Text = this.resultat;

}

使用“DecodeOpenSSLPrivateKey”和“DecodeRSAPrivateKey”,它与此处使用的函数相同:http://www.jensign.com/opensslkey/index.html

我获得的哈希结果: hash = "I4t474liO6H+ p8SeDsoIkxmi8b0="

如果我​​使用 openSSL 命令做同样的事情: $ echo "Salut tout le monde"| openssl dgst -sha1 | openssl enc -base64

我得到这个: "NjFlOWI4Y2NjN2U2MzFkNTQwNTRmZjE1ZGUyYzk2MDczYTM2ZjRjZAo="

与我的符号结果相同,使用以下命令:

$ echo "Salut tout le monde"| openssl dgst -sha1 -sign PrivateKey.pem | openssl dgst -sha1 -sign PrivateKey.pem | openssl enc -base64

I Have a signature like this: "V6c6XNK7O8+IKKuGTGENdZwoWxhQLIEFHW7XSdoXzMAp1gLu8B5UxXi0lr6JhVDW6SI8p8PtlT+fxEoafY+ZCigIq4xW6e32f6hxYWeYi7silh44I1m7LF7JYR1lfcegnO0Cw+YPqLPRrZUbdcrvMo1ijUGH3SuK+iot2lait9s="

with My Code I obtain this:

result = "YlbnppFDP5ejdYOPoUI/1W6i+ClrKrxbIdA24IqllGDRWGBrWLtGwlfNkh4+B+gzcrX8HI7PkNP1Pq2ud1jE4EhfUVjBkZJwXj/ZYI3fPF41oiIMdF63lZiR/SEHQ5rMTbdsfQtkkBmFTr8udCJNnmnSimRQ458nZvIsYOUW6j4="

我找不到我的错误:(有人可以帮助我吗?

Hy everybody,

I try to sign one message with RSA algorithm, i'm working to compact Framwork this is my Code:

private void Signer_Click(object sender, EventArgs e)
{
    ///cle de string ver byte[]
    string clePriveeFile = @"\Program Files\PrivateKey.pem";

    StreamReader reader = new StreamReader(clePriveeFile);
    string contenuKeyPrivate = reader.ReadToEnd();
    byte[] keyByte = Certification.DecodeOpenSSLPrivateKey(contenuKeyPrivate);

    string Data = "Salut tout le monde";

    byte[] DataBytes = System.Text.Encoding.UTF8.GetBytes(Data);//.UTF7.GetBytes(Data);

    //Hasher le message
    SHA1Managed sha1 = new SHA1Managed();
    byte[] hash = sha1.ComputeHash(DataBytes);

    string hach = Convert.ToBase64String(hash);



    RSACryptoServiceProvider Rsa = Certification.DecodeRSAPrivateKey(keyByte);

    byte[] sign = Rsa.SignHash(hash, CryptoConfig.MapNameToOID("SHA1"));

    string resultat = Convert.ToBase64String(sign);

    this.label1.Text = this.resultat;

}

with 'DecodeOpenSSLPrivateKey' and 'DecodeRSAPrivateKey' it's the sames functions use here: http://www.jensign.com/opensslkey/index.html

for the result of my Hash i obtain: hash = "I4t474liO6H+p8SeDsoIkxmi8b0="

if i do same things with openSSL command: $ echo "Salut tout le monde"| openssl dgst -sha1 | openssl enc -base64

I obtain this: "NjFlOWI4Y2NjN2U2MzFkNTQwNTRmZjE1ZGUyYzk2MDczYTM2ZjRjZAo="

same with my sign result, with this command:

$ echo "Salut tout le monde"| openssl dgst -sha1 -sign PrivateKey.pem | openssl enc -base64

I Have a signature like this: "V6c6XNK7O8+IKKuGTGENdZwoWxhQLIEFHW7XSdoXzMAp1gLu8B5UxXi0lr6JhVDW6SI8p8PtlT+fxEoafY+ZCigIq4xW6e32f6hxYWeYi7silh44I1m7LF7JYR1lfcegnO0Cw+YPqLPRrZUbdcrvMo1ijUGH3SuK+iot2lait9s="

with My Code I obtain this:

result = "YlbnppFDP5ejdYOPoUI/1W6i+ClrKrxbIdA24IqllGDRWGBrWLtGwlfNkh4+B+gzcrX8HI7PkNP1Pq2ud1jE4EhfUVjBkZJwXj/ZYI3fPF41oiIMdF63lZiR/SEHQ5rMTbdsfQtkkBmFTr8udCJNnmnSimRQ458nZvIsYOUW6j4="

I can't to find my error :( can someone help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云之铃。 2024-10-12 19:20:15

一个问题是 echo 输出包含换行符后缀。尝试散列“Salut tout le monde\n”。
另一个是OpenSSL以十六进制编码格式输出SHA1摘要,因此将结果编码为Base64是没有意义的。

One problem is that the echo output contains a newline suffix. Try hashing "Salut tout le monde\n".
Another is that OpenSSL outputs the SHA1 digest in hex-encoded format, so encoding the result in Base64 is meaningless.

德意的啸 2024-10-12 19:20:15

或者在 openssl 命令行中,使用 echo|set /p=Salut tout le monde | openssl

/p 将抑制回车

or in openssl command line, use echo|set /p=Salut tout le monde | openssl

/p will suppress the carriage return

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文