Search Smith

ColdFusion, SQL queries, and, of course, searching

ColdFusion 10 and Solr range queries

Posted by David Faber on February 20, 2012

I have not yet had a chance to install the ColdFusion 10 beta and try the new and improved CFSEARCH tag. However, this morning when looking at the Solr enhancements page for CF 10, under “Storing your custom data” I noticed the following:

Consider the following code:

<cfsearch criteria='some_criteria and column_i: [ 10 - 20 ]'...>

Here, some_criteria indicates filtering. For example column_i: [ 10 - 20 ] means search all items whose values are between 10 and 20. column_i is the custom field provided by user while indexing.

If this is truly the case, then ColdFusion is tweaking the query text before sending to Solr as Solr uses the “TO” keyword in range queries. Using a hyphen does not work in queries to the Solr web service.

Believe it or not, ColdFusion 9 does seem to include support for range queries – at least, it will not return an error when you attempt a range query on a CF-approved™ field such as custom1, custom2, etc. (I say “seems” because I’ve not been able to get CFSEARCH to return any actual data using such a query.) However, using a hyphen instead of the “TO” keyword causes an error and that error comes from Solr rather than directly from ColdFusion. So it appears that as of version 9 at least, ColdFusion does not replace the hyphen in range queries with the relevant Solr operator.

Posted in ColdFusion, Solr | Tagged: , , , , | Leave a Comment »

ColdFusion 10 now in public beta

Posted by David Faber on February 17, 2012

Good news courtesy of Adobe’s ColdFusion blog.

Of particular interest to readers of Search Smith might be the Solr enhancements in ColdFusion 10. The CFSEARCH tag will now support custom fields above and beyond custom1, custom2, etc., and will also have an orderBy attribute for sorting. I take this to mean that not only has Adobe extended the capabilities of the CFSEARCH tag but also removed the comments around the custom field definitions in the default ColdFusion schema.xml file. What is not clear is whether one can search the contents of those custom fields automatically (i.e., without specifying the field name in the query). There are also some limitations.

The Data Import Handler for indexing data from a database also looks interesting.

Posted in ColdFusion, Solr | Tagged: , , , , , | Leave a Comment »

ColdFusion 9: Getting TripleDES to play nice with .Net

Posted by David Faber on February 6, 2012

I recently had the opportunity to try to implement TripleDES encryption in ColdFusion to try and talk to another piece of software written in .Net. Should be simple and straightforward, right? Actually, it turned out to be rather painful – in part because the fix was so easy.

The reference page for ColdFusion’s encrypt() function on Adobe’s web site states that the algorithm “DES-EDE” is used for Triple DES encryption. This is a typo; the correct algorithm is “DESEDE” (without the hyphen). On the other hand, an article in Adobe’s knowledgebase on strong encyrption with ColdFusion states that “TripleDES” can be used as a synonym for “DESEDE”. This is true, but only to a point, as we will see below.

First, here is some ColdFusion code that uses .Net’s implementation of Triple DES in order to encrypt a string:

<cfset utf8_encoder = createObject("dotnet", "System.Text.UTF8Encoding") />
<cfset ascii_encoder = createObject("dotnet", "System.Text.ASCIIEncoding") />

<cfset cipher_mode = "ECB" />
<cfset padding_mode = "PKCS7" />

<cfquery name="get_key" datasource="#application.datasource#">
    SELECT 'ABCDEFGHIJKLMNOPRSTUVWYZ' AS key_value, '@vector@' AS init_vector
      FROM dual
</cfquery>

<cfset key_array = utf8_encoder.GetBytes(get_key.key_value) />
<cfset iv_array = ascii_encoder.GetBytes(get_key.init_vector) />

<cfset to_encrypt = "a@b.com" />
<cfset to_encrypt_array = utf8_encoder.GetBytes(to_encrypt) />

<cfset tdes = createObject("dotnet", "System.Security.Cryptography.TripleDESCryptoServiceProvider") />
<cfset tdes.Set_Key(key_array) />
<cfset tdes.Set_IV(iv_array) />
<cfset tdes.Set_Mode(createObject("dotnet", "System.Security.Cryptography.CipherMode")[cipher_mode]) />
<cfset tdes.Set_Padding(createObject("dotnet", "System.Security.Cryptography.PaddingMode")[padding_mode]) />

<cfset the_encryptor = tdes.CreateEncryptor() />

<cfset resultArray = the_encryptor.TransformFinalBlock( to_encrypt_array, 0, arrayLen(to_encrypt_array) ) />
<cfset the_encrypted_string = createObject("dotnet", "System.Convert").ToBase64String( resultArray ) />

Yes, you really do have to do all that work just to encrypt a simple string using .Net’s implementation of Triple DES. In fact, it is not much more work to do this in ColdFusion than it is to do it in .Net. This seems like a far cry from how ColdFusion does things:

<cfset the_encrypted_string = encrypt(to_encrypt, get_key.key_value, "DESEDE/#cipher_mode#/#padding_mode#Padding", "Base64", get_key.init_vector) />

One line of code vs. about 15 lines of code – not too bad! The problem is that the results won’t be the same:

CF: bhPAwaCFmCo=
.Net: 6FJVEJ8bMac=

To make a long story short, after some poking around I found out (thanks to a very helpful post on StackOverflow) that the key must be base64-encoded in the call to encrypt():

<cfset the_encrypted_string = encrypt(to_encrypt, toBase64(get_key.key_value), "DESEDE/#cipher_mode#/#padding_mode#Padding", "Base64", get_key.init_vector) />

And then the results will be the same:

CF: 6FJVEJ8bMac=
.Net: 6FJVEJ8bMac=

Now here’s where things get a little bit curious. As I mentioned above, Adobe claims that “TripleDES” is a synonym for “DESEDE” when used in the encrypt() function. But when we try to plug it in here:

<cfset the_encrypted_string = encrypt(to_encrypt, toBase64(get_key.key_value), "TripleDES/#cipher_mode#/#padding_mode#Padding", "Base64", get_key.init_vector) />

ColdFusion throws an error:

The TripleDES/ECB/PKCS7Padding algorithm is not supported by the Security Provider you have chosen.

Now, this can be gotten around by using PKCS5Padding instead of PKCS7, and the two seem to be compatible. However, I don’t think I would want to try to explain that to a client in the event that something goes wrong. More curious is this:

<cfset the_encrypted_string = encrypt(to_encrypt, get_key.key_value, "TripleDES/#cipher_mode#/PKCS5Padding", "Base64", get_key.init_vector) />

“TripleDES” throws an error (The key specified is not a valid key for this encryption: Wrong key size.), while “DESEDE” happily encrypts the string anyway, but gives the wrong result! Just a head’s up for those who need to get ColdFusion and .Net talking to one another.

Posted in ColdFusion | Tagged: , , , , , | 2 Comments »