2016-07-02 10 views
-1

はCA2000と苦情がありません:スコープ(https://msdn.microsoft.com/library/ms182289.aspx)を失う前にオブジェクトを配置する。これのコードでは、次の警告.NETコード解析警告

警告CA2000を生成メソッド 'CryptoComputer.Encrypt(string、string、string、string)'、オブジェクト 'new RijndaelManaged()'はすべての例外パスに沿って配置されません。 System.IDisposable.Disposeを呼び出す前にオブジェクト 'new RijndaelManaged()'をスコープから外してください。

public static string Encrypt(string plainText, string passPhrase, string saltValue, string initVector) 
      { 
       var initVectorBytes = Encoding.UTF8.GetBytes(initVector); 
       var saltValueBytes = Encoding.UTF8.GetBytes(saltValue); 
       var plainTextBytes = Encoding.UTF8.GetBytes(plainText); 
       string cipherText; 
       PasswordDeriveBytes password = null; 
       RijndaelManaged symmetricKey = null; 
       MemoryStream memoryStream = null; 
       try 
       { 
        memoryStream = new MemoryStream(); 

        password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations); 
        byte[] keyBytes = password.GetBytes(keySize/8); 

        symmetricKey = new RijndaelManaged { Mode = CipherMode.CBC }; 
        var encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes); 

        var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write); 

        cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); 

        cryptoStream.FlushFinalBlock(); 

        var cipherTextBytes = memoryStream.ToArray(); 


        cipherText = Convert.ToBase64String(cipherTextBytes); 

       } 
       catch (Exception) 
       { 

        throw; 
       } 
       finally 
       { 
        password?.Dispose(); 
        symmetricKey?.Dispose(); 
        memoryStream?.Dispose(); 

       } 

       return cipherText; 
      } 

どのように私は

+0

使用 "を使用して(...)"、symmetricKey、MemoryStreamを。 –

+1

Microsoftの分析ルールで「password?Dispose()」という構文が認識されないためですか? 'if(obj!= null)obj.Dispose();'として書き直すとどうなりますか? – kennyzx

+0

Kennyzxはおそらく正しいでしょう...古い方法でコードを書いたり、偽の警告を抑えなければならないでしょう –

答えて

0

使い捨てインスタンスは、それがスコープの外に出た後に処分されることを確認しますこれは、以下のようなUsing { ... }ブロックを使用して、それを包む、Microsoftのガイドラインを満たすために、このコードを書き換えることができます。以下の図だけでなく、using {}ブロックにすべての使い捨てオブジェクトをラップするようにしてください。

using (symmetricKey = new RijndaelManaged { Mode = CipherMode.CBC }) 
{ 
    //rest code goes here 
} 
+0

いいえ、それはあなたのパスを示していない@BinsonEldhose –

+0

@BinsonEldhose。私が言及したように、答えで述べたものだけでなく、すべての使い捨てオブジェクトを 'using {}'ブロックで囲む必要があります。 – Rahul

1

"(...)を使用して" 内のすべての使い捨てのオブジェクト:パスワードの

... 
var initVectorBytes = Encoding.UTF8.GetBytes(initVector); 
var saltValueBytes = Encoding.UTF8.GetBytes(saltValue); 
var plainTextBytes = Encoding.UTF8.GetBytes(plainText); 
string cipherText; 
using (var memoryStream = new MemoryStream()) 
{ 
    using (var password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations)) 
    { 
    var keyBytes = password.GetBytes(keySize/8); 
    using (var symmetricKey = new RijndaelManaged {Mode = CipherMode.CBC}) 
    { 
     var encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes); 
     var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write); 
     cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); 
     cryptoStream.FlushFinalBlock(); 
     var cipherTextBytes = memoryStream.ToArray(); 
     cipherText = Convert.ToBase64String(cipherTextBytes); 
    } 
    } 
} 
return cipherText; 
... 

using Statement (C# Reference)

関連する問題