Question

Coldfusion Encrypt() with Blowfish differs from Blowfish encryption in PHP and other languages

Asked by: khusroks

The result of encrypt(myText, key, "BLOWFISH", "HEX") gives you a value that you can decrypt just fine in coldfusion, but not the same value as you would get if you ran the same encryption (blowfish, hex) using PHP.  Basically I need to encrypt some text in coldfusion and send it over to an API that expects the value that PHP's blowfish encryption generates.  

I've tried padding the myText variable with null characters to fill it so the length is a multiple of 8 (have to use URLDecode("%00") instead of char(0) as the null character, since char(0) doesn't actually increase the length of the string).  But that doesn't seem to have much of any effect.

Essentially I'm trying to make the result of
encrypt(myText, key, "BLOWFISH", "HEX")
match what you get using this tool:
http://webnet77.com/cgi-bin/helpers/blowfish.pl
this tool seems to output the result that would be generated by php and what the API expects.

If someone can make the result of coldfusion's blowfish encryption match what you get using that tool above I would really appreciate it.

<cfset myText='<?xml version="1.0" encoding="utf-8"?><body version="1.0"><class type="ClassUser" locale="en_US"><method type="Login"><email>myemail@gmail.com<email><password>mypassword<password><method><class><body>'>
 
<cfset myKey="306ab5a7306ab5a7">
 
<cfset encryptedText=encrypt(myText, myKey, "Blowfish", "HEX")>
 
<cfoutput>#encryptedText#</cfoutput>

                                  
1:
2:
3:
4:
5:
6:
7:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-09-22 at 15:26:22ID24753297
Tags

Coldfusion

,

Blowfish

,

encrypt

,

decrypt

,

php

Topics

Cold Fusion Markup Language

,

Encryption for Network Security

,

PHP and Databases

Participating Experts
2
Points
500
Comments
31

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. How to decrypt PHP
    How do I decrypt encoded PHP codes? Thanks
  2. How to encrypt by php and decrypt by swf?
    Hi ALL, How to encrypt by php and decrypt by swf and then encrypt by swf and decrypt by php? Please help
  3. Decrypt PHP Encrypted file with Perl
    Ok experts, i need some help with this one. I'm going to start to receive an encrypted file form another company and decrypt and pars it. So far, I have been unsuccessful in decrypting the file using perl. Here is an example file they sent in decrypting the file using php: H...
  4. Decrypt Rijndael
    Hi, I am using PHP 's mbcrypt (in the code) to encrypt a string. I want to decrypt that string in VB.net. I have seen some code to do Rijndael decryption in VB.net but it does not work for me. Could you help? Many thanks, John
  5. DUKPT decryption for NCR pin pad
    I am trying to integrate Pin pad device with my point of sale application. I have NCR pinpad device. Application communicates with device using java pos. i am getting encrypted pin from PIN pad. I want to decrypt the PIN with in the application. can you help me to decrypt DUK...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: Ray_PaseurPosted on 2009-09-23 at 05:40:38ID: 25402388

Is there any way to drop out of CF into PHP (or pull in a local PHP module) to do the encryption?  I am thinking if you are still on the same server, you would be relatively safe calling a PHP script, passing a string in the URL, and getting a PHP-encrypted string back   This might be easier than trying to make CF do something differently.

 

by: khusroksPosted on 2009-09-23 at 07:23:31ID: 25403379

Unfortunately, that's not something I can do - we don't have PHP available in our environment.

Maybe someone can help me understand what the issue is here.  If it's the same "blowfish" algorithm outputing in the same format (hex), shouldn't the result be the same?

 

by: _agx_Posted on 2009-09-23 at 18:21:02ID: 25409568

> If it's the same "blowfish" algorithm outputing in the same format (hex), shouldn't the result be the same?

I don't know about blowfish specifically, but most algorithms allow you to different parameters (IV, Salt and/or iterations). So even if two programs are using the same algorithm, if they're using different parameters they might produce totally different results.  

Do you know what parameters PHP uses for its implementation?

 

by: _agx_Posted on 2009-09-23 at 18:25:57ID: 25409583

... BTW: My guess would be php is using an IV (see initialization vector in livedocs).  If that actually is the case, you have to modify the CF code to use an IV that matches whatever one is used by php.  Unfortunately, I have no idea what that is.

 

by: khusroksPosted on 2009-09-23 at 19:13:23ID: 25409696

Nope - php isn't using an IV or Salt, and the mode is the same (ecb).

 

by: _agx_Posted on 2009-09-23 at 19:57:05ID: 25409809

Hm, well they're doing something different.  Do you have a link to the API?  

 

by: khusroksPosted on 2009-09-23 at 20:01:35ID: 25409822

Here's what they say in the API

    * Encoding algorithm: blowfish
    * Encoding mode: electronic codebook (ecb)
    * All requests must be encrypted using the Interface Key as the encryption key.
    * Encrypted requests must be encoded in hexadecimal format. For example, the 2 binary bytes \xDF\xA8 are encoded as DFA8 using 4 ASCII characters.
    * If the size of the data is not n * blocksize, the data must be padded with '\0'.

