使用c#for compact framework中的RSA算法对消息进行哈希和签名

使用c#for compact framework中的RSA算法对消息进行哈希和签名,c#,compact-framework,C#,Compact Framework,大家好 我尝试用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); st

大家好

我尝试用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”和“DecodeSPrivateKey”,这里使用的是相同的函数:

对于散列的结果,我获得:Hash=“I4t474liO6H+p8SeDsoIkxmi8b0=”

如果我使用openSSL命令执行相同的操作:$echo“salltout le monde”| openSSL dgst-sha1 | openSSL enc-base64

我得到这样的结果:“NjFlOWI4Y2NjN2U2MzFkNTQwNTRmZjE1ZGUyYzk2MDczYTM2ZjRjZAo=”

与我的签名结果相同,使用以下命令:

$echo“向世界致敬”| openssl dgst-sha1-签名PrivateKey.pem | openssl enc-base64

我有这样一个签名:“V6c6XNK7O8+Ikkugtgendzwowxhqliefh7xDoxzmap1glu8b5uxxi0lr6jhvdw6si8p8ptlt+fxEoafY+zgicq4xw6e32f6hxyweyi7silh44i7lf7jyr1lfegno0cw+yqlprrzubdcrvmo1jugh3suk+iot2ait9s=”

通过我的代码,我获得以下信息:

结果=“Ylbnpfdp5ejdyopoui/1W6i+ClrKrxbIdA24IqllGDRWGBrWLtGwlfNkh4+B+gzcrx8hi7pknp1pq2ud1je4ehfujbkzjwxj/ZYI3fPF41oiIMdF63lZiR/sehq5rmtbdsfqkbmftr8udcjnnsimrq458nzvisyouw6j4=”


我找不到我的错误:(有人能帮我吗?

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

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


/p将抑制回车

我尝试了你的方法,但我的代码没有得到相同的值,可能是因为我使用c#?的代码中有错误,现在在OpenSSl中使用“echo”salt tout le monde\n“| OpenSSl dgst-sha1”我得到了这个:d12bb896839290faf65f279e3fe179b339eeecca我也尝试过echo“salt tout le monde\n”|openssl dgst-sha1-binary | openssl enc-base64但是我在c中的代码没有得到相同的结果#