zoukankan      html  css  js  c++  java
  • 多线程下System.Security.Cryptography.Aes CreateDecryptor报“Safe handle has been closed”的解决方案

    因为系统需要对一些核心数据进行预加载以保证查询速度。

    所以在application_start 事件中启用了后台线程对相关的数据进行加载并解密(为了保证解密的效率,将AES对像做了静态对像来保存:private static readonly Aes aesAlg = Aes.Create();)

    但是在有些情况下报“Safe handle has been closed”异常导致加载失败。

    具体的堆栈信息:

    System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success)
    at System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle, Boolean& success)
    at System.Security.Cryptography.CapiNative.UnsafeNativeMethods.CryptDuplicateKey(SafeCapiKeyHandle hKey, IntPtr pdwReserved, Int32 dwFlags, SafeCapiKeyHandle& phKey)
    at Microsoft.Win32.SafeHandles.SafeCapiKeyHandle.Duplicate()
    at System.Security.Cryptography.CapiSymmetricAlgorithm.SetupKey(SafeCapiKeyHandle key, Byte[] iv, CipherMode cipherMode, Int32 feedbackSize)
    at System.Security.Cryptography.AesCryptoServiceProvider.CreateDecryptor(SafeCapiKeyHandle key, Byte[] iv)

    经查阅到一篇资料得知:

    在通常情况下可以重用这个对像,但是最好不要在多线程中使用。

    所以,解决方法:

    要么将AES对像每次实例化(但效率会降低),

    要么就用其它的来代替,比如(RijndaelManaged,正在测试,目前没有发现此问题了)。

  • 相关阅读:
    PyCharm小技巧
    How to install torcs package in Debian
    QT4.8.6静态编译
    Debian初识(选择最佳镜像发布站点加入source.list文件)
    Dev-C++ 小问题锦集
    ubuntu 12.04lts 安装mysql ,并通过QT连接
    win7下安装ubuntu14.04lts 双系统
    cmake打印变量值
    驾车常识:小轿车灯光
    汽车点火开关的功能介绍
  • 原文地址:https://www.cnblogs.com/dengxi/p/7150824.html
Copyright © 2011-2022 走看看