Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
 
Add your own
alternative version

Password Safe Database Reader Library in C# for .NET

, 16 Oct 2007 GPL3
An independent library implementation to read Password Safe Password Manager V3 database files
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.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

Svante Seleborg
Web Developer Axantum Software AB
Sweden Sweden
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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411023.1 | Last Updated 16 Oct 2007
Article Copyright 2007 by Svante Seleborg
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid