Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Symmetric Algorithm

Posted on 2008-10-11
2
Medium Priority
?
313 Views
Last Modified: 2013-11-07
Hello All,

My sample code is listed below:
Please look into this.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Security.Cryptography;
using System.Text;
 
/// <summary>
/// Summary description for SymmetricAlgorithm
/// </summary>
public abstract class SymmetricAlgorithm : IDisposable
{
	// State that is accessible to subclasses.
	protected int BlockSizeValue;
	protected int FeedbackSizeValue;
	protected byte[] IVValue;
	protected int KeySizeValue;
	protected byte[] KeyValue;
	protected KeySizes[] LegalBlockSizesValue;
	protected KeySizes[] LegalKeySizesValue;
	protected CipherMode ModeValue;
	protected PaddingMode PaddingValue;
 
	// Create a symmetric algorithm object.
	public static SymmetricAlgorithm Create()
	{
		return (SymmetricAlgorithm)
			(CryptoConfig.CreateFromName
			(CryptoConfig.SymmetricDefault, null));
	}
	public static SymmetricAlgorithm Create(String algName)
	{
		return (SymmetricAlgorithm)
			(CryptoConfig.CreateFromName(algName, null));
	}
 
	// Constructor.
	public SymmetricAlgorithm()
	{
		ModeValue = CipherMode.CBC;
		PaddingValue = PaddingMode.PKCS7;
 
		// Note: some implementations check that the subclass is
		// one of the builtin classes.  This is to ensure that
		// the user hasn't made their own cryptographic algorithm.
		//
		// Supposedly this is to obey US export rules that say
		// that plugging a new algorithm into an existing system
		// should not be allowed.
		//
		// However, releasing source code makes such rules meaningless
		// as the user could always modify the source and recompile.
		//
		// Even if this library was binary-only, it wouldn't stop
		// the user implementing their own encryption class that
		// didn't inherit from this class and then use that.
	}
 
	// Destructor.
	~SymmetricAlgorithm()
	{
		Dispose(false);
	}
 
	// Get or set the block size.
	public virtual int BlockSize
	{
		get
		{
			return BlockSizeValue;
		}
		set
		{
			KeySizes.Validate(LegalBlockSizesValue, value,
				"Crypto_InvalidBlockSize");
			BlockSizeValue = value;
		}
	}
 
	// Get or set the feedback size.
	public virtual int FeedbackSize
	{
		get
		{
			return FeedbackSizeValue;
		}
		set
		{
			if(value > BlockSizeValue)
			{
				throw new CryptographicException
					//(_("Crypto_InvalidFeedbackSize"),value.ToString());
					(("Crypto_InvalidFeedbackSize"),value.ToString());
			}
			FeedbackSizeValue = value;
		}
	}
 
	// Get or set the IV.
	public virtual byte[] IV
	{
		get
		{
			if(IVValue == null)
			{
				GenerateIV();
			}
			return IVValue;
		}
		set
		{
			if(value == null)
			{
				throw new ArgumentNullException("value");
			}
			else if(value.Length != BlockSizeValue)
			{
				throw new CryptographicException
					//(_("Crypto_InvalidIVSize"), value.ToString());
					(("Crypto_InvalidIVSize"), value.ToString());
			}
			IVValue = value;
		}
	}
 
	// Get or set the key value.
	public virtual byte[] Key
	{
		get
		{
			if(KeyValue == null)
			{
				GenerateKey();
			}
			return KeyValue;
		}
		set
		{
			if(value == null)
			{
				throw new ArgumentNullException("value");
			}
			KeySizes.Validate(LegalKeySizesValue, value.Length * 8,
				"Crypto_InvalidKeySize");
			KeyValue = value;
			KeySizeValue = value.Length * 8;
		}
	}
 
	// Get or set the key size value.
	public virtual int KeySize
	{
		get
		{
			return KeySizeValue;
		}
		set
		{
			KeySizes.Validate(LegalKeySizesValue, value,
				"Crypto_InvalidKeySize");
			KeySizeValue = value;
		}
	}
 
