AES加密到解密未正确输出
AES encryption to decryption not outputting correctly
嘿,我最难弄清楚如何解密加密文本。
这是我的草图代码:
#include "AES.h"
#include "base64.h"
AES aes;
void gen_iv(byte *iv) {
for (int i = 0 ; i < N_BLOCK ; i++ ) {
iv[i]= (byte) *(volatile uint8_t *)0x3FF20E44;
}
}
void setup() {
Serial.begin(115200);
Serial.println("nBooting...");
char b64data[2000];
byte cipher[1000];
byte iv [N_BLOCK];
char *encodedFinal;
Serial.println("Let's encrypt:");
byte *key = (unsigned char*)"5TGB&YHN7UJM(IK<";
byte *my_iv = (unsigned char*)"!QAZ2WSX#EDC4RFV";
char *msg = "{"data":{"value":300}, "SEQN":700 , "msg":"IT WORKS!!" }";
//Set the key for AES
aes.set_key(key, sizeof(key));
/*
==================================================================
Encoding section
==================================================================
*/
//Encode IV to Base64
base64_encode(b64data, (char *)my_iv, N_BLOCK);
Serial.println(" IV -> Base64: " + String(b64data));
Serial.println(" Orignal Msg: " + String(msg));
//Encode message into Base64
int b64len = base64_encode(b64data, (char *)msg, String(msg).length());
Serial.println(" Message -> Base64: " + String(b64data));
// Encrypt into AES256
aes.do_aes_encrypt((byte *)b64data, b64len , cipher, key, 256, my_iv);
Serial.println("Encrypted: " + String(b64data));
//Encode everything now in Base64
base64_encode(b64data, (char *)cipher, aes.get_size());
Serial.println("Encrypted -> Base64: " + String(b64data));
encodedFinal = (char*)b64data;
Serial.println("Final encoded: " + String(encodedFinal));
/*
==================================================================
Decoding section
==================================================================
*/
Serial.println();
Serial.println();
Serial.println();
Serial.println();
//Decoding everything from Base64
char b64dataDecode[2000];
byte cipherDecode[1000];
//Decode from Base64 to Encrypted msg
base64_decode(b64dataDecode, (char *)encodedFinal, aes.get_size());
Serial.println(" Base64 -> Encrypted: " + String(b64dataDecode));
//Decoding from Encrypted
aes.do_aes_decrypt((byte *)encodedFinal, base64_dec_len(encodedFinal, String(encodedFinal).length()), cipherDecode, key, 256, my_iv);
Serial.println("Encrypted -> Original Msg: ") + String(encodedFinal);
Serial.println("Done...");
}
void loop() {
// put your main code here, to run repeatedly:
}
这是我得到的输出:
Booting...
Let's encrypt:
IV -> Base64: IVFBWjJXU1gjRURDNFJGVg==
Orignal Msg: {"data":{"value":300}, "SEQN":700 , "msg":"IT WORKS!!" }
Message -> Base64: eyJkYXRhIjp7InZhbHVlIjozMDB9LCAiU0VRTiI6NzAwICwgIm1zZyI6IklUIFdPUktTISEiIH0=
Encrypted: eyJkYXRhIjp7InZhbHVlIjozMDB9LCAiU0VRTiI6NzAwICwgIm1zZyI6IklUIFdPUktTISEiIH0=
Encrypted -> Base64: sD9f8LnxQrlOvTODLbzXPM5wWMk6+KnpmGiowTtKswGK80+yf9DyHjjiF94TwUpP/1V4f9KsHA7+1oAmBy12Dl8Dvk/ZclFvNeNrXSwCFlU=
Final encoded: sD9f8LnxQrlOvTODLbzXPM5wWMk6+KnpmGiowTtKswGK80+yf9DyHjjiF94TwUpP/1V4f9KsHA7+1oAmBy12Dl8Dvk/ZclFvNeNrXSwCFlU=
Base64 -> Encrypted: ⸮?_⸮⸮B⸮N⸮3⸮-⸮⸮<⸮pX⸮:⸮⸮⸮h⸮⸮;J⸮⸮⸮O⸮⸮⸮8⸮⸮⸮JO⸮UxҬ⸮ր&
Encrypted -> Original Msg:
Done...
正如您在上面看到的,解密工作不正常。虽然加密工作得很好(但我认为编码部分没有正确输出,因为它与Base64编码相同?(。
帮助将是伟大的,以解决这个问题!
首先用密钥和IV加密,然后编码到base64。在解码器端,解码base64,并使用相同的密钥和IV.进行解密
请注意,AES函数会更改IV,因此如果在同一函数中进行测试,则必须将其保持为常量并复制到可变函数。
为了简单起见,删除base64部分,只需尝试加密和解密:
int msglen = strlen(msg);
int msglen_padded = msglen + (N_BLOCK - ((msglen - 1) % 16));
byte cipher[msglen_padded];
byte msg_decoded[msglen_padded];
byte iv[N_BLOCK];
aes.iv_inc();
aes.set_IV(my_iv);
aes.get_IV(iv);
aes.do_aes_encrypt(msg, strlen(msg) + 1, cipher, key, 256, iv);//+1 to encrypt null
aes.set_IV(my_iv);
aes.get_IV(iv);
aes.do_aes_decrypt(cipher, cipher_len, msg_decoded, key, 256, iv);
if (aes.get_size())
{
msg_decoded[aes.get_size() - 1] = 0;
printf("%sn", msg);
}
然后,您需要将cipher
编码为base64,并在AES加密/解密之间解码回cipher
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 请解释"函数1(p1,p2,p3);"的输出
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 为什么我的代码在输出中增加了93天
- 如何从void函数输出字符串
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- AES加密到解密未正确输出
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 为什么这不是解密
- C++格式化输出问题
- 将值从二维数组输出到文本文件
- 仿射密码解密,输出大小写不同
- 尝试从 Visual Studios C++ 中的加密字符串输出解密字符串时出现逻辑错误
- 异或字符串加密/解密输出错误
- C++解密第13章(2004)JeffKent:Ifstream程序不返回预期的输出