|
using System;
using System.Diagnostics;
using System.Security.Cryptography;
namespace ManyMonkeys.Cryptography
{
/// <summary>
/// Summary description for Twofish encryption algorithm of which more information can be found at http://www.counterpane.com/twofish.html.
/// This is based on the MS cryptographic framework and can therefore be used in place of the RijndaelManaged classes
/// provided by MS in System.Security.Cryptography and the other related classes
/// </summary>
public sealed class Twofish : SymmetricAlgorithm
{
/// <summary>
/// This is the Twofish constructor.
/// </summary>
public Twofish()
{
this.LegalKeySizesValue = new KeySizes[]{new KeySizes(128,256,64)}; // this allows us to have 128,192,256 key sizes
this.LegalBlockSizesValue = new KeySizes[]{new KeySizes(128,128,0)}; // this is in bits - typical of MS - always 16 bytes
this.BlockSize = 128; // set this to 16 bytes we cannot have any other value
this.KeySize = 128; // in bits - this can be changed to 128,192,256
this.Padding = PaddingMode.Zeros;
this.Mode = CipherMode.ECB;
}
/// <summary>
/// Creates an object that supports ICryptoTransform that can be used to encrypt data using the Twofish encryption algorithm.
/// </summary>
/// <param name="key">A byte array that contains a key. The length of this key should be equal to the KeySize property</param>
/// <param name="iv">A byte array that contains an initialization vector. The length of this IV should be equal to the BlockSize property</param>
public override ICryptoTransform CreateEncryptor(byte[] key, byte[] iv)
{
Key = key; // this appears to make a new copy
if (Mode == CipherMode.CBC)
IV = iv;
return new TwofishEncryption(KeySize, ref KeyValue, ref IVValue, ModeValue, TwofishBase.EncryptionDirection.Encrypting);
}
/// <summary>
/// Creates an object that supports ICryptoTransform that can be used to decrypt data using the Twofish encryption algorithm.
/// </summary>
/// <param name="key">A byte array that contains a key. The length of this key should be equal to the KeySize property</param>
/// <param name="iv">A byte array that contains an initialization vector. The length of this IV should be equal to the BlockSize property</param>
public override ICryptoTransform CreateDecryptor(byte[] key, byte[] iv)
{
Key = key;
if (Mode == CipherMode.CBC)
IV = iv;
return new TwofishEncryption(KeySize, ref KeyValue, ref IVValue, ModeValue, TwofishBase.EncryptionDirection.Decrypting);
}
/// <summary>
/// Generates a random initialization Vector (IV).
/// </summary>
public override void GenerateIV()
{
IV = new byte[16]{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
}
/// <summary>
/// Generates a random Key. This is only really useful in testing scenarios.
/// </summary>
public override void GenerateKey()
{
Key = new byte[KeySize/8];
// set the array to all 0 - implement a random key generation mechanism later probably based on PRNG
for (int i=Key.GetLowerBound(0);i<Key.GetUpperBound(0);i++)
{
Key[i]=0;
}
}
/// <summary>
/// Override the Set method on this property so that we only support CBC and EBC
/// </summary>
public override CipherMode Mode
{
set
{
switch (value)
{
case CipherMode.CBC:
break;
case CipherMode.ECB:
break;
default:
throw (new CryptographicException("Specified CipherMode is not supported."));
}
this.ModeValue = value;
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I'm Alphons van der Heijden, living in Lelystad, Netherlands, Europa, Earth. And currently I'm retiring from hard working ( ;- ), owning my own company. Because I'm full of energy, and a little to young to relax ...., I don't sit down, but create and recreate software solutions, that I like. Reinventing the wheel is my second nature. My interest is in the area of Internet technologies, .NET etc. I was there in 1992 when Mosaic came out, and from that point, my life changed dramatically, and so did the world, in fact. (Y)
I've been working with all aspects of software development since 1979 - from compiler construction to management. Currently I'm an independent consultant mostly specializing in computer security. Please see my homepage for contact details.
I speak C like a native, and have a pretty good grasp of C++. The most recent five years C# has been the main development language. Traditionally Unix has been the dominating environment, but currently the scales have tipped over to Windows, due to market demands but I'm equally at home developing in both environments.
When I'm not coding I'm usually sitting on one of my 4 bikes, indoors or outdoors, on the road or in the woods.
I am currently employed as a C# / MS SQL developer for a Medical Software company working on a large enterprise system.
I have used Delphi and MSM MUMPS in previous roles and love both languages.
I'm an avid Windows Mobile fan and moderator on http://xda-developers.com. I code in C#, and C++ for WM as well as cooking custom ROMs and learning to disasm the inners of my device.
I have some intensive experience with C#, ASP.NET and Delphi. Also I like to write API's, which I realized when I made a SmartCard library.