Skip to main content
Ben Nadel at CFCamp 2023 (Freising, Germany) with: Luis Majano
Ben Nadel at CFCamp 2023 (Freising, Germany) with: Luis Majano

Aoccdrnig To Rscheearch - ColdFusion UDF To Garble Words

By
Published in Comments (1)

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.

Want to use code from this post? Check out the license.

Reader Comments

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel