Reuse ICryptoTransform objects

前端 未结 1 1080
小蘑菇
小蘑菇 2021-01-19 14:33

I have a class that is used to encrypt textual data. I am trying to reuse the ICryptoTransform objects where possible. However, the second time I am trying to use the same o

1条回答
  •  鱼传尺愫
    2021-01-19 14:51

    What you're missing is the bug (and bugfix) in .NET Framework :).

    There's a Microsoft Connect Issue about this same problem; specifically that AesCryptoServiceProvider.CreateDecryptor() returns an object that says CanReuseTransform=true, but doesn't seem to behave correctly.

    The bug was fixed in the .NET 4.6.2 release, but is guarded behind a retargeting change. That means that in order to see the fix you need to

    1. Install .NET Framework 4.6.2 or higher.
    2. Change the minimum framework version of your main executable to be 4.6.2 or higher.

    If you have the newer framework installed, but want to keep your executable targeting a lower version of the framework you need to set the switch Switch.System.Security.Cryptography.AesCryptoServiceProvider.DontCorrectlyResetDecryptor to false.

    From the AppContext class documentation (under "Remarks"):

    Once you define and document the switch, callers can use it by using the registry, by adding an AppContextSwitchOverrides element to their application configuration file, or by calling the AppContext.SetSwitch(String, Boolean) method programmatically.

    For the configuration file (your.exe.config):

    
      
        
      
    
    

    0 讨论(0)
提交回复
热议问题