在mbed微控制器(c ++)上加密AES128-CBC并在nodejs中解密AES128-CBC

时间:2015-02-24 19:35:58

标签: c++ node.js cryptography aes mbed

当我尝试解密nodejs端的字符串时,我收到“错误:错误:06065064:数字信封例程:EVP_DecryptFinal_ex:bad decrypt”。

我知道为了更好的安全性我应该使用随机IV并且总是为每个新加密更改IV,我也应该实现身份验证,但为了理解这个并修复“不良解密”问题,我会坚持简单的事情。一旦我可以在mbed和nodejs上加密和解密,我将实现随机/更改IV和HMAC以增强安全性。

我想加密mbed端的传感器数据并解密nodejs中的传感器数据,但是,当尝试解密nodejs端的数据时,我收到“bad decrypt”错误。如何修复“错误解密”错误?

mbed加密数据:
D90E1518FF2E5D79D6F848BCB4A49BCAE3ADDC6F1D6E04265613968CFF242855C10C619C8E281A33DA690039274AA65ECAFA05631C7BB38815442E780E27E34F2B6C4B9FE1B18678077227A05ACB233D8B8A81412E584A6ECAD10397FCF36072B043F93D67B63678A5D385B402D88AF99A62E12413E7BBFDB920B51F732C0933

mbed c ++代码:

#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代码:

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);

更新
我将C ++加密/未加密的数据发送到我的串行终端,以查看数据的样子:

  

JSON:   { “数据1”:[ “结果”,5.50], “数据2”:[ “结果”,700], “数据3”:[ “结果”,65.70]}

     

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

     

首次运行

     

加密   D90E1518FF2E5D79D6F848BCB4A49BCAE3ADDC6F1D6E04265613968CFF242855C10C619C8E281A33DA690039274AA65ECAFA05631C7BB38815442E780E27E34F2B6C4B9FE1B18678077227A05ACB233D8B8A81412E584A6ECAD10397FCF36072B043F93D67B63678A5D385B402D88AF99A62E12413E7BBFDB920B51F732C0933

     

再次解密   7B224461746131223A5B22526573756C74222C352E35305D2C224461746132223A5B22526573756C74222C3730305D2C224461746133223A5B22526573756C74222C36352E37305D7D00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

1 个答案:

答案 0 :(得分:2)

您的邮件未填充,但Node的Decipher需要PKCS#7 padding输入,并且在找不到任何内容时失败。通过调用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;
}