AES加密解密无法正确输出

时间:2018-11-04 05:36:58

标签: c++ c encryption arduino arduino-esp8266

嘿,我最困难的时间是试图弄清楚如何解密加密文本。

这是我的草图代码:

#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...

如您在上面看到的,解密无法正常工作。加密工作得很好(但是我认为Encoding部分由于与Base64编码相同而无法正确显示出来?)。

为解决此问题提供帮助!

1 个答案:

答案 0 :(得分:0)

首先使用密钥和IV进行加密,然后编码为base64。在解码器端,对base64进行解码,并使用相同的密钥和IV进行解密。

请注意,AES函数会更改IV,因此,如果要在同一函数中进行测试,则必须保持不变并复制到可变对象。

为简单起见,删除base64部分,然后尝试加密和解密:

Uncaught TypeError: WebSocketClient is not a constructor

然后您要在AES加密/解密之间将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\n", msg); } 编码为base64,然后解码回cipher