Click here to Skip to main content
13,353,095 members (74,759 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 15 Dec 2010

Enhanced String Handling II

, 16 Dec 2010
Rate this:
Please Sign up or sign in to vote.
Overcoming limitations of: Enhanced String Handling


In my previous article, Enhanced String Handling, EnhancedStringHandling.aspx, I ended the article with a section: Where do we go from here, where I pointed out a limitation: “The specifications as they stand now will not allow for a delimiter as part of the value. So for example: {identifier::value containing an open or close brace} will not pass the brace matching check. This is not of theoretical interest only. For example, if we would like to write a {Decrypt::encrypted value} decryption construct—if we cannot guarantee that the encrypted value has neither open nor close braces, then we cannot write such a ProcessDecrypt class. This problem is not necessarily confined to the case of a single charactered delimiter because the multi charactered delimiters are transformed into single charactered delimiters.”

A few days after I published the article, it occurred to me that the limitation was in my mind only.

The Perceived Problem

When we attempt to write a regular expression that will match on the form: {Crypt::encrypted-text} we are in trouble if the encrypted-text contains an open delimiter (“{”), a close delimiter (“}”) or a separator string (“::”). Therefore, I concluded, prematurely, that we needed more powerful machinery to handle such situations.

Fixing the Limitation

Up to now, throughout the article: Enhanced String Handling, we handled problems of this kind by substituting the offending string with a new string. In this case, we can use this very same technique by providing the encrypted text as unicode numbers converted to strings.

So if we are to encrypt the string: “01234 -- The quick brown fox jumped over the lazy dogs -- 56789!” we will first encrypt it into a string or characters. But instead of including the encrypted characters in a string as is (in the {Crypt::encrypted string} construct) we will include the unicode character representation of the encrypted string, comma separated. So the string:

"01234 -- The quick brown fox jumped over the lazy dogs -- 56789!" 

encrypted will be represented as:


The ProcessCrypt(), therefore, will process a construct representing the above string, encrypted, like so: {Crypt::225,14,132,..}. Therefore, we solve the issue of an encrypted string that can contain any character including a delimiter or a separator. See CypherTest(), TestMethod. (The sample code in ProcessCrypt() is not meant to be the most secure encryption/decryption code, it is meant to illustrate the point—that we can overcome a delimiter or a separator in the string value to be processed.)

public void CypherTest()
    string org = "01234 -- The quick brown fox jumped over the lazy dogs -- 56789!";
    string encripted = string.Format("Text: {{Crypt::{0}}}",

    var context = new List<IProcessEvaluate>();
    context.Add(new ProcessCrypt());
    var eval = new EnhancedStringEval(context);
    string decript = eval.EvaluateString(encripted);

    Assert.AreEqual("Text: " + org, decript);

While ProcessCrypt is as follows:

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

 public ProcessCrypt() { }

 #region IProcessEvaluate Members

 /// <summary>
 /// </summary>
 /// <param name="src"></param>
 /// <param name="ea"></param>
 public void Evaluate(object src, EnhancedStringEventArgs ea)
     // 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;

     // Task 2
     ea.IsHandled = true;

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


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

 private const string _criptSplitter = ",";
 private static Regex _reCrypt;






  • 15th December, 2010: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Avi Farah
United States United States

You may also be interested in...


Comments and Discussions

GeneralRe: My vote of 2 Pin
Indivara16-Dec-10 4:12
subeditorIndivara16-Dec-10 4:12 
GeneralRe: My vote of 2 Pin
John Simmons / outlaw programmer17-Dec-10 1:14
memberJohn Simmons / outlaw programmer17-Dec-10 1:14 
GeneralRe: My vote of 2 Pin
Paul Selormey16-Dec-10 12:43
memberPaul Selormey16-Dec-10 12:43 
John Simmons / outlaw programmer wrote:
Formatting sucks.

As someone pointed out, if all you saw in this article is his signature ending and then disregarding all the efforts that went into the design and implementation of this code, then you need to examine your approach to people.

Many here have posted many things in their profile, he did not, and but decided to uniquely sign up his article. At most you could have made a suggestion, like putting the signature in his profile.

John Simmons / outlaw programmer wrote:
I don't know what you used to actually write the content, but it added all kinds of junk tags.

You are picking a wrong target, for all the efforts that go into the outlook of the Codeproject, the site article editor is hardly useful. If you want to help, please talk to the right people.

Best regards,
Jesus Christ is LOVE! Please tell somebody.

GeneralRe: My vote of 2 Pin
John Simmons / outlaw programmer17-Dec-10 3:20
memberJohn Simmons / outlaw programmer17-Dec-10 3:20 
GeneralRe: My vote of 2 Pin
Paul Selormey17-Dec-10 4:52
memberPaul Selormey17-Dec-10 4:52 
QuestionFew Questions... Pin
Paul Selormey15-Dec-10 23:32
memberPaul Selormey15-Dec-10 23:32 
GeneralRe: Few Questions... Pin
Avi Farah16-Dec-10 8:00
memberAvi Farah16-Dec-10 8:00 
GeneralRe: Few Questions... Pin
Paul Selormey16-Dec-10 12:20
memberPaul Selormey16-Dec-10 12:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 16 Dec 2010
Article Copyright 2010 by Avi Farah
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid