This is the literal answer to your question, because it will not issue CA warnings without suppressing them, and will only ever call Dispose once:
MemoryStream encryptedStream = null; CryptoStream cryptStream = null; try { encryptedStream = new MemoryStream(); cryptStream = new CryptoStream(encryptedStream, cryptoTransform, CryptoStreamMode.Write); cryptStream.Write(inputInBytes, 0, inputInBytes.Length); cryptStream.FlushFinalBlock(); result = encryptedStream.ToArray(); } finally { if (cryptStream != null) { cryptStream.Dispose(); } else { if (encryptedStream != null) encryptedStream.Dispose(); } } string output = Convert.ToBase64String(result);
But any developer who deserves their salt should take a look at this and go, "hmm, he doesn't seem to know using , I better rewrite this." Do not do this in production code. Suppress warning. Getting code like this correct (and if it remains correct in the face of change) is actually more difficult than writing code that uses using to suppress false warnings (indeed, I'm not quite sure that the code above is correct!). This primarily strikes the whole static code analysis: write reliable code. You should see code analysis as a tool, not an arbiter of correctness.
source share