AES加密解密无法正确输出

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AES加密解密无法正确输出相关的知识,希望对你有一定的参考价值。

嘿所有我正在努力弄清楚如何解密加密文本的解密顺序。

这是我的草图代码:

#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("
Booting...");  

    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,因此如果要在同一函数中进行测试,则必须将其保持为常量并复制到mutable。

为简单起见,删除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("%s
", msg);
}

然后你想将cipher编码为base64,并在AES加密/解密之间解码回cipher

以上是关于AES加密解密无法正确输出的主要内容,如果未能解决你的问题,请参考以下文章

正确使用AES对称加密

为什么我的C#代码在使用AES CBC 128位加密时不产生与以下网站相同的输出:https://cryptii.com/pipes/aes-encryption

如何正确使用来自 mbedtls 的 aes cbc api

AES 加密在 iOS 13.4 上无法正常工作

mbedtls 和 openssl 之间的不同 AES-256 加密输出

Java AES解密 中文乱码