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",
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.