2

I'm encrypting a string in ColdFusion

enc_string = '7001010000006aaaaaabbbbbb';
uid = encrypt(enc_string,'WTq8zYcZfaWVvMncigHqwQ==','AES','Hex'); 
// secret key for tests only

Result:

DAEB003D7C9DBDB042C63ED214E85854EAB92A5C1EC555765B565CD8723F9655

Later I want to decrypt that string in Node (just an example)

uid='DAEB003D7C9DBDB042C63ED214E85854EAB92A5C1EC555765B565CD8723F9655'
decipher = crypto.createDecipher('aes-192-ecb', 'WTq8zYcZfaWVvMncigHqwQ==')
decipher.setAutoPadding(false);
dec = decipher.update(uid, 'hex', 'utf8')
dec += decipher.final('utf8')

I have tried few ciphers but with no luck. I would like not to modify the ColdFusion code to make it work, but if there is no other chance I will do that. I want to send some ciphered data with GET from one site to another. Any advice?

EDIT: I tried all AES, DES, with IV, without IV, with & without padding. Tried also base64. Also with no luck.

Leigh
  • 28,765
  • 10
  • 55
  • 103
Bonanza
  • 1,601
  • 14
  • 23

3 Answers3

5

ColdFusion Encryption with IV

enc_string = '7001010000006aaaaaabbbbbb';
myKey = Tobase64("abcdefghijkl1234");
myIV = charsetDecode("abcdefghijkl9876", "utf-8");
uid=encrypt(enc_string,myKey,'AES/CBC/PKCS5Padding','hex',myIV);

Encrypted uid value is:

614981D0BC6F19A3022FD92CD6EDD3B289214E80D74823C3279E90EBCEF75D90

Now we take it to node:

var Crypto = require('crypto');

var key = new Buffer('abcdefghijkl1234');
var iv = new Buffer('abcdefghijkl9876');
var encrypted = new Buffer('614981D0BC6F19A3022FD92CD6EDD3B289214E80D74823C3279E90EBCEF75D90', 'hex');
var decipher = Crypto.createDecipheriv('aes-128-cbc', key, iv);
var decrypted = decipher.update(encrypted);
var clearText = Buffer.concat([decrypted, decipher.final()]).toString();

console.log(clearText);

Result is:

7001010000006aaaaaabbbbbb

what was expected.


Origin of the problem

Originally in Coldfusion i was using key generated by:

GenerateSecretKey(algorithm [,keysize]);

which generated base64 key which was required by encrypt method. And there was no 'secret' from which was generated.

In Node Crypto method createDecipheriv gets Buffer as params. Buffers requires secret, not keys. I'm not sure why it doesn't work without IV.

What need to be changed in Coldfusion

  1. Don't use GenerateSecretKey if you want to decrypt in other language than CF
  2. Use Tobase64(secret) to generate key
  3. Use IV and generate it using charsetDecode(ivSecret, "utf-8")
  4. Algorithm: AES/CBC/PKCS5Padding
  5. For AES/ECB look @Leigh answer

In Node every input is Buffer.

I think that this short tutorial can help also people who have same issue in other languages like cf->php or cf->python.

Community
  • 1
  • 1
