Encryption program that works with NodeJs and mbedtls

First of all, let me start by saying that I am not a cryptographer in any way, and I am not very good at writing c-code, so please excuse me if the answer to this question is obvious or answered. I am developing a messaging program and cannot use TLS on the target platform. As a result, I need to find a way to encrypt each message using a symmetric encrypted key with a shared key, such as AES.

I am looking for a way to encrypt and decrypt data between the mbedtls program (e.g. aescrypt2) on one end and the nodejs program on the other. Mbedtls, formerly polarssl, is a library that provides encryption for embedded devices. The source code includes some sample programs, such as aescrypt2, rsaencrypt, ecdsa, and crypt_and_hash.

Aescrypt2 works fine when the resulting encrypted data is also decrypted using aescrypt2, but I can not get the data to be encrypted using aescrypt before decrypting using nodejs crypto or any other program, including openssl. For instance:

echo 'this is a test message' >test.txt aescrypt 0 test.txt test.out hex:E76B2413958B00E193 aescrypt 1 test.out test.denc hex:E76B2413958B00E193 cat test.denc this is a test message 

With openssl:

 openssl enc -in out.test -out outfile.txt -d -aes256 -k E76B2413958B00E193 bad magic number 

Sample node code that currently doesn't work

  var crypto = require('crypto'); var AESCrypt = {}; AESCrypt.decrypt = function(cryptkey, iv, encryptdata) { encryptdata = new Buffer(encryptdata, 'base64').toString('binary'); var decipher = crypto.createDecipheriv('aes-256-cbc', cryptkey, iv), decoded = decipher.update(encryptdata, 'binary', 'utf8'); decoded += decipher.final('utf8'); return decoded; } AESCrypt.encrypt = function(cryptkey, iv, cleardata) { var encipher = crypto.createCipheriv('aes-256-cbc', cryptkey, iv), encryptdata = encipher.update(cleardata, 'utf8', 'binary'); encryptdata += encipher.final('binary'); encode_encryptdata = new Buffer(encryptdata, 'binary').toString('base64'); return encode_encryptdata; } var cryptkey = crypto.createHash('sha256').update('Nixnogen').digest(), iv = 'a2xhcgAAAAAAAAAA', buf = "Here is some data for the encrypt", // 32 chars enc = AESCrypt.encrypt(cryptkey, iv, buf); var dec = AESCrypt.decrypt(cryptkey, iv, enc); console.warn("encrypt length: ", enc.length); console.warn("encrypt in Base64:", enc); console.warn("decrypt all: " + dec); 

This results in errors or garbage text every time. I also tried to configure various things.

I tried this in a hundred different ways, including using the -pass pass:password argument to no avail. Using nodejs, I either got bad decryption errors, or distorted nonsense after decryption. I tried using a lot of online tutorials, such as this one , and suggestions from this topic , and everything else I can find. I read that different encryption programs use different standards, so compatibility between platforms / programs / languages ​​is not always guaranteed, but I think that someone was in this prejudice before and knows the solution?

How do I use nodejs to decrypt data encrypted with aescrypt2 (or such a program)? I was only able to get it to work by calling system exec and node to execute aescrypt2 to decrypt / encrypt data, which is not ideal, since it slows down significantly. I am open to using a program other than aescrypt2. The only requirement is that it must work on Linux, cannot use openssl libs (because they are not supported on the target system), the program should be small and simple due to space limitations, and above all, encryption / decryption should be compatible with nodejs. Any help would be greatly appreciated.

+5
source share
1 answer

How do I use nodejs to decrypt data encrypted with aescrypt2 (or such a program)?

Sorry, but there is no better answer than: doing the same thing that aescrypt2 does when decrypting the file. You contacted the source yourself, so just follow the same steps in node.js as in C in decryption.

First of all, check out the layout of the file containing the encrypted data :

  /* * The encrypted file must be structured as follows: * * 00 .. 15 Initialization Vector * 16 .. 31 AES Encrypted Block #1 * .. * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext) */ 

So, you need to extract the encrypted blocks and HMAC from the IV file, and not try to decrypt everything that you try using openssl (your openssl example also does not use the correct IV, but tries to get it from the provided key - read the man page ).

Then return the key. the actual key used for encryption / decryption is not the one specified on the command line, but 8192 iteration of hashing IV with the passed key on the command line using SHA256.

Finally, they decrypt, using AES-256-ECB (your openssl and node.js examples use CBC!), Every 16 bytes and XOR result with permeable 16 bytes (IV is used for the first 16 bytes).

There, perhaps more, I simply listed the most obvious things that I saw while reading the aescrypt2.c code.

So, my advice: try to write the same logic in node.js and try to find critical calls to node.js for the corresponding copies of mbedtls.

I'm not a cryptography specialist, but I'm sure there are so many steps in aescrypt implementation that seem complicated (like generating the actual key), because they know how to do cryptography and just do it right.

+2
source

All Articles