当我将x509证书发送到encypt和decypt消息时,我收到了一些错误信息,无法解决此问题.有人会发生什么事来解决这个错误吗?谢谢.
描述:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.
例外细节:
System.Security.Cryptography.CryptographicException:keyset不存在.
来源错误:
第53行:使用(RSACryptoServiceProvider rsaProviderDecrypt =(RSACryptoServiceProvider)cerDecrypt.PublicKey.Key)第54行:
{第55行:plainHashBytes = rsaProviderDecrypt.Decrypt(encryptedHashBytes,false); 第56行:
rsaProviderDecrypt.Clear(); 第57行:
rsaProviderDecrypt.Dispose();源文件:E:\ PayUSite\PayMvcApp\Controllers\HashMessageController.cs行:55
堆栈跟踪:
[CryptographicException:keyset不存在.]
System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)+41
System.Security.Cryptography.RSACryptoServiceProvider.DecryptKey(SafeKeyHandle pKeyContext,Byte [] pbEncryptedKey,Int32 cbEncryptedKey,Boolean fOAEP,ObjectHandleOnStack ohRetDecryptedKey)+0
System.Security.Cryptography .RSACryptoServiceProvider.Decrypt(Byte [] rgb,Boolean fOAEP)+579
源代码:
string docFile = Server.MapPath("~/docx/DirectAccess_StepByStep.doc");
HashAlgorithm hash = HashAlgorithm.Create("SHA1");
byte[] hashedBytes;
using (FileStream fs = new FileStream(docFile, FileMode.Open))
{
//compute message hash value
hashedBytes = hash.ComputeHash(fs);
hash.Dispose();
fs.Close();
}
string hashedString = Convert.ToBase64String(hashedBytes);
//encrypt message digest
string priKeyFile = Server.MapPath("~/certificate/WosMiddle.pfx");
X509Certificate2 certEncrypt = new X509Certificate2(priKeyFile, "123456");
byte[] …Run Code Online (Sandbox Code Playgroud) 存在这样的情况:应用系统需要每1分钟向在线用户发布消息,并且代码使用多头任务来读取消息列表.但程序运行不正常,会抛出一个超出范围异常的索引.请有人可以提出任何建议,谢谢.
private Timer taskTimer;
private static readonly object _locker = new object();
private static IList<Message> _messages = null;
private void OnTimerElapsed(object sender)
{
var msgModel = new MessageModel();
_messages = msgModel.GetMessageList();
var msgCount = _messages.Count();
Task[] _tasks = new Task[msgCount];
for (int i = 0; i < msgCount; i++)
{
if (i < msgCount)
{
_tasks[i] = Task.Factory.StartNew(() =>
{
lock (_locker)
{
PushMessage(i);
}
});
}
}
//waiting all task finished
while (_tasks.Any(t => !t.IsCompleted)) { }
}
private …Run Code Online (Sandbox Code Playgroud) 看似很简单,却发生了这个错误。
IList<string> list = new List<string>();
list.Add("001");
list.Add("002");
list.Add("003");
list.Add("004");
list.ToList().RemoveAll(s => s == "002");
return list.Count.ToString();
Run Code Online (Sandbox Code Playgroud)
列表 Count 应该是 3,但它仍然是 4。是 RemoveAll() 方法中的错误吗?如果使用 List 而不是 IList 声明,则效果很好。
编辑 1. 如果不使用 ToList() 方法,则没有可调用的 RemoveAll() 方法。
如何避免这种情况使用 IList 作为参数,首先 list 是一个引用类型。我不应该完全使用 IList 作为声明吗?我们在项目中到处都使用了 IList。
public string List()
{
IList<string> list = new List<string>();
list.Add("001");
list.Add("002");
list.Add("003");
list.Add("004");
Remove(list);
return list.Count.ToString();
}
private void Remove(IList<string> list)
{
list.ToList().RemoveAll(a => a == "002");
}
Run Code Online (Sandbox Code Playgroud)