Encrypt and Decrypt function in JS to Qt
-
wrote on 29 Jan 2025, 08:08 last edited by aha_1980
Hi,
I need help to convert below function to qt:// Encrypt const encrypt = (key, message) => { const cipher = crypto.createCipheriv('aes-128-ecb', key, null) cipher.setAutoPadding(false) const encryptedData = Buffer.concat([cipher.update(message), cipher.final()]) return encryptedData } // Decrypt const decrypt = (key, data) => { const decipher = crypto.createDecipheriv('aes-128-ecb', key, null) decipher.setAutoPadding(false) const decryptedData = Buffer.concat([decipher.update(data), decipher.final()]) return decryptedData }
-
Hi,
I need help to convert below function to qt:// Encrypt const encrypt = (key, message) => { const cipher = crypto.createCipheriv('aes-128-ecb', key, null) cipher.setAutoPadding(false) const encryptedData = Buffer.concat([cipher.update(message), cipher.final()]) return encryptedData } // Decrypt const decrypt = (key, data) => { const decipher = crypto.createDecipheriv('aes-128-ecb', key, null) decipher.setAutoPadding(false) const decryptedData = Buffer.concat([decipher.update(data), decipher.final()]) return decryptedData }
@Damian7546 Take a look at https://github.com/bricke/Qt-AES
-
@Damian7546 Take a look at https://github.com/bricke/Qt-AES
wrote on 30 Jan 2025, 08:51 last edited by Damian7546@jsulm looks good.
QByteArray crypt(QByteArray key, QByteArray message) { QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB); return encryption.encode(message, key); } QByteArray decrypt(QByteArray key, QByteArray data) { QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB); return encryption.decode(data, key); }
-
-
@Damian7546 Take a look at https://github.com/bricke/Qt-AES
wrote on 26 Feb 2025, 18:45 last edited by Damian7546@jsulm I still problem with encrypt message in qt.
I preapred simple function based on Qt-AES
QByteArray encrypt(QByteArray key, QByteArray message) { QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB); return encryption.encode(message, key); }
And run example:
qDebug() << encrypt("test","test").toHex();
And I get empty output.
The same key and message I encrypt in JS example like below :
var crypto = require('crypto'); var mykey = crypto.createCipher('aes-128-ecb', 'test'); var mystr = mykey.update('test') mystr += mykey.final('hex'); console.log(mystr);
And the result is :
35e7a77ebef24d71581bc4f6a7fb77f0Why in qt doesnt work ?
-
Hi,
Did you first try the Qt-AES examples to ensure it's working properly ?
If so, then modify the example to match your current setup. Is it still working ? -
Hi,
Did you first try the Qt-AES examples to ensure it's working properly ?
If so, then modify the example to match your current setup. Is it still working ?wrote on 27 Feb 2025, 06:06 last edited by Damian7546 -
@jsulm I still problem with encrypt message in qt.
I preapred simple function based on Qt-AES
QByteArray encrypt(QByteArray key, QByteArray message) { QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB); return encryption.encode(message, key); }
And run example:
qDebug() << encrypt("test","test").toHex();
And I get empty output.
The same key and message I encrypt in JS example like below :
var crypto = require('crypto'); var mykey = crypto.createCipher('aes-128-ecb', 'test'); var mystr = mykey.update('test') mystr += mykey.final('hex'); console.log(mystr);
And the result is :
35e7a77ebef24d71581bc4f6a7fb77f0Why in qt doesnt work ?
wrote on 27 Feb 2025, 07:46 last edited by Bonnie- The output is empty because your key is not valid. It needs to be 16 bytes in your case (AES128).
- You are using the deprecated
crypto.createCipher(algorithm, password)
in Node.js which is different from the currentcrypto.createCipheriv(algorithm, key, iv)
.
From the Node.js doc, you can find that
The implementation of crypto.createCipher() derives keys using the OpenSSL function EVP_BytesToKey with the digest algorithm set to MD5, one iteration, and no salt.
As I can see this Qt-AES project only supports key/iv mode, so you either change your js code to use
crypto.createCipheriv
with a valid key, or you write a function to do the same as EVP_BytesToKey. You can also link openssl and call EVP_BytesToKey, actually if you do that you can try learning openssl APIs to do all the encrytion and decryption parts you need.- Another thing need to change is to set the padding mode to
PKCS7
QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB, QAESEncryption::PKCS7);
-
- The output is empty because your key is not valid. It needs to be 16 bytes in your case (AES128).
- You are using the deprecated
crypto.createCipher(algorithm, password)
in Node.js which is different from the currentcrypto.createCipheriv(algorithm, key, iv)
.
From the Node.js doc, you can find that
The implementation of crypto.createCipher() derives keys using the OpenSSL function EVP_BytesToKey with the digest algorithm set to MD5, one iteration, and no salt.
As I can see this Qt-AES project only supports key/iv mode, so you either change your js code to use
crypto.createCipheriv
with a valid key, or you write a function to do the same as EVP_BytesToKey. You can also link openssl and call EVP_BytesToKey, actually if you do that you can try learning openssl APIs to do all the encrytion and decryption parts you need.- Another thing need to change is to set the padding mode to
PKCS7
QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB, QAESEncryption::PKCS7);
wrote on 27 Feb 2025, 08:36 last edited by Damian7546@Bonnie Thank for response.
skipping the above test,
I try send encrypted data to my slave device.
The device need 128 bit key, so prepared key in qt is :
QByteArray key= "67452301674523014cdc0c0000000000"but something is wrong with encrypted.
Above I mentioned about JS function because it is works :
// Encrypt const encrypt = (key, message) => { const cipher = crypto.createCipheriv('aes-128-ecb', key, null) cipher.setAutoPadding(false) const encryptedData = Buffer.concat([cipher.update(message), cipher.final()]) return encryptedData }
So, what can I do to encrypt my data in the same way in JS example ?
Maybe the problem is that my key in QByteArray represent 32 bytes , an no 16 bytes ? -
@Bonnie Thank for response.
skipping the above test,
I try send encrypted data to my slave device.
The device need 128 bit key, so prepared key in qt is :
QByteArray key= "67452301674523014cdc0c0000000000"but something is wrong with encrypted.
Above I mentioned about JS function because it is works :
// Encrypt const encrypt = (key, message) => { const cipher = crypto.createCipheriv('aes-128-ecb', key, null) cipher.setAutoPadding(false) const encryptedData = Buffer.concat([cipher.update(message), cipher.final()]) return encryptedData }
So, what can I do to encrypt my data in the same way in JS example ?
Maybe the problem is that my key in QByteArray represent 32 bytes , an no 16 bytes ?wrote on 27 Feb 2025, 08:54 last edited by Bonnie@Damian7546 said in Encrytp and Decrypt function in JS to Qt:
Maybe the problem is that my key in QByteArray represent 32 bytes , an no 16 bytes ?
Obviously this is a problem, if I try your above js code with the key you provide, I'll receive "RangeError: Invalid key length".
If you want to use a 32 bytes key, then you should useaes-256-ecb
.
Also issetAutoPadding(false)
really necessary? With this, your message data's length "must be a multiple of the cipher's block size".
Of course if there's no padding, then you don't need to change QAESEncryption's padding mode. -
@Damian7546 said in Encrytp and Decrypt function in JS to Qt:
Maybe the problem is that my key in QByteArray represent 32 bytes , an no 16 bytes ?
Obviously this is a problem, if I try your above js code with the key you provide, I'll receive "RangeError: Invalid key length".
If you want to use a 32 bytes key, then you should useaes-256-ecb
.
Also issetAutoPadding(false)
really necessary? With this, your message data's length "must be a multiple of the cipher's block size".
Of course if there's no padding, then you don't need to change QAESEncryption's padding mode.wrote on 27 Feb 2025, 09:19 last edited by Damian7546@Bonnie @SGaist , I tested encrypt function according with below key and data in Qt , but solution is different with JS example.
I prepared message data length which one be multiple of the cipher's block size.Below example in Qt:
QByteArray encrypt(QByteArray key, QByteArray message) { QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB, QAESEncryption::ZERO); return encryption.encode(message, key); } qDebug() << "eCommandLine: " << eCommandLine.toHex()<< " Bytes: " << eCommandLine.length(); qDebug() << "encryptKey: " << keys.encryptKey.toHex() << " Bytes: " << keys.encryptKey.length(); QByteArray DATAcrypt = encrypt(keys.encryptKey, eCommandLine); qDebug() << "DATAcrypt: " << DATAcrypt.toHex()<<" Bytes: " << DATAcrypt.length();
And result:
eCommandLine: "01000000000754e486bf6c74161e64c8" Size in bytes: 16 EncryptKey: "67452301674523010c2f910100000000" Size in bytes: 16 DATAcrypt: "f3693ed674eb3933039a61fdf7a35003" Size in bytes: 16
Below example in JS:
var crypto = require('crypto'); // Encrypt const encrypt = (key, message) => { const cipher = crypto.createCipheriv('aes-128-ecb', key, null) cipher.setAutoPadding(false) const encryptedData = Buffer.concat([cipher.update(message), cipher.final()]) return encryptedData } console.log(eCommandLine); console.log(encryptKey); console.log(encrypt(encryptKey, eCommandLine ));
And result:
01000000000754e486bf6c74161e64c8 <Buffer 67 45 23 01 67 45 23 01 0c 2f 91 01 00 00 00 00> <Buffer 2d 46 9e 27 e4 63 b8 78 81 80 0f 3b 29 2b e1 92 ef b9 62 35 5d dc c7 71 6a 7d 11 a4 72 d7 72 26>
@Bonnie Like you can see the encrypted buffers in both solutions are different.
Which one is good ? -
@Bonnie @SGaist , I tested encrypt function according with below key and data in Qt , but solution is different with JS example.
I prepared message data length which one be multiple of the cipher's block size.Below example in Qt:
QByteArray encrypt(QByteArray key, QByteArray message) { QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB, QAESEncryption::ZERO); return encryption.encode(message, key); } qDebug() << "eCommandLine: " << eCommandLine.toHex()<< " Bytes: " << eCommandLine.length(); qDebug() << "encryptKey: " << keys.encryptKey.toHex() << " Bytes: " << keys.encryptKey.length(); QByteArray DATAcrypt = encrypt(keys.encryptKey, eCommandLine); qDebug() << "DATAcrypt: " << DATAcrypt.toHex()<<" Bytes: " << DATAcrypt.length();
And result:
eCommandLine: "01000000000754e486bf6c74161e64c8" Size in bytes: 16 EncryptKey: "67452301674523010c2f910100000000" Size in bytes: 16 DATAcrypt: "f3693ed674eb3933039a61fdf7a35003" Size in bytes: 16
Below example in JS:
var crypto = require('crypto'); // Encrypt const encrypt = (key, message) => { const cipher = crypto.createCipheriv('aes-128-ecb', key, null) cipher.setAutoPadding(false) const encryptedData = Buffer.concat([cipher.update(message), cipher.final()]) return encryptedData } console.log(eCommandLine); console.log(encryptKey); console.log(encrypt(encryptKey, eCommandLine ));
And result:
01000000000754e486bf6c74161e64c8 <Buffer 67 45 23 01 67 45 23 01 0c 2f 91 01 00 00 00 00> <Buffer 2d 46 9e 27 e4 63 b8 78 81 80 0f 3b 29 2b e1 92 ef b9 62 35 5d dc c7 71 6a 7d 11 a4 72 d7 72 26>
@Bonnie Like you can see the encrypted buffers in both solutions are different.
Which one is good ?wrote on 28 Feb 2025, 01:40 last edited by Bonnie@Damian7546 Your input data is different according to the output results.
In Qt/C++ code, "eCommandLine" is 16-bytes binary raw data whose hex string is "01000000000754e486bf6c74161e64c8".
In JS code, "eCommandLine" is a 32-bytes string whose content is plain "01000000000754e486bf6c74161e64c8". -
@Damian7546 Your input data is different according to the output results.
In Qt/C++ code, "eCommandLine" is 16-bytes binary raw data whose hex string is "01000000000754e486bf6c74161e64c8".
In JS code, "eCommandLine" is a 32-bytes string whose content is plain "01000000000754e486bf6c74161e64c8".wrote on 28 Feb 2025, 13:21 last edited by Damian7546@Bonnie I fixed buffer in JS and solutions are the same.
JS result:<Buffer 01 00 00 00 00 07 54 e4 86 bf 6c 74 16 1e 64 c8> <Buffer 67 45 23 01 67 45 23 01 0c 2f 91 01 00 00 00 00> <Buffer f3 69 3e d6 74 eb 39 33 03 9a 61 fd f7 a3 50 03>
Qt result:
eCommandLine: "01000000000754e486bf6c74161e64c8" Size in bytes: 16 EncryptKey: "67452301674523010c2f910100000000" Size in bytes: 16 DATAcrypt: "f3693ed674eb3933039a61fdf7a35003" Size in bytes: 16
1/12