Do you know how to do the last part in CF?  I've tried, but I'm not sure if I'm doing it right.  (Either way, the padding should only affect the last few characters, right?  Even the first few characters of my result is different from the PHP result)

 

by: _agx_Posted on 2009-09-23 at 20:47:26ID: 25409947

> * Encoding mode: electronic codebook (ecb)

    I am assuming the default encoding mode for Blowfish is ECB, but I'm not 100% sure that is true.

> If the size of the data is not n * blocksize, the data must be padded with '\0'

    Yes, using URLDecode("%00") is the right way to produce the null character (ie "\0").  

>  the padding should only affect the last few characters, right

    No, I wouldn't assume that at all.  Having that predicatable a result would seem to defeat purpose of
    encryption (ie make it easily crackable). Granted that is just IMO.  You'd have to read the actual specs of the  
    algorithm.
   
>  For example, the 2 binary bytes

    That is interesting.  They say "binary" bytes.  Somewhere in the back of my mind I seem to remember
    reading something about a difference between php's encoding and CF's?  Something to do with the php
    functions working with bytes as opposed to strings (as in CF), which resulted in a difference between php
    and CF's output.  Let me search around and see if I can find the reference I'm thinking of.

 

by: khusroksPosted on 2009-09-24 at 05:43:16ID: 25412650

to confirm, yes - coldfusion's blowfish default is ecb

regarding n*blocksize - this means the length of the string needs to be a multiple of 8, right?  and then I would need to add as many URLDecode("%00") characters to then end of the string as are needed to make the length a multiple of 8?

please let me know if you find anything on this.  thanks for the help.

 

by: khusroksPosted on 2009-09-24 at 07:47:10ID: 25413900

I looped through adding one padding character at a time to the end of my text string to test what happens to the result of the encryption.  only the last few characters of the encrypted string changed at all - the beginning of the string was exactly the same.

so basically, it seems that the padding is not the issue (or at least not the primary issue) since the result of the encryption between between coldfusion and php differs right from the start.

 

by: _agx_Posted on 2009-09-24 at 10:23:03ID: 25415637

> For example, the 2 binary bytes

Unfortunately, I think I'm interpreting that statement incorrectly.  It says "Encrypted" requests (ie the output, not the input).  

Do you have the source or just the API?

 

by: khusroksPosted on 2009-09-24 at 13:15:17ID: 25417393

just the api...and the result that they're expecting, which is the same result when you plug the text into this tool.

http://webnet77.com/cgi-bin/helpers/blowfish.pl  

 

by: _agx_Posted on 2009-09-25 at 00:42:43ID: 25420749

Unfortunately, if you don't have access to the source, I really don't know how you will be able to figure out the differences.  

 

by: khusroksPosted on 2009-09-25 at 06:52:18ID: 25422935

OK - I got my hands on the PHP function that's used to encrypt - here it is

function encrypt($data, $key, $alg = "blowfish", $mode = "ecb", $iv = "00000000") {
if (false === ($td = @mcrypt_module_open($alg, "", $mode, ""))) {
throw new Exception("Can not initialize the encryption module!", ENCRYPTION_ERROR);
}
if (@mcrypt_generic_init($td, $key, $iv)) {
throw new Exception("Encryption error!", ENCRYPTION_ERROR);
}
$data = @mcrypt_generic($td, $data);
@mcrypt_generic_deinit($td);
@mcrypt_module_close($td);
$data = @unpack("H*", $data);
return $data[1];
}

 

by: _agx_Posted on 2009-09-25 at 08:14:57ID: 25423856

Umm... are they using an IV?

 

by: khusroksPosted on 2009-09-25 at 08:33:50ID: 25424073

no - just the key and the text

 

by: _agx_Posted on 2009-09-25 at 15:08:05ID: 25427665

I'm not great with php code, but it doesn't look like that shows how the encryption is done, or even the data types involved.

 

by: _agx_Posted on 2009-09-25 at 17:50:08ID: 25428332

Have you tried encrypting it using java directly to see if you get the same results? If you do and have access to createObject("java", ...) you could use that instead.

 

by: khusroksPosted on 2009-09-25 at 17:54:51ID: 25428339

I know that Java, PHP, .NET and Delphi all produce exactly the same result for algorithm=BLOWFISH, mode=ECB, encoding=HEX.  The API I need to post to has libraries for all of them, but none for Coldfusion, which is why I need to write the functions myself.

So yes, theoretically that might work, but I have no clue how to use createObject("java", ...).   Can you help?

 

by: _agx_Posted on 2009-09-25 at 18:07:49ID: 25428373

Yes.  Do you happen to have a java example handy? If not, I can hunt one down.

 

by: _agx_Posted on 2009-09-25 at 18:11:39ID: 25428378

Nevermind. I found one here:

http://www.java-tips.org/java-se-tips/javax.crypto/encryption-and-decryption-using-symmetric-2.html

<cfset myText = "something">
<cfset myKey="306ab5a7306ab5a7">
<!--- get a cipher instance --->
<cfset Cipher = createObject("java", "javax.crypto.Cipher")>
<cfset encryptor = Cipher.getInstance("Blowfish/ECB/PKCS5Padding")>
<!--- must convert the key string into a KeySpec object first --->
<cfset keySpec = createObject("java", "javax.crypto.spec.SecretKeySpec").init(myKey.getBytes(), "Blowfish")>
<!--- initialize the cipher for encrypting --->
<cfset encryptor.init(Cipher.ENCRYPT_MODE, keySpec)>
<!--- do the encrypt --->
<cfset encryptedTextFromJava = encryptor.doFinal(myText.getBytes())>
<!--- finally convert it to hex --->
<cfset encryptedText = BinaryEncode(encryptedTextFromJava, "HEX")>
<cfoutput>
	encryptedText = #encryptedText#<br>
</cfoutput>

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:

Select allOpen in new window

 

by: khusroksPosted on 2009-09-25 at 18:30:59ID: 25428410

IT WORKED!!  You rock!  Thanks for sticking with me on this one.  I had almost given up myself.

 

by: _agx_Posted on 2009-09-25 at 18:32:44ID: 25428412

You are very welcome. I wish I'd thought of it sooner.  I can totally appreciate how frustrating this kind of thing can be!

 

by: _agx_Posted on 2009-09-25 at 18:35:07ID: 25428418

BTW - You might want to fill out a possible bug report @ Adobe ... 'cause I still can't figure out why the heck the results are different.  Despite trying all kinds of values, settings, padding, etc..

 

by: khusroksPosted on 2009-09-25 at 18:38:51ID: 25428430

I'll give it a shot, but I'm not sure they'll consider it a bug since it seems to encrypt<->decrypt in coldfusion just fine, just not compatible with the output everywhere else - they may even consider it a security "feature" :)

 

by: _agx_Posted on 2009-09-25 at 18:46:12ID: 25428449

Great.  I think we could all do without "features" like that ;-)  Even if they could explain why the results are different, that would be helpful.  I mean CF does run on top of java, so I'd expect they're using something like the example above internally anyway.  Well.. at least there's a work-around for now!

 

by: khusroksPosted on 2009-09-25 at 18:56:19ID: 25428462

Hey - one last question - to decrypt the data coming back from the API I need to use the same algorithm in reverse.  What's the reverse of the BinaryDecode function?

<cfset Cipher = createObject("java", "javax.crypto.Cipher")>
<cfset decryptor = Cipher.getInstance("Blowfish/ECB/NoPadding")>
<!--- must convert the key string into a KeySpec object first --->
<cfset keySpec = createObject("java", "javax.crypto.spec.SecretKeySpec").init(myKey.getBytes(), "Blowfish")>
<!--- initialize the cipher for encrypting --->
<cfset decryptor.init(Cipher.DECRYPT_MODE, keySpec)>
<!--- do the decrypt --->
<cfset decryptedTextFromJava = decryptor.doFinal(returnText.getBytes())>

                                              
1:
2:
3:
4:
5:
6:
7:
8:

Select allOpen in new window

 

by: _agx_Posted on 2009-09-25 at 19:09:47ID: 25428495

I would just create a string and let it do the conversion.  Note, I replaced your setting with "PKCS5Padding" to match my local example.

<cfset Cipher = createObject("java", "javax.crypto.Cipher")>
<cfset decryptor = Cipher.getInstance("Blowfish/ECB/PKCS5Padding")>
<!--- must convert the key string into a KeySpec object first --->
<cfset keySpec = createObject("java", "javax.crypto.spec.SecretKeySpec").init(myKey.getBytes(), "Blowfish")>
<!--- initialize the cipher for encrypting --->
<cfset decryptor.init(Cipher.DECRYPT_MODE, keySpec)>
<!--- do the decrypt --->
<cfset decryptedTextFromJava = decryptor.doFinal(BinaryDecode(encryptedText, "HEX"))>
<cfset decryptedText = createObject("java", "java.lang.String").init(decryptedTextFromJava)>
<cfoutput>
	decryptedText = #decryptedText#<br>
</cfoutput>
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:

Select allOpen in new window

 

by: _agx_Posted on 2009-09-25 at 19:21:43ID: 25428522

> decryptedTextFromJava

... Not that it really matters :)  but I could have chosen a more accurate variable name there.  Cipher.doFinal(...) returns BYTES not text.

 

by: khusroksPosted on 2009-09-26 at 13:04:38ID: 25431315

Brilliant!  Thanks again for all your help with this.

 

by: _agx_Posted on 2009-09-27 at 10:34:36ID: 25434683

You're welcome.  If you find out anything else that explains the discrepancies, post a follow-up. I'll do the same.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...