Bonanza
  • 1,601
  • 14
  • 23
  • Thanks for sharing your knowledge @Bonanza, could you explain as well why your original solution didn't work and this works fine? that will help the reader in understanding the problem better! – Anurag Apr 13 '16 at 04:26
  • Updated answer. Hope that's what you asked for. – Bonanza Apr 13 '16 at 08:01
  • Looks perfect now. Thanks! – Anurag Apr 13 '16 at 08:05
  • 2
    @Bonanza - Thanks for posting more detail, but item 1 and 2 are not quite correct. See [my answer for details](http://stackoverflow.com/a/36608403/104223). – Leigh Apr 13 '16 at 20:07
  • 1
    @Leigh thanks for your clarifications. Didn't know that. Will edit my answer with anchor to your answer. – Bonanza Apr 13 '16 at 20:16
3

A few clarifications and corrections to the accepted answer

Short answer:

  • Use "crytographically random" keys produced by GenerateSecretKey() rather than creating one with Tobase64(secret).
  • Although technically ECB mode works (see below), CBC mode is preferred as the more secure method. For CBC, see my full example: Encrypt in ColdFusion, Decrypt in Node.js

Longer Answer:

  • Don't use GenerateSecretKey if you want to decrypt in other language

No, it is perfectly fine to use the generated value with the encryption functions in other languages - as long as they follow the specifications. That does not mean the values can be used in any language exactly "as is". It may need tweaking to conform with language X or Y's implementation. (For example, a function in language X may expect the key to be a hexadecimal string, instead of base64. So you may need to convert the key value first). That did not quite happen in the original code, which is why the decryption did not work.

GenerateSecretKey() produces a cryptographically random key for the specified algorithm. (While CF generates base64 encoded key strings, it could just as easily be hex encoded. The binary value of the key is what matters.) The generated key is suitable for use with any language that implements the same encryption algorithms and key sizes. However, as I mentioned in the earlier comments, symmetric encryption only works if everything matches. You must use the same key, same algorithm, same iv, etcetera for both encrypting AND decrypting. In the original code, both the "key" and "algorithm" values were different. That is why the decryption failed.

The original code used crypto.createCipher(algorithm, password). Per the API, "password" is used to derive the cipher key. In other words, the Node.js code was using a totally different key than in the CF code. Also, Node.js was configured to use a 192 bit key, whereas the CF code was using a 128 bit key.

To answer your original question, yes - you can use ECB mode (though it is strongly discouraged). However, it requires modifying the CF code to derive the same password Node.js will be using. (The other direction is not possible as it involves one-way hashing.)

To derive the "password" in CF, decode the secret key string into binary and generate an md5 hash. Then decode the hash into binary and re-encode it as base64 to make the encrypt() function happy.

CF:

plainText = "7001010000006aaaaaabbbbbb";
secretKey = "WTq8zYcZfaWVvMncigHqwQ==";
keyHash = hash(binaryDecode(secretKey, "base64"), "md5");
nodeJSPassword = binaryEncode(binaryDecode(keyHash, "hex"), "base64");
encryptedText = encrypt(plainText, nodeJSPassword, "AES/ECB/PKCS5Padding", "Hex"); 
writeOutput(encryptedText);

Result:

C43E1179C15CD962373A6E28486D6F4ADB12FBB6731EF99C9212474E18D51C70

On the Node.js side, modify the code to use a 128 bit key, not 192. Also, password strings are first decoded into binary. When creating the cipher object, you need to indicate the input string is base64 encoded, to ensure it is interpreted properly.

Node.js

var password = 'WTq8zYcZfaWVvMncigHqwQ==';
var passwordBinary = new Buffer(password, "base64");
var encrypted = 'C43E1179C15CD962373A6E28486D6F4ADB12FBB6731EF99C9212474E18D51C70'
var crypto = require('crypto');
var decipher = crypto.createDecipher('aes-128-ecb', passwordBinary );
var decrypted = decipher.update(encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');
console.log(decrypted);

Result:

7001010000006aaaaaabbbbbb

Having said that, Using ECB mode is NOT recommended. The preferred method is CBC mode (with a random iv), which generates less predictable output and hence is more secure.

  • than CF Use Tobase64(secret) to generate key

Along those same lines, while you can technically use arbitrary strings, ie "abcdefghijkl1234", to generate a key - DON'T. A very important part of strong encryption is using secret keys that are "truly random and contain sufficient entropy". So do not just do it yourself. Use a proven function or library, like GenerateSecretKey(), which was specifically designed for the task.

Community
  • 1
  • 1
Leigh
  • 28,765
  • 10
  • 55
  • 103
  • Always generate secrets with special methods. Never commit to public repositories :) – Bonanza Apr 13 '16 at 20:18
  • Um... no idea what you are talking about in that last part {cough}{whistle} ;-) – Leigh Apr 13 '16 at 20:36
  • That explains thing very well. I was almost convinced that `GenerateSecretKey()` had to do with the decryption issue at nodeJs end, but now I know how to use it right :) – Anurag Apr 14 '16 at 03:06
  • Encryption interop is like getting two warring factions to agree to peace. Once everyone is finally speaking the same language, and agrees on the terms it goes swimmingly. Getting to that point ... can be a bit of challenge ;-) Usually it is the fact that CF defaults to ECB mode, and most libs use CBC, causing problems, but not this time. – Leigh Apr 14 '16 at 03:20