	// Get the list of legal block sizes.
	public virtual KeySizes[] LegalBlockSizes
	{
		get
		{
			return LegalBlockSizesValue;
		}
	}
 
	// Get the list of legal key sizes.
	public virtual KeySizes[] LegalKeySizes
	{
		get
		{
			return LegalKeySizesValue;
		}
	}
 
	// Get or set the cipher mode.
	public virtual CipherMode Mode
	{
		get
		{
			return ModeValue;
		}
		set
		{
			if(value < CipherMode.CBC || value > CipherMode.CTS)
			{
				throw new CryptographicException
					//(_("Crypto_InvalidCipherMode"));
					(("Crypto_InvalidCipherMode"));
 
			}
			ModeValue = value;
		}
	}
 
	// Get or set the padding mode.
	public virtual PaddingMode Padding
	{
		get
		{
			return PaddingValue;
		}
		set
		{
			if(value < PaddingMode.None || value > PaddingMode.Zeros)
			{
				throw new CryptographicException
					//(_("Crypto_InvalidPaddingMode"));
					(("Crypto_InvalidPaddingMode"));
			}
			PaddingValue = value;
		}
	}
 
	// Clear the state of this object.
	public void Clear()
	{
		((IDisposable)this).Dispose();
	}
 
	// Dispose the state of this object.
	void IDisposable.Dispose()
	{
		Dispose(true);
		GC.SuppressFinalize(this);
	}
	protected virtual void Dispose(bool disposing)
	{
		if(KeyValue != null)
		{
			Array.Clear(KeyValue, 0, KeyValue.Length);
		}
		if(IVValue != null)
		{
			Array.Clear(IVValue, 0, IVValue.Length);
		}
	}
 
	// Create a decryptor object with the current key and IV.
	public virtual ICryptoTransform CreateDecryptor()
	{
		return CreateDecryptor(Key, IV);
	}
 
	// Create a decryptor object with a specific key and IV.
	public abstract ICryptoTransform CreateDecryptor
		(byte[] rgbKey, byte[] rgbIV);
 
	// Create an encryptor object with the current key and IV.
	public virtual ICryptoTransform CreateEncryptor()
	{
		return CreateEncryptor(Key, IV);
	}
	public abstract ICryptoTransform CreateEncryptor
		(byte[] rgbKey, byte[] rgbIV);
 
	// Generate a random IV.
	public abstract void GenerateIV();
 
	// Generate a random key.
	public abstract void GenerateKey();
 
	// Determine if a key size is valid.
	public bool ValidKeySize(int bitLength)
	{
		return KeySizes.Validate(LegalKeySizesValue, bitLength);
	}
 
	// Validate "rgbKey" and "rgbIV" values for a call on
	// "CreateDecryptor" or "CreateEncryptor".
	internal void ValidateCreate(byte[] rgbKey, byte[] rgbIV)
	{
		if(rgbKey == null)
		{
			throw new ArgumentNullException("rgbKey");
		}
		KeySizes.Validate(LegalKeySizesValue, rgbKey.Length * 8,
			"Crypto_InvalidKeySize");
		if(rgbIV != null)
		{
			// Verify the size of the IV against the block size.
			if((rgbIV.Length * 8) != BlockSizeValue)
			{
				throw new CryptographicException
					//(_("Crypto_InvalidIVSize"), rgbIV.Length.ToString());
					(("Crypto_InvalidIVSize"), rgbIV.Length.ToString());
			}
		}
		else if(ModeValue != CipherMode.ECB)
		{
			// We must have an IV in every mode except ECB.
			throw new ArgumentNullException("rgbIV");
		}
	}
 
}; // class SymmetricAlgorithm

Open in new window

0
Comment
Question by:DevIndian
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 1500 total points
ID: 22694719
The error says it all
Please go through the follwoing link and there is no method named Validate for KeySize
0
 

Author Comment

by:DevIndian
ID: 22694975
I can see the keySize class, how can I add the method Validate in it.Please suggest me.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question