System.Security.Cryptography.CryptographicException:該項不適於在指定狀態下使用 -- 解決方案


代碼

WindowsIdentity wi = Logon(UserName, Password);
if (null != wi) {
                                  
                                    WindowsImpersonationContext wic = wi.Impersonate();
                                    CspParameters cp = new CspParameters();

                                    cp.ProviderType = 1;
                                    
                                    cp.Flags = CspProviderFlags.UseMachineKeyStore;
                                    
                                    cp.KeyNumber = (int)KeyNumber.Exchange;
                                    
                                    cp.KeyContainerName = "KEY_1";
                                    
                                  RSACryptoServiceProvider csp = new RSACryptoServiceProvider( cp);
                                  

                                    this.txtPublicKey.Text = this.PublicKey = csp.ToXmlString(false);
                                  
                                    csp.Clear();
                                    wic.Undo();

 症狀:是我用了加密算法后,把應用掛在IIS上,就時不時報如上錯。

解決:

  while impersonated and dont clean up the keys of the RSACryptoProvider using the clear method while impersonated.
      解決步驟:

      Impersonate user
      Create a new RSACryptoServiceProvider (pair)
      Encrypt or Decrypt your data
      Call clear to release the keys (pair.Clear())
      Undo impersonation
  2.  當 RSACryptoProvider 創建了密鑰文件但由於權限不足而無法刪除該密鑰文件時,會導致此錯誤;

    解決:

      若要更正此錯誤,請確認代理服務帳戶對“Documents and Settings”文件夾中 RSA 計算機密鑰文件夾(例如,C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA)的“完全控制”權限設置為“允許”。

我以為RSA加密解密問題就此終結,結果。。。。

  異常:

  System.Security.Cryptography.CryptographicException:該項不適於在指定狀態下使用。

這個問題這個問題為難死我了!

  這個問題是客戶機子上出現的,而我身邊所有的機子都沒有過。

  從何下手!客戶那里不可以遠程訪問,就別提調試了。

  問問他們的環境有無變化:?

      ------重裝過產品

  我想問題就應該出在產品身上,因為我加密的時候,模擬用戶是我們公司產品建立的一個帳戶。

  我又找啊找啊,終於找到了原因!

  The most common issue in this area occurs when a local (non-domain) user's password is administratively reset. On WinXP or 2K3, this causes all data protected by DPAPI (including user private keys) to be lost; at least until the password is set back. This is by design, and in fact is an important
security feature

        解決:

        方案一:將模擬帳戶的密碼恢復到以前的那個密碼;

        方案二:C:\Documents and Settings\%userName%\Application Data\Microsoft\的Crypto目錄給刪掉


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM