【发布时间】:2013-08-12 03:45:43
【问题描述】:
我有以下代码:
public byte[] GenerateSignature(List<string> text_list)
{
StringBuilder text_string = new StringBuilder();
string private_key = "<RSAKeyValue><Modulus>zDYX4tbHSy....";
byte[] digital_signature = null;
for (int i = 0; i < text_list.Count; i++)
{
text_string.Append(text_list[i]);
}
try
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(private_key);
RSAPKCS1SignatureFormatter rsa_form = new RSAPKCS1SignatureFormatter(rsa);
rsa_form.SetHashAlgorithm("SHA1");
SHA1Managed sha1 = new SHA1Managed();
UnicodeEncoding encoding = new UnicodeEncoding();
byte[] data = encoding.GetBytes(text_string.ToString());
byte[] hash = sha1.ComputeHash(data);
digital_signature = rsa_form.CreateSignature(hash);
}
catch (Exception)
{
digital_signature = Encoding.Unicode.GetBytes("false");
}
return digital_signature;
}
现在,如果我更改私钥中的字符,应用程序会崩溃并在 rsa.FromXmlString(private_key) 行上关闭,即使代码包含在 try catch 块中。另一方面,如果我将私钥设置为无意义的值,例如blablabla,则异常会被捕获并优雅地处理。
为什么当我从私钥更改单个字符时应用程序崩溃并关闭?例如,如果我将“<RSAKeyValue><Modulus>zDYX4tbHSy....”更改为“<RSAKeyValue><Modulus>ADYX4tbHSy....”,(将 z 更改为 A)应用程序崩溃并关闭。该应用程序是一个 Windows Phone 应用程序,但我想这应该没什么区别。
更新
这是抛出异常之前的调用堆栈:
MobileApp.dll!MobileApp.Classes.SignatureMobile.GenerateSignature(System.Collections.Generic.List text_list) 第 38 行 C# MobileApp.dll!MobileApp.StartPage.Button_LogIn_Click(object sender, System.Windows.RoutedEventArgs e) Line 74 + 0x4 bytes C# System.Windows.dll!System.Windows.Controls.Primitives.ButtonBase.OnClick() + 0x1f 字节 System.Windows.dll!System.Windows.Controls.Button.OnClick() + 0x1f 字节
System.Windows.dll!System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(System.Windows.Input.MouseButtonEventArgs e) + 0x4e 字节
System.Windows.dll!System.Windows.Controls.Control.OnMouseLeftButtonUp(System.Windows.Controls.Control ctrl, System.EventArgs e) + 0xc 字节
System.Windows.dll!MS.Internal.JoltHelper.FireEvent(System.IntPtr unmanagedObj, System.IntPtr unmanagedObjArgs, int argsTypeIndex, int actualArgsTypeIndex, string eventName) + 0x115 字节 [外部代码]
【问题讨论】:
-
大概你有一个崩溃的堆栈跟踪 - 它是什么?
-
异常类型是什么?
-
@SriramSakthivel 我不知道。事实上,Visual Studio 不会报告异常。应用程序只是在模拟器中关闭,仅此而已。
-
相关性不大但很有用,注册到 AppDomain 上的 UnhandledException,这样如果从应用程序中抛出,您可以捕获异常。
-
你检查过
text_list是否为空吗?
标签: c# encryption key private