Program crashing when Applying SHA-256/SHA-512 instead of SHA-1 when signing documents with RSA

僤鯓⒐⒋嵵緔 提交于 2019-12-13 08:36:09

问题


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();  

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?

EDIT1:

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);

回答1:


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.




回答2:


Hard to say if you do not say the exact exception. But note that SHA-2 was not supported in older versions of Windows. It has been added to Windows XP in SP3, and in Windows 2003 Server in a hotfix or something. See this post.




回答3:


My guess is that your are trying to use a 512-bit key. 512-bit keys are too short to use with SHA-256 (there is not enough room for the minimum padding).

Try using a 1024- or 2048-bit key instead.



来源:https://stackoverflow.com/questions/5002674/program-crashing-when-applying-sha-256-sha-512-instead-of-sha-1-when-signing-doc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!