Я хочу зашифровать текст с помощью AES 128 CBC, но результаты в Arduino и онлайн-инструментах отличаются. Я пробовал другую библиотеку Arduino, но проблема все еще та же.
мой код в Arduino следующим образом.
#include <Crypto.h>
#include <ebase64.h>
#define BLOCK_SIZE 16
byte key[BLOCK_SIZE] = {0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30};
byte iv[BLOCK_SIZE] = {0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30};
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];
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];
base64_encode(encoded, (char*)enciphered, encrypted_size);
strcpy(output, encoded);
}
void decrypt(char* enciphered, char* output, int length)
{
length = length + 1; //re-adjust
int decodedLen = base64_dec_len(enciphered, length);
char decoded[length];
Serial.println(enciphered);
base64_decode(decoded, enciphered, length);
bufferSize(enciphered, length);
byte deciphered[length];
AES aesDecryptor(key, iv, AES::AES_MODE_128, AES::CIPHER_DECRYPT);
aesDecryptor.process((uint8_t*)decoded, deciphered, length);
strcpy(output, (char*)deciphered);
}
void setup()
{
Serial.begin(115200);
while (!Serial) {
; //wait
}
char plain_text[] = "Now is the time ABCDABC";
// encrypt
int length = 0;
bufferSize(plain_text, length);
Serial.println(length);
char encrypted[length];
encrypt(plain_text, encrypted, length);
Serial.println(encrypted);
// decrypt
length = strlen(encrypted);
char decrypted[length];
decrypt(encrypted, decrypted, length);
Serial.println(decrypted);
}
void loop()
{
}
Ключ и IV:
{0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30};
или
{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0 ',' 0 ',' 0 ',' 0 ',' 0 '};
Текст: «Сейчас время ABCDABC»;
зашифровано
1a7OeiH628V7IIoLU6 + 3n70Dzp6FBQjlGPxSwnuXdzo =
и онлайн-инструмент AES.
[https://www.devglan.com/online-tools/aes-encryption-decryption]
Ключ: 0000000000000000
IV: 0000000000000000
Текст: сейчас время ABCDABC
Я зашифрован в сети, и результат
1a7OeiH628V7IIoLU6 + 3n7ILev6IwcZYVNLalS / TBEg =
Так что я не могу расшифровать друг друга. Кто-нибудь может мне помочь? Большое спасибо!