1

The ciphers you are using to encrypt and decrypt are not equal.

For Node to decrypt your result to the expected string, you should first make sure that encrypting the initial string in Node gives you the same encrypted result.

Consider the following, which runs through all known (to me) AES ciphers in Node, and tries to get the same encrypted result that you get from Coldfusion:

var crypto = require('crypto');
var key = 'WTq8zYcZfaWVvMncigHqwQ==';
var algorithm;

var ciphers = [
  'aes-128-cbc',
  'aes-128-cbc-hmac-sha1',
  'aes-128-cfb',
  'aes-128-cfb1',
  'aes-128-cfb8',
  'aes-128-ctr',
  'aes-128-ecb',
  'aes-128-gcm',
  'aes-128-ofb',
  'aes-128-xts',
  'aes-192-cbc',
  'aes-192-cfb',
  'aes-192-cfb1',
  'aes-192-cfb8',
  'aes-192-ctr',
  'aes-192-ecb',
  'aes-192-gcm',
  'aes-192-ofb',
  'aes-256-cbc',
  'aes-256-cbc-hmac-sha1',
  'aes-256-cfb',
  'aes-256-cfb1',
  'aes-256-cfb8',
  'aes-256-ctr',
  'aes-256-ecb',
  'aes-256-gcm',
  'aes-256-ofb',
  'aes-256-xts',
  'aes128',
  'aes192',
  'aes256'
]

function encrypt(text){
  var cipher = crypto.createCipher(algorithm, key);
  var crypted = cipher.update(text,'utf8','hex');
  crypted += cipher.final('hex');
  return crypted;
}

for (var i = 0; i < ciphers.length; i++) {
  algorithm = ciphers[i];
  console.log(encrypt("7001010000006aaaaaabbbbbb"));
}

If you run this you will get the following output:

