Aoccdrnig To Rscheearch - ColdFusion UDF To Garble Words

Posted September 24, 2007 at 4:50 PM by Ben Nadel

Tags: ColdFusion

Earlier today, I was talking about the clever SPAM mail I got; it used randomly inserted characters to prevent anti-spam engines from matching banned words while at the same time keeping the text human-readable. This made me think of that popular email that went around a while back that demonstrated that as long as the first and last letters of each word were kept in place, the rest of the letters could be shuffled and it would still be human-readable (for the most part).

Here is the example that made the email rounds:

Aoccdrnig to rscheearch at Cmabrigde uinervtisy, it deosn't mttaer waht oredr the ltteers in a wrod are, the olny iprmoetnt tihng is taht the frist and lsat ltteres are at the rghit pclae. The rset can be a tatol mses and you can sitll raed it wouthit a porbelm. Tihs is bcuseae we do not raed ervey lteter by it slef but the wrod as a wlohe.

I had some time at lunch and thought it would be fun to turn this into a ColdFusion user defined function: GarbleText(). This takes one argument, the target text, and shuffles the letters of each word, leaving the first and last letter of each word the same. Therefore, the only words that will be shuffled will be those consisting of at least 4 letters:

(first:1 letter)(middle: 2+ letters)(last:1 letter)

