Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Enhanced String Handling II

, 16 Dec 2010
Overcoming limitations of: Enhanced String Handling
EnhancedStringEvaluate.zip
EnhancedStringEvaluate
bin
Debug
ConfigEvaluate.vshost.exe
ConfigEvaluate.vshost.exe.manifest
EnhancedStringEvaluate.dll
EnhancedStringEvaluate.pdb
ConcordPairEvaluate.csproj.vs10x
EnhancedStringEvaluate.csproj.user
EnhancedStringEvaluate.csproj.vs10x
obj
Debug
Decl
EnhancedStringEvaluate.dll
EnhancedStringEvaluate.pdb
DesignTimeResolveAssemblyReferencesInput.cache
EnhancedStringEvaluate.dll
EnhancedStringEvaluate.pdb
Refactor
ConfigEvaluate.dll
ResolveAssemblyReference.cache
TempPE
Properties
EvaluateTest
bin
Debug
EnhancedStringEvaluate.dll
EnhancedStringEvaluate.pdb
EvaluateSampleTest.dll
EvaluateSampleTest.pdb
EvaluateTest.dll
EvaluateTest.pdb
TestEvaluation.exe
TestEvaluation.pdb
EvaluateSampleTest.csproj.vs10x
EvaluateTest.csproj.vs10x
obj
Debug
Decl
EvaluateSampleTest.dll
EvaluateSampleTest.pdb
DesignTimeResolveAssemblyReferencesInput.cache
EvaluateSampleTest.dll
EvaluateSampleTest.pdb
EvaluateTest.dll
EvaluateTest.pdb
ResolveAssemblyReference.cache
TempPE
Properties
TestEvaluate
bin
Debug
abc.text
EnhancedStringEvaluate.dll
EnhancedStringEvaluate.pdb
TestEvaluate.exe
TestEvaluate.pdb
TestEvaluate.vshost.exe.manifest
TestEvaluation.exe
TestEvaluation.pdb
TestEvaluation.vshost.exe
TestEvaluation.vshost.exe.manifest
TestOnTheFlyConfig.vshost.exe
TestOnTheFlyConfig.vshost.exe.manifest
obj
Debug
Decl
TestEvaluation.exe
TestEvaluation.pdb
DesignTimeResolveAssemblyReferencesInput.cache
Refactor
ResolveAssemblyReference.cache
TempPE
TestEvaluate.exe
TestEvaluate.pdb
TestEvaluation.exe
TestEvaluation.pdb
ProcessEvaluate
Properties
TestEvaluate.csproj.vs10x
TestEvaluation.csproj.user
TestEvaluation.csproj.vs10x
TestOnTheFlyConfig.csproj.vs10x
Enhanced String Handling II.pdf
using System;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnhancedStringEvaluate;
using System.Text.RegularExpressions;


namespace TestEvaluation.ProcessEvaluate
{
	public sealed class ProcessCrypt : IProcessEvaluate
	{
		/// <summary>
		/// Decrypts a string of the form XXX,XXX,XXX,...
		/// </summary>
		static ProcessCrypt()
		{
			_key = new byte[] { 16, 124, 169, 206,   4, 105,  91, 217, 249,   3,
				 36,  34, 109,  82, 219, 181, 125,  93,  98, 188, 91, 166, 182,  46 };
			_iv = new byte[] { 129, 176, 94, 51, 190, 43, 35, 14 };

			var reo = RegexOptions.Singleline | RegexOptions.IgnoreCase;
			_reCrypt = new Regex(@"{\s*Crypt\s*::(?<cipher>[0-9,]*)}", reo);
		}

		/// <summary>
		/// Unlike other examples where I gave you the freedom to choose different delimiters.
		/// In which case the constructor has a parameter DelimitersAndSeparator instance.
		/// For this example I will assume that we have anchored down the delimiters and the 
		/// separator to the default values.  Therefore we do not need to pass in the
		/// DelimitersAndSeparator instance.
		/// </summary>
		public ProcessCrypt() { }

		#region IProcessEvaluate Members

		/// <summary>
		/// Evaluate Task 1:	Read from the string construct passed in through the ea parameter. 
		///						The string is passed in ea.EhancedPairElem.Value.
		///	Evaluate Task 2:	Setting the ea.IsHandled to true if the Evaluate() method evaluated 
		///						the ea.EhancedPairElem.Value to a different value from its original 
		///						value and to false otherwise.
		///	Evaluate Task 3:	In case that the Evaluate() method evaluated the original text to a 
		///						different value from its original ea.EhancedPairElem.Value, then update 
		///						the value pointed to by ea.EhancedPairElem.Value to the new value.
		/// </summary>
		/// <param name="src"></param>
		/// <param name="ea"></param>
		public void Evaluate(object src, EnhancedStringEventArgs ea)
		{
			// Evaluate Task 1:
			string encrypted = ea.EhancedPairElem.Value;

			// ea.IsHandled == false by default
			Match m = _reCrypt.Match(encrypted);
			if (!m.Success) return;

			string deciphered = _reCrypt.Replace(encrypted, CipherReplace);
			if (deciphered == encrypted) return;

			// Evaluate Task 2
			ea.IsHandled = true;

			// Evaluate Task 3
			ea.EhancedPairElem.Value = deciphered;
		}

		#endregion

		/// <summary>
		/// 
		/// </summary>
		/// <param name="m"></param>
		/// <returns></returns>
		private string CipherReplace(Match m)
		{
			string encrypted = m.Groups["cipher"].Value;
			return Decrypt(encrypted);
		}

		private static byte[] _key;
		private static byte[] _iv;
		private const string _criptSplitter = ",";
		private static Regex _reCrypt;

		public string Decrypt(string text)
		{
			byte[] cipher = BreakUpCipher(text);
			//byte[] cipher = Convert.FromBase64String(text);
			using (TripleDES des3 = new TripleDESCryptoServiceProvider())
			using (ICryptoTransform decrypt = des3.CreateDecryptor(_key, _iv))
			{
				Encoding utf = new UTF8Encoding();
				return utf.GetString(decrypt.TransformFinalBlock(cipher, 0, cipher.Length));
			}
		}

		private byte[] BreakUpCipher(string text)
		{
			string[] ret = text.Split(_criptSplitter.ToCharArray());
			return Array.ConvertAll<string, byte>(ret, s => (byte)('\u0000' + int.Parse(s)));
		}

		/// <summary>
		/// In production code, Encrypt(..) does not belong here.
		/// It is provided here so we can test the Decrypt
		/// </summary>
		/// <param name="text"></param>
		/// <returns></returns>
		public static string Encrypt(string text)
		{
			Encoding utf = new UTF8Encoding();
			using (TripleDES des3 = new TripleDESCryptoServiceProvider())
			using (ICryptoTransform encrypt = des3.CreateEncryptor(_key, _iv))
			{
				byte[] cipher = encrypt.TransformFinalBlock(utf.GetBytes(text), 0, utf.GetByteCount(text));
				string[] sCipher = Array.ConvertAll<byte, string>(cipher, b => b.ToString());
				return string.Join(_criptSplitter, sCipher);
			}
		}
	}
}

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)

Share

About the Author

Avi Farah

United States United States
avifarah@gmail.com

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 16 Dec 2010
Article Copyright 2010 by Avi Farah
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid