views:

74

answers:

2

I am trying to encrypt a string using ColdFusion encrypt() with a 3rd party provided key like this:

encrypteded = encrypt('theString', 'FD52250E230D1CDFD5C2DF0D57E3E0FEFD52250E230D1CDF', 'DESEDE/CBC/NoPadding', 'BASE64', ToBase64('0'));

I get:

"The key specified is not a valid key for this encryption: Wrong key algorithm, expected DESede."

What do I have to do to this key in terms of encoding/decoding to get it into the right format?

A: 

The only thing that seems off to me is the algorithm value you're using. Maybe try this?

encrypteded = encrypt('theString', 'FD52250E230D1CDFD5C2DF0D57E3E0FEFD52250E230D1CDF', 'DESEDE', 'BASE64', ToBase64('0'));

I don't know if the /CBC/NoPadding settings will be what you want, but I don't think they will be allowed in the algorithm argument.

Adam Tuttle
adam, DESEDE/CBC/NoPadding is definitely what i need to use. Unfortunately that is also what is causing the problem.
Howie Ross
Extra settings like /CBC/NoPadding are allowed in the algorithm. But as Edward M Smith mentioned, NoPadding means you have do any padding of strings yourself. ie Make sure the input strings are a multiple of 8 bytes.
Leigh
+1  A: 

Generally, when using provided keys from other languages, you have to do a little gymnastics on it to get it into Base64.

Try this for the key argument:

 ToBase64(BinaryDecode('FD52250E230D1CDFD5C2DF0D57E3E0FEFD52250E230D1CDF','hex'))

But, to make this work for me, the input string needed to be a multiple of 8 bytes (because you're specifying NoPadding), and the IV needed to also be a multiple of 8 bytes.

So, this ended up working for me - not sure if you'll be able to decrypt it on the other end, tho, if the IV they're specifying is really what you've got listed there.

 encrypteded = encrypt('theStrin', ToBase64(BinaryDecode('FD52250E230D1CDFD5C2DF0D57E3E0FEFD52250E230D1CDF','hex')), 'DESEDE/CBC/NoPadding', 'BASE64', ToBase64('0000'));

No IV also worked as well (with different output, obviously):

encrypteded = encrypt('theStrin', ToBase64(BinaryDecode('FD52250E230D1CDFD5C2DF0D57E3E0FEFD52250E230D1CDF','hex')), 'DESEDE/CBC/NoPadding', 'BASE64');

If you've been given a Hex IV, then you can use it as such:

encrypteded = encrypt('theStrin', ToBase64(BinaryDecode('FD52250E230D1CDFD5C2DF0D57E3E0FEFD52250E230D1CDF','hex')), 'DESEDE/CBC/NoPadding', 'BASE64', BinaryDecode("7fe8585328e9ac7b","hex"));

Hopefully this is enough info to get you on your way!

Edward M Smith