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
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »
Feb 9, 2012 at 10:29 PM
Learning ColdFusion 9: Application-Specific Data Sources
@Ben, No offence, but if people were really wanting advanced features they would be using a platform like ASP.NET MVC. CFML is so structurally compromised as a tag-based scripting language that ... read »
Feb 9, 2012 at 10:03 PM
Subversion - Cleanup Failed To Process The Following Paths
@Leviaguirre, do you still have problems with this? ... read »