ab1e8ddd6be53040fcfdf07578704ed9831c4e962eddd36899fc3819b51d6ade
ab1e8ddd6be53040fcfdf07578704ed9831c4e962eddd36899fc3819b51d6ade
ff19a0b91dad25671632581655f53139ac1f5554383951e255
e4756965c26df5b2e7e2e5291f5a2b1bc835b523ae7e39da0d
ff93cfff713798bcf94ff60fb61a6d9d4ae0a7ad6672e77a22
ff19a0b91dad25671632581655f5313940ed1d69d874cf04d7
70ef98bda47bd95e64221c144c4fdec1e5ad1422ca9f4589653214577adf9d9a
918559eaab9a983f91160dbdb2f093f55b0a2bc011fbe1b309
ff19a0b91dad25671632581655f53139cb62004d669030b400
2c4e36eb6b08107bbdf9c79c2f93160211128977181fee45ab
37fed7d50a56f42fa26805a69c38b12b519e59116702a9f0d15a437791600b3a
01f4d909c587684862ea9e27598f5d5c489028a223cc79be1a
0c482981e6aefa068b0c0429ba1e46894c39d7e7f27d114651
01c9d7545c3bfe8594ebf5aef182f5d4930db0555708057785
01f4d909c587684862ea9e27598f5d5c7aa4939a9008ea18c4
6fb304a32b676bc3ec39575e73752ad71255f7615a94ed93f78e6d367281ee41
7494a477258946d781cb53c9b37622248e0ba84a48c577c9df
01f4d909c587684862ea9e27598f5d5c889a935648f5f7061f
ea16ecf9ad13756f9bd8ad3fcff2a9e06778647d763f88e679dde519e7155cd6
ea16ecf9ad13756f9bd8ad3fcff2a9e06778647d763f88e679dde519e7155cd6
d0688b6632962acf7905ede7e4f9bd7b2d557e3b828a855208
c0119ab62e5c7a3d932042648291f7cd97c30c9b42c9fa1779
d0f72742cc0415a74e201fcc649f90cf9506eac14e24fd96a9
d0688b6632962acf7905ede7e4f9bd7b5e4921830c30ae8223
d6cd01243405e8741e4010698ab2943526f741cfdb2696b5a6d4e7c14479eccf
2592fb4b19fd100c691598c4bdb82188b6e9d6a6b308d0d627
d0688b6632962acf7905ede7e4f9bd7bf375251be38e1d1e08
d9ae0f940e7c40dcb3a620a5e2a1341819632124af5014bf2f
ab1e8ddd6be53040fcfdf07578704ed9831c4e962eddd36899fc3819b51d6ade
37fed7d50a56f42fa26805a69c38b12b519e59116702a9f0d15a437791600b3a
ea16ecf9ad13756f9bd8ad3fcff2a9e06778647d763f88e679dde519e7155cd6

The encrypted result you have from Coldfusion is not present in the above output.

So, using the AES ciphers available in Node, the encrypted result is always different from your encrypted result from Coldfusion. If the encrypted result is always different, you cannot decrypt it to the same value.

The Coldfusion Encryption Docs are not very helpful at describing exactly which algorithm is used when simply specifying "AES". I would strongly recommend specifying a precise algorithm to use, including which key size to use, and choose one that has a corresponding algorithm in Node.

duncanhall
  • 11,035
  • 5
  • 54
  • 86
  • 1
    Yes, [Strong encryption in ColdFusion](https://helpx.adobe.com/coldfusion/kb/strong-encryption-coldfusion-mx-7.html) is a better resource. The default is "AES/ECB/PKCS5Padding". I think the difference is due to the fact that node.js is using 128 bit key as a "password", rather than the actual key. http://stackoverflow.com/questions/7787773/encrypt-with-node-js-crypto-module-and-decrypt-with-java-in-android-app – Leigh Apr 07 '16 at 18:38
  • Tried that but without success. Can't make it work. Does anybody have working solution? – Bonanza Apr 07 '16 at 20:36
  • 1
    Tried what? It will only work if both sides are using the same everything, algorithm, mode, padding, and iv. In the original code they are not doing that. – Leigh Apr 07 '16 at 21:03
  • Tried to find matching algorithms, modes, padding and iv. Thats why I'm asking the question. Original code is only example. I changed there params. I'm looking for working example. I already spent more than a day with that. – Bonanza Apr 08 '16 at 07:11
  • 1
    Using "AES/ECB/PKCS5Padding" with an iv should work. Can you please update your post with your attempt at using those settings, and the result, and we will go from there. – Leigh Apr 08 '16 at 14:07
  • Ok. I found a solution myself. I think i needed a few days rest from the topic. I will update question with answer. – Bonanza Apr 11 '16 at 07:46
  • 1
    @Leigh your suggestion to use "AES/ECB/PKCS5Padding" with an iv worked as updated in the new answer by [Bonanza](/users/729041/bonanza). If you can post another answer with explanation as to why this approach works, that will go a long way for the future readers to understand the problem and the solution you provided. – Anurag Apr 13 '16 at 04:31
  • 1
    (Edit) @Anurag - Thanks. For the curious I posted an explanation, and a few clarifications about the original issue. Side note, I just noticed a typo. I typed EBC, but obviously meant CBC ;-) – Leigh Apr 13 '16 at 20:11