I perform this using Java regular expressions and the special character set: \w (word characters). This means that in addition to first and last letters, all punctuation will be kept in the same place (as it is not considered a word character.

Here is the GarbleText() ColdFusion user defined function:

  • <cffunction
  • name="GarbleText"
  • access="public"
  • returntype="string"
  • output="false"
  • hint="This takes a string and garbles the text leaving the start and end characters in place.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="Text"
  • type="string"
  • required="true"
  • hint="The text that we are going to garble."
  • />
  •  
  • <!--- Define the local scope. --->
  • <cfset var LOCAL = StructNew() />
  •  
  • <!---
  • Define a pattern that we are going to use to find the
  • text that we can garble. Our patternw will require us
  • to have at least two characters between the start
  • and end characters.
  • --->
  • <cfset LOCAL.Pattern = CreateObject(
  • "java",
  • "java.util.regex.Pattern"
  • ).Compile(
  • "(?<=\b\w)(\w{2,})(?=\w\b)"
  • ) />
  •  
  • <!--- Create a pattern matcher for our regex. --->
  • <cfset LOCAL.Matcher = LOCAL.Pattern.Matcher(
  • ARGUMENTS.Text
  • ) />
  •  
  • <!---
  • Create the text buffer that we are going to use to
  • build the garbled text.
  • --->
  • <cfset LOCAL.Buffer = CreateObject(
  • "java",
  • "java.lang.StringBuffer"
  • ).Init()
  • />
  •  
  • <!---
  • Create a static instance of the collections utility so
  • that we can easily shuffle our substrings.
  • --->
  • <cfset LOCAL.Collections = CreateObject(
  • "java",
  • "java.util.Collections"
  • ) />
  •  
  • <!---
  • Now, use the pattern matcher to loop over the matching
  • substrings that we are going to garble.
  • --->
  • <cfloop condition="LOCAL.Matcher.Find()">
  •  
  • <!--- Get the matching substring. --->
  • <cfset LOCAL.Match = LOCAL.Matcher.Group(
  • JavaCast( "int", 0 )
  • ) />
  •  
  • <!--- Create an array for the characters. --->
  • <cfset LOCAL.Chars = ArrayNew( 1 ) />
  •  
  • <!--- Build out the characters array. --->
  • <cfloop
  • index="LOCAL.CharIndex"
  • from="1"
  • to="#Len( LOCAL.Match )#"
  • step="1">
  •  
  • <!--- Add character to array. --->
  • <cfset ArrayAppend(
  • LOCAL.Chars,
  • Mid( LOCAL.Match, LOCAL.CharIndex, 1 )
  • ) />
  •  
  • </cfloop>
  •  
  •  
  • <!--- Shuffle the character array. --->
  • <cfset LOCAL.Collections.Shuffle(
  • LOCAL.Chars
  • ) />
  •  
  • <!---
  • Add the garbled substring back into the string
  • buffer in our ongoing result. As we do this, convert
  • the character array to a string (we don't need to
  • worry about special characters since they were not
  • matched in the original pattern.
  • --->
  • <cfset LOCAL.Matcher.AppendReplacement(
  • LOCAL.Buffer,
  • ArrayToList( LOCAL.Chars, "" )
  • ) />
  •  
  • </cfloop>
  •  
  •  
  • <!---
  • Now that we have garbled all of the matchign substrings,
  • we have to add the rest of the text that succeeded the
  • last match.
  • --->
  • <cfset LOCAL.Matcher.AppendTail(
  • LOCAL.Buffer
  • ) />
  •  
  •  
  • <!--- Return the garbled text. --->
  • <cfreturn LOCAL.Buffer.ToString() />
  • </cffunction>

Taking this ColdFusion UDF, we can then pass in some text to get a garbled response:

  • <!--- Store some text that we want to garble. --->
  • <cfsavecontent variable="strText">
  • This is quite embarrassing to admit, but I have been
  • totally checking you out from across the room. I don't
  • think that I have ever seen anyone look so amazingly
  • sexy in a dress like that. It's not like me to just
  • come out and say something to that effect, but your
  • legs, your curves, your beauty - I seem to be unable
  • to control myself.
  • </cfsavecontent>
  •  
  • <!--- Output the garbled text. --->
  • #GarbleText( strText )#

Running the above code, we get this garbled output:

Tihs is quite ebnraasrsimg to adimt, but I have been tltloay ckhniceg you out form aroscs the room. I don't thnik taht I have eevr seen aynnoe look so alamgziny sxey in a dress lkie that. It's not like me to just come out and say shnoitemg to that ecfeft, but your lges, your cveurs, yuor btuaey - I seem to be unalbe to cootrnl mlesyf.

Fun times. Try out the GarbleText() UDF for yourself.


You Might Also Be Interested In:



Reader Comments

Sep 24, 2007 at 9:49 PM // reply »
14 Comments

Tkhnas for sahring tihs ilecbirdny uslsees fcuitnon wtih the rset of the cfiolosudn wolrd. I am srue my clehdirn wlil lvoe it.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 24, 2013 at 11:21 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, Ha ha, let's us never speak of justifying "##" notation again :P ... read »
May 24, 2013 at 11:18 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Ah, so it was indeed how I vaguely remembered it to be: A direct assignment value = users.id[ i ] causes value to retain the sticky datatype of the query column. Although unnecessary in ... read »
May 24, 2013 at 9:11 AM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Brandon, Hi, No, I haven't been able to do that. I have just kept it as it is. ... read »
May 23, 2013 at 9:52 PM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Muhmmadibn Did you figure out a solution to launching PDFs? I am running into the same issues myself. There is no way to close the PDF or go back once you launch it. Thanks in advance! ... read »
May 23, 2013 at 6:06 PM
The Girl Who Broke My Heart, And Made Me A Better Person
Good day,ladies and gentle men, my name is Dr AMADI the great spell caster in Africa, i have help so many people for different kind of problems,who say there is no solution to problems on earth, that ... read »
May 23, 2013 at 4:26 PM
ColdFusion QueryAppend( qOne, qTwo )
@Heather, Glad people are still getting value out of this! ... read »
May 23, 2013 at 3:49 PM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, I meant the code at the bottom (not the video). I did try to experiment with an intermediary variable, like: value = users.id[ i ]; arrayContains( userIDs, value ); ... but t ... read »
May 23, 2013 at 11:06 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Are you talking about As Number: YES As String: YES As Java: YES? If so, that's with 3 different ways of referencing the constant 1, not users.id[1]. Query object references(*) are what seem ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools