# Getting a decryptor object

I need to lock on my AesManaged instance _Aes to call CreateDecryptor() however the CreateDecryptor() method is not thread safe and this function could be called by multiple threads. Is this the correct way to create a disposable object while needing to use a lock on the parent creating the object?

byte[] encryptedTextBlob = GetEncryptedTextBlob();

ICryptoTransform decryptor = null;
lock(_Aes)
{
decryptor = _Aes.CreateDecryptor();
}

using (decryptor)
using (var encryptedStream = new MemoryStream(encryptedTextBlob))
using (var decrypedStream = new CryptoStream(encryptedStream, decryptor, CryptoStreamMode.Read))
{
}

-

I would say you're not using AES correctly - don't hold on to an instance of it in your class (guessing by the underscore in the name) but rather create it as needed, while holding AES's parameters (the key and IV) in your class:

using (var aes = new AesManaged() { IV = _IV, Key = _Key })
using (var decryptor = aes.CreateDecryptor())
using (var encryptedStream = new MemoryStream(encryptedTextBlob))
using (var decrypedStream = new CryptoStream(encryptedStream, decryptor, CryptoStreamMode.Read))
{
}

-
I am getting the data from a 3rd party source that has a fixed IV and Key, that is why I used only the one copy of AesManaged, I thought creating all those copies of AesManaged all to be identical would be expensive. –  Scott Chamberlain Jan 25 '13 at 21:11
Understood, I will use that way. I have asked a new question without all of that Aes stuff in the way as I still would like to know the answer to my original question in case I need to use it in another situation. –  Scott Chamberlain Jan 25 '13 at 22:02

Jesse C. Slicer's answer addresses your main concern, but after seeing those multiple using blocks, you might be interested to know that you can create multiple variables in a single using statement. I think this is a bit easier to read, personally:

using (var aes = new AesManaged() { IV = _IV, Key = _Key },
var decryptor = aes.CreateDecryptor(),
var encryptedStream = new MemoryStream(encryptedTextBlob),
var decrypedStream = new CryptoStream(encryptedStream, decryptor, CryptoStreamMode.Read),

Interesting, I didn't know you could do this. But personally, I think multiple usings are better, because that way, changing the code is easier. –  svick Jan 25 '13 at 19:16
@svick I see your point, but I think it would be too easy to accidentally an inject somewhere in the using cascades -- still totally valid c#, but altering the meaning of the program: –  Reacher Gilt Jan 25 '13 at 22:18