简体   繁体   English

将3个独立的字节数组合并为一个字节数组

[英]Combine 3 separate byte arrays into one byte array

I would like to combine 3 byte arrays into one byte array. 我想将3个字节的数组合并为一个字节的数组。 I tried the following code and I'm not sure why not all of the arrays are being copied into one byte array. 我尝试了以下代码,但不确定为什么不是所有数组都被复制到一个字节数组中。

What seems to be happening is, all of the hmacDigest array and cipher array are being copied, but then a part of the cipher array is being copied again. 似乎正在发生的事情是,所有hmacDigest数组和cipher数组都在被复制,但是随后再次复制了cipher数组的一部分。 What I would like to accomplish is combine the cipher array, iv array and the hmacDigest array and store the combined arrays into the combined byte array. 我要完成的工作是将密码数组,iv数组和hmacDigest数组组合在一起,然后将组合的数组存储到组合的字节数组中。 Also, I would like to combine those arrays in this order: cipher, iv, hmacdigest 另外,我想按以下顺序组合这些数组:cipher,iv,hmacdigest

#include "mbed.h"

#include "cyassl/ctaocrypt/hmac.h"
#include "cyassl/ctaocrypt/aes.h"

#include "MbedJSONValue.h"
#include "LinearTempSensor.h"

#include <string>

using namespace std;

Serial pc(USBTX, USBRX);

LinearTempSensor sensor(p20, 300, LinearTempSensor::MCP9701);
MbedJSONValue sensorResults;

Aes enc;
Aes dec;

Hmac hmac;

float Vout, Tav, To, TempValue;

std::string s;

int main() {

    pc.baud(115200);

    const byte key[16] = { 0x6d, 0x6e, 0x62, 0x76, 0x63, 0x78, 0x7a, 0x6c, 0x6b, 0x6a, 0x68, 0x67, 0x66, 0x64, 0x73, 0x61 };
    const byte iv[16]  = { 0x61, 0x73, 0x64, 0x66, 0x67, 0x68, 0x6a, 0x6b, 0x6c, 0x70, 0x6f, 0x69, 0x75, 0x79, 0x74, 0x72 };

    byte plain[128] = { 'a', 'b', 'c', 'd', 'e', 'f' };   // an increment of 16, fill with data
    byte cipher[128];
    byte deciphered[128];

    byte hkey[24] = { 0x8b, 0x21, 0x31, 0x21, 0xb7, 0xbe, 0x33, 0x1a, 0xcf, 0x1f, 0x71, 0x70, 0x45, 0xaf, 0x5c, 0x02, 0xa7, 0xa1, 0x4c, 0x34, 0xd4, 0xbc, 0x4b, 0x4a };    // fill key with keying material
    byte buffer[2048];   // fill buffer with data to digest
    byte hmacDigest[SHA256_DIGEST_SIZE];

    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

    TempValue = sensor.GetAverageTemp();

    //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] = TempValue;

    //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];
    plain[s.size()]=0;
    memcpy(plain,s.c_str(),s.size());//<-- Fills plain array with the JSON values

    // encrypt
    AesSetKey(&enc, key, sizeof(key), iv, AES_ENCRYPTION);
    AesCbcEncrypt(&enc, cipher, plain, sizeof(plain));

    HmacSetKey(&hmac, SHA256, hkey, sizeof(key));
    HmacUpdate(&hmac, buffer, sizeof(buffer));
    HmacFinal(&hmac, hmacDigest);

    //cipher now contains the cipher text from the plain text.

    pc.printf("\r\nAES Key: ");
    for(int i=0; i<sizeof(key); i++) 
    {
        //if(i%16==0) pc.printf("\r\n");
        pc.printf("%.2X",key[i]);
    }

    pc.printf("\r\nAES IV: ");
    for(int i=0; i<sizeof(iv); i++) 
    {
        //if(i%16==0) pc.printf("\r\n");
        pc.printf("%.2X",iv[i]);
    }

    pc.printf("\r\nPlain HEX: ");
    for(int i=0; i<sizeof(plain); i++) 
    {
        //if(i%16==0) pc.printf("\r\n");
        pc.printf("%.2X",plain[i]);
    }

    pc.printf("\r\nEncrypted: ");
    for(int i=0; i<sizeof(cipher); i++) 
    {
        //if(i%16==0) pc.printf("\r\n");
        pc.printf("%.2X",cipher[i]);
    }

    pc.printf("\r\nhmacDigest: ");
    for(int i=0; i<sizeof(hmacDigest); i++) 
    {
        //if(i%16==0) pc.printf("\r\n");
        pc.printf("%.2X",hmacDigest[i]);
    }

    // decrypt
    AesSetKey(&dec, key, sizeof(key), iv, AES_DECRYPTION);
    AesCbcDecrypt(&dec, deciphered, cipher, sizeof(cipher));

    pc.printf("\r\nDecrypted: ");
    for(int  i=0; i<sizeof(deciphered); i++) 
    {
        //if(i%16==0) pc.printf("\r\n");
        //pc.printf("%.2X",deciphered[i]);
        pc.printf("%c",deciphered[i]);
    }

    //Combine the EncryptedData + IV + HMAC
    const int S_CIPHER = sizeof(cipher);
    const int S_IV = sizeof(iv);
    const int S_HMACDIGEST = sizeof(hmacDigest);

    const int S_TOTAL = S_CIPHER + S_IV + S_HMACDIGEST;

    byte combined[S_TOTAL];
    //Copy arrays in individually.
    memcpy(combined, cipher, S_CIPHER);
    memcpy(combined, iv, S_IV);
    memcpy(combined, hmacDigest, S_HMACDIGEST);

    pc.printf("\r\nOutput: ");
    for(int i=0; i<sizeof(combined); i++) 
    {
        //if(i%16==0) pc.printf("\r\n");
        //pc.printf("%.2X",deciphered[i]);
        pc.printf("%.2X",combined[i]);
    }   
}

You memcpy arrays always at the same point of combined array memcpy数组始终位于组合数组的同一点

Change it to: 更改为:

memcpy(combined, cipher, S_CIPHER);
memcpy(combined+S_CIPHER, iv, S_IV);
memcpy(combined+S_CIPHER+S_IV, hmacDigest, S_HMACDIGEST);

memcpy prototype is memcpy原型是

void *memcpy(void *dest, const void *src, size_t n); void *memcpy(void *dest, const void *src, size_t n);

You have to move dest pointer to the correct position each time you copy a single array. 每次复制单个数组时,都必须将dest指针移动到正确的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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