我想将3字节数组合成一个字节数组.我尝试了以下代码,我不确定为什么不将所有数组都复制到一个字节数组中.
似乎正在发生的是,正在复制所有hmacDigest数组和密码数组,但随后又会再次复制密码数组的一部分.我想要完成的是组合密码数组,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]);
}
}
Run Code Online (Sandbox Code Playgroud)
memcpy数组总是在组合数组的同一点
将其更改为:
memcpy(combined, cipher, S_CIPHER);
memcpy(combined+S_CIPHER, iv, S_IV);
memcpy(combined+S_CIPHER+S_IV, hmacDigest, S_HMACDIGEST);
Run Code Online (Sandbox Code Playgroud)
memcpy 原型是
空虚
*memcpy(void *dest, const void *src, size_t n);
dest每次复制单个数组时,都必须将指针移动到正确的位置.
| 归档时间: |
|
| 查看次数: |
730 次 |
| 最近记录: |