Click here to Skip to main content
15,886,362 members
Articles / General Programming / String

Enhanced String Handling

Rate me:
Please Sign up or sign in to vote.
4.78/5 (16 votes)
16 Dec 2010CPOL28 min read 45.3K   338   38  
Allow for constructs within a string to be programmatically evaluated for a final string value
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnhancedStringEvaluate;
using System.Security.Cryptography;
using System.IO;
using System.Text.RegularExpressions;


namespace TestEvaluation.ProcessEvaluate
{
	/// <summary>
	/// Purpose:
	///		Can be used for strings you need to publish yet protected.  
	///		Like database connection string in a configuration file.
	///	
	/// Format:
	///		{Decrypt::encrypted-string}
	/// </summary>
	public class ProcessDecrypt : IProcessEvaluate
	{
		private readonly TripleDESCryptoServiceProvider _tDes;
		private readonly IDelimitersAndSeparator _delim;
		private readonly Regex _reDecript;

		public ProcessDecrypt() : this(DelimitersAndSeparator.DefaultDelimitedString) { }
		public ProcessDecrypt(IDelimitersAndSeparator delim, byte[] key = null, byte[] iv = null)
		{
			_tDes = new TripleDESCryptoServiceProvider();
			_tDes.Key = key;
			if (key == null)
				_tDes.Key = Array.ConvertAll<byte, byte>(_tDes.Key, b => (byte)(((int)b + 7) % ((int)byte.MaxValue + 1)));

			_tDes.IV = iv;
			if (iv == null)
				_tDes.IV = Array.ConvertAll<byte, byte>(_tDes.IV, b => (byte)(((int)b - 7) % ((int)byte.MaxValue + 1)));

			_delim = delim;

			RegexOptions reo = RegexOptions.Singleline | RegexOptions.IgnoreCase;
			string pattern = @"({)\s*Decrypt\s*::()(})";
		}

		#region IProcessEvaluate Members

		public void Evaluate(object src, EnhancedStringEventArgs ea)
		{
			throw new NotImplementedException();
		}

		#endregion

		/// <summary>
		/// Decrypt encrypted text
		/// </summary>
		/// <param name="sEncripted"></param>
		/// <returns></returns>
		private string DecryptTextFromMemory(string sEncripted)
		{
			try
			{
				byte[] Data = new ASCIIEncoding().GetBytes(sEncripted);
				using (var msDecrypt = new MemoryStream(Data))
				{
					var csDecrypt = new CryptoStream(msDecrypt, _tDes.CreateDecryptor(), CryptoStreamMode.Read);

					byte[] fromEncrypt = new byte[Data.Length];
					csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
					return new ASCIIEncoding().GetString(fromEncrypt);
				}
			}
			catch (CryptographicException e)
			{
				Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
				return null;
			}
		}

		/// <summary>
		/// Give the ability to encript text
		/// </summary>
		/// <param name="text"></param>
		/// <returns></returns>
		public string EncryptTextToMemory(string text)
		{
			try
			{
				byte[] ret;
				using (var mStream = new MemoryStream())
				using (var cStream = new CryptoStream(mStream, _tDes.CreateEncryptor(), CryptoStreamMode.Write))
				{
					byte[] toEncrypt = new ASCIIEncoding().GetBytes(text);
					cStream.Write(toEncrypt, 0, toEncrypt.Length);
					cStream.FlushFinalBlock();
					ret = mStream.ToArray();
				}

				return new ASCIIEncoding().GetString(ret);
			}
			catch (CryptographicException e)
			{
				Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
				return null;
			}
		}
	}
}

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 Code Project Open License (CPOL)


Written By
United States United States
avifarah@gmail.com

Comments and Discussions