在使用RSA签名文档时应用SHA-256 / SHA-512而不是SHA-1时,程序崩溃 [英] Program crashing when Applying SHA-256/SHA-512 instead of SHA-1 when signing documents with RSA

查看:574
本文介绍了在使用RSA签名文档时应用SHA-256 / SHA-512而不是SHA-1时,程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我在txt_EmpSignature文本框中生成一个签名和地方,然后在Sign按钮单击事件处理程序中使用以下参数签名文档:

First I generate a Signature and place in the txt_EmpSignature text box and then to sign documents i'm using the following argument in the "Sign" button click event handler:

RSACryptoServiceProvider MySigner = new RSACryptoServiceProvider();
openFileToSign.ShowDialog();
FileStream file_to_sign = new FileStream(openFileToSign.FileName, FileMode.Open, FileAccess.Read);
BinaryReader reader_to_sign = new BinaryReader(file_to_sign);

byte[] data_to_sign = reader_to_sign.ReadBytes((int)file_to_sign.Length);
MySigner.FromXmlString(txt_EmpSignature.Text);
SHA1CryptoServiceProvider SHA = new SHA1CryptoServiceProvider();
byte[] signature_to_sign = MySigner.SignData(data_to_sign,SHA);
UTF8Encoding UTF = new UTF8Encoding();
txt_DocSignature.Text = Convert.ToBase64String(signature_to_sign);
reader_to_sign.Close();
file_to_sign.Close();  

然后当我用SHA256CryptoServiceProvider替换SHA1CryptoServiceProvider,我得到一个未处理的异常, t指定!

处理SHA-2 algorthims而不是SHA-1时有什么区别吗?

Then when I replace the "SHA1CryptoServiceProvider" with "SHA256CryptoServiceProvider" I get an unhandled exception that i couldn't specify!
Is there any difference when handling SHA-2 algorthims instead of SHA-1?

指定档案时,打开在openFileToSign打开文件对话框。我正在处理WINDOWS 7

The Exception happens when I specify the file and click "Open" in the "openFileToSign" Open File Dialogue. And I'm working on "WINDOWS 7"

EDIT2:

异常消息说值无效

,并且异常在行中出现:

The Exception message says "Value was invalid"
and the exception happenes in the line:

byte[] signature_to_sign = MySigner.SignData(data_to_sign,SHA);


推荐答案

回答我自己的问题,本文章

结果证明 CryptoConfig 不能理解 SHA256CryptoServiceProvider

相反,我们使用 SHA256Managed 来生成哈希,然后一切都会很好地工作。

Answering my own question as i found the answer finally in this article.
It turned out that SHA256CryptoServiceProvider is not understood by the CryptoConfig.
Instead, we use SHA256Managed to generate the hash and then everything will work beautifully.

这篇关于在使用RSA签名文档时应用SHA-256 / SHA-512而不是SHA-1时,程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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