Please tell me if I'm doing this right, and what else is needed to make this hardened.
It seems like it's too simple, like there's almost nothing I'm actually doing. For example, there are 4 or 5 stacked using statements!
Here is the code to review:
class Program
{
static void Main(string[] args)
{
var enc = true;
var fileIn = string.Empty;
var fileOut = string.Empty;
/* parse command line to populate enc, fileIn, fileOut */
Console.Write("key: ");
var key = Encoding.UTF8.GetBytes(Console.ReadLine());
Console.Write("IV: ");
var iv = Encoding.UTF8.GetBytes(Console.ReadLine());
using (var aes = AesManaged.Create())
using (var md5 = MD5.Create())
using (var readStream = File.OpenRead(fileIn))
using (var writeStream = File.OpenWrite(fileOut))
using (var cryptoStream = new CryptoStream(
enc ? writeStream : readStream,
enc
? aes.CreateEncryptor(md5.ComputeHash(key), md5.ComputeHash(iv))
: aes.CreateDecryptor(md5.ComputeHash(key), md5.ComputeHash(iv)),
enc ? CryptoStreamMode.Write : CryptoStreamMode.Read))
{
if (enc) { readStream.CopyToAsync(cryptoStream).Wait(); }
else { cryptoStream.CopyToAsync(writeStream).Wait(); }
}
}
}
CreateEncryptor()
andCreateDecryptor
both return anIDisposable
object which should be wrapped in ausing
clause before it's passed to theCryptoStream
. \$\endgroup\$/* parse command line to populate enc, fileIn, fileOut */
commented-out part. \$\endgroup\$