Skip to content

Instantly share code, notes, and snippets.

@marcbarry
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcbarry/96470e5424bfa2ffd193 to your computer and use it in GitHub Desktop.
Save marcbarry/96470e5424bfa2ffd193 to your computer and use it in GitHub Desktop.
Simple (non-AEAD) c# helper console app
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace SimpleAES
{
class Program
{
/// <summary>
///
/// </summary>
const int ReadBufferSize = 512 * 1024;
/// <summary>
/// entrypoint
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("Usage: SimpleAES.exe -encrypt [src] [dst] [key] [iv]");
Console.WriteLine(" -decrypt [src] [dst] [key] [iv]");
Console.WriteLine(" -base64encode [src] [dst]");
Console.WriteLine(" -base64decode [src] [dst]");
Console.WriteLine(" -tohex [value]\n");
Console.WriteLine("Note: [key] argument should be supplied in hex (i.e. FF-FF-FF-FF-FF-FF-FF-FF-FF-FF-FF-FF-FF-FF-FF-FF) and should be either 128 or 256 bits");
Console.WriteLine(" [iv] argument should be supplied in hex (i.e. 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00) and should be 128 bits");
return;
}
try
{
var action = args[0].Substring(1).ToLower();
switch (action)
{
case "encrypt":
case "decrypt":
{
var key = args[3].Split('-').Select(b => Convert.ToByte(b, 16)).ToArray();
var iv = args[4].Split('-').Select(b => Convert.ToByte(b, 16)).ToArray();
if (action == "encrypt")
{
EncryptFile(args[1], args[2], key, iv);
}
else
{
DecryptFile(args[1], args[2], key, iv);
}
break;
}
case "base64encode":
{
Base64Encode(args[1], args[2]);
break;
}
case "base64decode":
{
Base64Decode(args[1], args[2]);
break;
}
case "tohex":
{
Console.WriteLine(BitConverter.ToString(Encoding.ASCII.GetBytes(args[1])));
break;
}
default:
{
Console.WriteLine("Invalid Option");
break;
}
}
}
catch (Exception ex)
{
Console.WriteLine("Exception: {0}", ex.Message);
}
}
/// <summary>
///
/// </summary>
/// <param name="sourceFile"></param>
/// <param name="destinationFile"></param>
/// <param name="key">128 bit, or 256 bit key (16/32 chars)</param>
/// <param name="iv">128 bit derived initialisation vector (16 chars)</param>
/// <returns></returns>
static void EncryptFile(string sourceFile, string destinationFile, byte[] key, byte[] iv)
{
var sourceFileLength = new FileInfo(sourceFile).Length;
using (var aes = new AesCryptoServiceProvider())
{
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
aes.KeySize = (key.Length * 8);
Console.WriteLine("AesKey : {0}", BitConverter.ToString(key));
Console.WriteLine("AesIV : {0}", BitConverter.ToString(iv));
using (var dstFile = new FileStream(destinationFile, FileMode.Create))
{
using (var encryptor = aes.CreateEncryptor(key, iv))
{
using (var cs = new CryptoStream(dstFile, encryptor, CryptoStreamMode.Write))
{
using (var srcFile = new FileStream(sourceFile, FileMode.Open))
{
Console.WriteLine("AesEncryptStart ({0} / {1} / {2} bit)", aes.Mode, aes.Padding, aes.KeySize);
Console.WriteLine("{0}/{1} bytes", 0, sourceFileLength);
int bytes;
var buffer = new byte[ReadBufferSize];
Int64 totalBytes = 0;
while ((bytes = srcFile.Read(buffer, 0, buffer.Length)) > 0)
{
totalBytes += bytes;
cs.Write(buffer, 0, bytes);
Console.WriteLine("{0}/{1}", totalBytes, sourceFileLength);
}
Console.WriteLine("AesEncryptComplete");
}
}
}
}
}
}
/// <summary>
///
/// </summary>
/// <param name="sourceFile"></param>
/// <param name="destinationFile"></param>
/// <param name="key">128 bit, or 256 bit key (16/32 chars)</param>
/// <param name="iv">128 bit derived initialisation vector (16 chars)</param>
static void DecryptFile(string sourceFile, string destinationFile, byte[] key, byte[] iv)
{
var sourceFileLength = new FileInfo(sourceFile).Length;
using (var aes = new AesCryptoServiceProvider())
{
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
aes.KeySize = (key.Length * 8);
Console.WriteLine("AesKey : {0}", BitConverter.ToString(key));
Console.WriteLine("AesIV : {0}", BitConverter.ToString(iv));
using (var srcFile = new FileStream(sourceFile, FileMode.Open))
{
using (var dstFile = new FileStream(destinationFile, FileMode.Create))
{
using (var decryptor = aes.CreateDecryptor(key, iv))
{
using (var cs = new CryptoStream(srcFile, decryptor, CryptoStreamMode.Read))
{
Console.WriteLine("AesDecryptStart ({0} / {1} / {2} bit)", aes.Mode, aes.Padding, aes.KeySize);
Console.WriteLine("{0}/{1} bytes", 0, sourceFileLength);
int bytes;
var buffer = new byte[4096];
Int64 totalBytes = 0;
while ((bytes = cs.Read(buffer, 0, buffer.Length)) > 0)
{
totalBytes += bytes;
dstFile.Write(buffer, 0, bytes);
Console.WriteLine("{0}/{1}", totalBytes, sourceFileLength);
}
Console.WriteLine("AesDecryptComplete");
}
}
}
}
}
}
/// <summary>
///
/// </summary>
/// <param name="sourceFile"></param>
/// <param name="destinationFile"></param>
static void Base64Encode(string sourceFile, string destinationFile)
{
var dataIn = new byte[new FileInfo(sourceFile).Length];
using (var srcFile = new FileStream(sourceFile, FileMode.Open))
{
using (var dstFile = new FileStream(destinationFile, FileMode.Create))
{
srcFile.Read(dataIn, 0, dataIn.Length);
var dataOut = Convert.ToBase64String(dataIn, 0, dataIn.Length);
dstFile.Write(Encoding.UTF8.GetBytes(dataOut), 0, dataOut.Length);
}
}
}
/// <summary>
///
/// </summary>
/// <param name="sourceFile"></param>
/// <param name="destinationFile"></param>
static void Base64Decode(string sourceFile, string destinationFile)
{
var dataIn = new byte[new FileInfo(sourceFile).Length];
using (var srcFile = new FileStream(sourceFile, FileMode.Open))
{
using (var dstFile = new FileStream(destinationFile, FileMode.Create))
{
srcFile.Read(dataIn, 0, dataIn.Length);
var dataOut = Convert.FromBase64String(Encoding.UTF8.GetString(dataIn));
dstFile.Write(dataOut, 0, dataOut.Length);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment