Skip to content

Instantly share code, notes, and snippets.

@DevJohnC
Created September 26, 2014 22:26
Show Gist options
  • Select an option

  • Save DevJohnC/8bc69cb5c708d726d9e7 to your computer and use it in GitHub Desktop.

Select an option

Save DevJohnC/8bc69cb5c708d726d9e7 to your computer and use it in GitHub Desktop.
//
// Author: John Carruthers (johnc@frag-labs.com)
//
// Copyright (C) 2014 John Carruthers
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.IO;
using System.Security.Cryptography;
using AdjutantFramework.Modules.Domains;
namespace AdjutantFramework.Network.Codecs
{
public class SymmetricEncryptionCodec<T> : DomainShareObject, IMessageCodec where T : SymmetricAlgorithm, new()
{
/// <summary>
/// Creates a SymmetricEncryptionCodec instance by derivingan IV and key from a shared secret.
/// </summary>
/// <param name="secret"></param>
/// <param name="salt"></param>
/// <param name="iterations"></param>
/// <returns></returns>
public static SymmetricEncryptionCodec<T> DeriveFromSecret(byte[] secret, byte[] salt, int iterations)
{
var pdb = new Rfc2898DeriveBytes(secret, salt, iterations);
var cryptoAlgo = new T();
var ivSize = cryptoAlgo.BlockSize/8;
var iv = pdb.GetBytes(ivSize);
var keySize = cryptoAlgo.LegalKeySizes[0].MaxSize;
var key = pdb.GetBytes(keySize);
cryptoAlgo.IV = iv;
cryptoAlgo.Key = key;
return new SymmetricEncryptionCodec<T>(cryptoAlgo);
}
public SymmetricEncryptionCodec(T cryptoAlgoInstance)
{
Algorithm = cryptoAlgoInstance;
}
public byte[] Encode(byte[] messageData)
{
using (var memStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memStream, Encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(messageData, 0, messageData.Length);
return memStream.ToArray();
}
}
}
public byte[] Decode(byte[] messageData)
{
using (var memStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memStream, Decryptor, CryptoStreamMode.Read))
{
cryptoStream.Write(messageData, 0, messageData.Length);
return memStream.ToArray();
}
}
}
public T Algorithm { get; private set; }
private ICryptoTransform _decryptor;
private ICryptoTransform Decryptor
{
get
{
if (_decryptor == null || !_decryptor.CanReuseTransform)
_decryptor = Algorithm.CreateDecryptor();
return _decryptor;
}
}
private ICryptoTransform _encryptor;
private ICryptoTransform Encryptor
{
get
{
if (_encryptor == null || !_encryptor.CanReuseTransform)
_encryptor = Algorithm.CreateEncryptor();
return _encryptor;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment