我正在尝试在Arduino上实现AES128和256(Adafruit Feather M0,对于使用SAMD21处理器的任何其他人!)。加密和解密 有效,但我无法“保存”加密值。我相信该示例将char数组的指针传递给void encrypt()
,但是当使用strcpy
,strncpy
或memcpy
将值从本地char数组复制到一个回到我的loop()
中,该值实际上从未被复制过来。
请注意,挂起只发生在void encrypt()
方法中,我想知道它是否是由encode_base64
行引起的,其中示例代码将数据转换为unsigned char*
。我已经能够strcpy
成功使用strncpy
,memcpy
和void decrypt()
,所以我能想到的是它是无符号字符类型。
虽然根据this,char最终被视为标准库中的unsigned char,我假设strcpy
函数是标准字符串库的一部分,而不是Arduino特有的东西。
我对C / C ++很陌生并且远不是专家,我不太清楚如何解决这个问题所以我希望有人可以指出我正确的方向。
代码(我在每个#include
的顶部包含了lib的链接)
#include <Crypto.h> // https://github.com/intrbiz/arduino-crypto
#include <base64.hpp> // https://github.com/Densaugeo/base64_arduino
#define BLOCK_SIZE 16
uint8_t key[BLOCK_SIZE] = { 0x1C,0x3E,0x4B,0xAF,0x13,0x4A,0x89,0xC3,0xF3,0x87,0x4F,0xBC,0xD7,0xF3, 0x31, 0x31 };
uint8_t iv[BLOCK_SIZE] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
char plain_text[] = "1234567890ABCDEF1234567890ABCDEF";
void bufferSize(char* text, int &length)
{
int i = strlen(text);
int buf = round(i / BLOCK_SIZE) * BLOCK_SIZE;
length = (buf <= i) ? buf + BLOCK_SIZE : length = buf;
}
void encrypt(char* plain_text, char* output, int length)
{
byte enciphered[length];
// RNG::fill(iv, BLOCK_SIZE); // Using fixed test iv
AES aesEncryptor(key, iv, AES::AES_MODE_128, AES::CIPHER_ENCRYPT);
aesEncryptor.process((uint8_t*)plain_text, enciphered, length);
int encrypted_size = sizeof(enciphered);
char encoded[encrypted_size];
encode_base64(enciphered, encrypted_size, (unsigned char*)encoded);
Serial.print("void encrypt :: Encrypted: ");
Serial.println(encoded);
// strcpy(output, encoded); //- Hangs
// strncpy(output, encoded, strlen((char*)encoded)); - Hangs
// memcpy(output, encoded, strlen((char*)encoded)); - Hangs
}
void decrypt(char* enciphered, char* output, int length)
{
length = length + 1; //re-adjust
char decoded[length];
decode_base64((unsigned char*)enciphered, (unsigned char*)decoded);
bufferSize(enciphered, length);
byte deciphered[length];
AES aesDecryptor(key, iv, AES::AES_MODE_128, AES::CIPHER_DECRYPT);
aesDecryptor.process((uint8_t*)decoded, deciphered, length);
Serial.print("void decrypt :: Decrypted: ");
Serial.println((char*)deciphered);
strcpy(output, (char*)deciphered);
// strncpy(output, (char*)deciphered, strlen((char*)deciphered));
// memcpy(output, (char*)deciphered, strlen((char*)deciphered));
}
void setup() {
Serial.begin(115200);
while (!Serial) {
; //wait
}
Serial.println("AES128-CBC Test :: Starting...");
Serial.print("Plaintext input "); Serial.println(plain_text);
}
void loop() {
Serial.println(" = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = \n");
// encrypt
int length = 0;
bufferSize(plain_text, length);
// Serial.print("Buffer length: ");
// Serial.println(length);
char encrypted[128];
encrypt(plain_text, encrypted, length);
// Serial.println("");
Serial.print("RETURNED Encrypted Value: ");
Serial.println(encrypted);
// decrypt
length = 128; // WAS strlen(encrypted);
char decrypted[length];
char testEncryptedPayload[] = "pJUX0k/h/63Jywlyvn7vTMa9NdJF9Mz6JOB1T1gDMq+0NUkNycBR780kMvCYILGP"; // Added for testing purposes
decrypt(testEncryptedPayload, decrypted, length);
Serial.print("RETURNED Decrypted Value: ");
Serial.println(decrypted);
delay(5000);
}
/*
EXAMPLE FROM DOCS => loop()
void loop() {
char plain_text[] = "1234567890ABCDEF1234567890ABCDEF";
// encrypt
int length = 0;
bufferSize(plain_text, length);
char encrypted[length];
encrypt(plain_text, encrypted, length);
Serial.println("");
Serial.print("Encrypted: ");
Serial.println(encrypted);
// decrypt
length = strlen(encrypted);
char decrypted[length];
decrypt(encrypted, decrypted, length);
Serial.print("Decrypted: ");
Serial.println(decrypted);
delay(5000);
}
*/
示例输出:
AES128-CBC Test :: Starting...
Plaintext input 1234567890ABCDEF1234567890ABCDEF
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
void encrypt :: Encrypted: pJUX0k/h/63Jywlyvn7vTMa9NdJF9Mz6JOB1T1gDMq/eQVoPjf/UYv+9SuzV8LQa
RETURNED Encrypted Value: L
void decrypt :: Decrypted: 1234567890ABCDEF1234567890ABCDEF
RETURNED Decrypted Value: 1234567890ABCDEF1234567890ABCDEF
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
答案 0 :(得分:0)
我还没有运行代码,但是根据我的经验,M0的“挂起”通常与内存有关。
这两行:
// strncpy(output, encoded, strlen((char*)encoded)); - Hangs
// memcpy(output, encoded, strlen((char*)encoded)); - Hangs
也许实际失败的是strlen
。也许encoded
是不是 NULL终止?您可以尝试实际长度吗? (例如memcpy(output, encoded, encoded_length);
)