我确定我在做错解码和解密,但我不知道究竟是什么。ESP8266 AES加密 - 解密
#include "AES.h"
#include "Base64.h"
AES aes;
// Our AES key. Note that is the same that is used on the Node-Js side but as hex bytes.
byte key[] = {0x7e, 0x4e, 0x42, 0x38, 0x43, 0x63, 0x4f, 0x4c, 0x23, 0x4a, 0x21, 0x48, 0x3f, 0x7c, 0x59, 0x72};
// The unitialized Initialization vector
byte iv[N_BLOCK] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
// Our message to encrypt. Static for this example.
String msg = "{\"data\":{\"value\":300}, \"SEQN\":700 , \"msg\":\"IT WORKS!!\" }";
uint8_t generate_random_unit8()
{
uint8_t really_random = *(volatile uint8_t *)0x3FF20E44;
return really_random;
}
// Generate a random initialization vector
void generate_iv(byte *vector)
{
for (int i = 0; i < N_BLOCK; i++)
{
vector[i] = (byte)generate_random_unit8();
}
}
void encrypt()
{
char b64data[200];
byte cipher[1000];
byte iv[N_BLOCK];
generate_iv(iv);
base64_encode(b64data, (char *)iv, N_BLOCK);
String IV_base64 = String(b64data);
Serial.println(" IV b64: " + IV_base64);
int b64len = base64_encode(b64data, (char *)msg.c_str(), msg.length());
Serial.println(" The lenght is: " + String(b64len));
// Encrypt! With AES128, our key and IV, CBC and pkcs7 padding
aes.do_aes_encrypt((byte *)b64data, b64len, cipher, key, 128, iv);
Serial.println("Cipher size: " + String(aes.get_size()));
base64_encode(b64data, (char *)cipher, aes.get_size());
Serial.println("Encrypted data in base64: " + String(b64data));
decrypt(b64data, IV_base64, aes.get_size());
}
void decrypt(String b64data, String IV_base64, int size)
{
char data_decoded[200];
char iv_decoded[200];
byte out[200];
char temp[200];
b64data.toCharArray(temp, 200);
base64_decode(data_decoded, temp, b64data.length());
IV_base64.toCharArray(temp, 200);
base64_decode(iv_decoded, temp, IV_base64.length());
aes.do_aes_decrypt((byte *)data_decoded, size, out, key, 128, (byte *)iv_decoded);
char message[msg.length()];
base64_decode(message, (char *)out, b64data.length());
printf("Out %s \n", message);
}
void setup_aes()
{
aes.set_key(key, sizeof(key)); // Get the globally defined key
}
void setup()
{
Serial.begin(115200);
}
void loop()
{
encrypt();
delay(1000);
}
解码的消息应该是{"data":{"value":300}, "SEQN":700 , "msg":"IT WORKS!!"
。但是我得到{"data":{"value":300}, "SEQN":700 , "msg":"IT WORKS!!" }�������������������������
。我会假设字符长度不正确?
正如回收商指出的那样,返回的字符串缺少终止符。但是,您可以将其仅添加到带有char消息[msg.length()+ 1]的已解码缓冲区中;在解密。但是我发现你的程序非常困惑。请避免不必要的转换(为什么消息应该是一个字符串?为什么你必须来回转换为字符串?最重要的是:为什么将明文转换为base64?) – frarugi87
generate_random_unit8'随机怎么样? – dandavis
是的,这整个代码搞砸了..从一个PoC的博客复制它。 –