简体   繁体   中英

encrypting AES128-CBC on mbed microcontroller(c++) and decrypting AES128-CBC in nodejs

I am receiving "Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt" when I try to decrypt a string on the nodejs side.

I do know for better security I should use a random IV and always change the IV for every new encryption and I should also implement authentication, but for the sake of understanding this and to fix the "bad decrypt" issue, I will stick to something simple. Once I can encrypt and decrypt on the mbed and nodejs, I will implement the random/changing IV and HMAC to enhance the security.

I would like to encrypt sensor data on the mbed side and decrypt the sensor data in nodejs, however, when try to decrypt the data on the nodejs side I receive the "bad decrypt" error. How do I fix the "bad decrypt" error?

mbed encrypted data:
D90E1518FF2E5D79D6F848BCB4A49BCAE3ADDC6F1D6E04265613968CFF242855C10C619C8E281A33DA690039274AA65ECAFA05631C7BB38815442E780E27E34F2B6C4B9FE1B18678077227A05ACB233D8B8A81412E584A6ECAD10397FCF36072B043F93D67B63678A5D385B402D88AF99A62E12413E7BBFDB920B51F732C0933

mbed c++ code:

#include "mbed.h"
#include "Crypto.h"
#include "MbedJSONValue.h"
#include "LinearTempSensor.h"
#include "TimeUtilities.h"
#include <string>

Serial pc(USBTX, USBRX);

RealTimeClock rtc;

LinearTempSensor sensor(p20, 1000, LinearTempSensor::MCP9701);

//unsigned char myKEY[16] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,};
//unsigned char myIV[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, };
unsigned char myKEY[16] = { 'm', 'n', 'b', 'v', 'c', 'x', 'z', 'l', 'k', 'j', 'h', 'g', 'f', 'd', 's', 'a' };
unsigned char myIV[16] = { 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'p', 'o', 'i', 'u', 'y', 't', 'r' };

unsigned char a[0x80] = { };

float Vout, Tav, To;

int main()
{
    pc.baud(115200);

    MbedJSONValue sensorResults;
    std::string s;

    //Create JSON
    sensorResults["Data1"][0] = "Result";
    sensorResults["Data1"][1] = 5.5;
    sensorResults["Data2"][0] = "Result";
    sensorResults["Data2"][1] = 700;
    sensorResults["Data3"][0] = "Result";
    sensorResults["Data3"][1] = 65.7;

    Vout = sensor.Sense();          // Sample data (read sensor)
    Tav  = sensor.GetAverageTemp(); // Calculate average temperature from N samples
    To   = sensor.GetLatestTemp();  // Calculate temperature from the latest sample

    //Serialize JSON
    s = sensorResults.serialize();
    //sl = s.size();

    //Print JSON string
    pc.printf("json: %s\r\n", s.c_str());

    //Convert JSON string to a char array to encrypt
    //char *a=new char[s.size()+1];
    a[s.size()]=0;
    memcpy(a,s.c_str(),s.size());

    //Print the char array to serial terminal
    pc.printf("\r\nJSON Char array");

    for(char i=0; i<s.size(); i++) 
    {
        if(i%16==0) pc.printf("\r\n");
        pc.printf("%.2X",s[i]);
    }

    AES myAES(AES_128, myKEY, myIV, CBC_MODE); // specify all params, look at BlockCipher.h for modes
    pc.printf("\r\n\r\nFirst run\r\n");
    myAES.encrypt(a,a,0x80); // same in and out buffer can be used
    pc.printf("\r\nEncrypted");
    for(char i=0; i<0x80; i++) 
    {
        //if(i%16==0) pc.printf("\r\n");
        pc.printf("%.2X",a[i]);
    }

    pc.printf("\r\nDecrypted again");
    myAES.decrypt(a,a,0x80);
    for(char i=0; i<0x80; i++) 
    {
        //if(i%16==0) pc.printf("\r\n");
        pc.printf("%.2X",a[i]);
    }

}

nodejs code:

var crypto = require("crypto")

function encrypt(key, data, iv) {
        var cipher = crypto.createCipheriv('aes-128-cbc', key, iv);
        var crypted = cipher.update(text, 'utf-8', 'hex');
        crypted += cipher.final('hex');

        return crypted;
}

function decrypt(key, data, iv) {
        var decipher = crypto.createDecipheriv('aes-128-cbc', key, iv);
        var decrypted = decipher.update(data, 'hex', 'utf-8');
        decrypted += decipher.final('utf-8');

        return decrypted;
}

var key = "mnbvcxzlkjhgfdsa";
var iv  = "asdfghjklpoiuytr";

var text = "{\"Data1\":[\"Result\",5.50],\"Data2\":[\"Result\",700],\"Data3\":[\"Result\",65.70]}";
console.log("Original Text: " + text);

var endata = "D90E1518FF2E5D79D6F848BCB4A49BCAE3ADDC6F1D6E04265613968CFF242855C10C619C8E281A33DA690039274AA65ECAFA05631C7BB38815442E780E27E34F2B6C4B9FE1B18678077227A05ACB233D8B8A81412E584A6ECAD10397FCF36072B043F93D67B63678A5D385B402D88AF99A62E12413E7BBFDB920B51F732C0933";
var decryptedText = decrypt(key, endata, iv);
console.log("Decrypted Text: " + decryptedText);

UPDATE:
I sent the C++ encrypted/unencrypted data to my serial terminal to see what the data looks like:

json: {"Data1":["Result",5.50],"Data2":["Result",700],"Data3":["Result",65.70]}

JSON Char array 7B224461746131223A5B22526573756C 74222C352E35305D2C22446174613222 3A5B22526573756C74222C3730305D2C 224461746133223A5B22526573756C74 222C36352E37305D7D

First run

Encrypted D90E1518FF2E5D79D6F848BCB4A49BCAE3ADDC6F1D6E04265613968CFF242855C10C619C8E281A33DA690039274AA65ECAFA05631C7BB38815442E780E27E34F2B6C4B9FE1B18678077227A05ACB233D8B8A81412E584A6ECAD10397FCF36072B043F93D67B63678A5D385B402D88AF99A62E12413E7BBFDB920B51F732C0933

Decrypted again 7B224461746131223A5B22526573756C74222C352E35305D2C224461746132223A5B22526573756C74222C3730305D2C224461746133223A5B22526573756C74222C36352E37305D7D00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Your message is not padded, but Node's Decipher expects a PKCS#7 padding of the input and fails when none is found. Disable padding by calling Decipher.setAutoPadding() :

function decrypt(key, data, iv) {
        var decipher = crypto.createDecipheriv('aes-128-cbc', key, iv);
        decipher.setAutoPadding(false);
        var decrypted = decipher.update(data, 'hex', 'utf-8');
        decrypted += decipher.final('utf-8');

        return decrypted;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM