Skip to content

Encryption and decryption issue #1

Open
@xswxm

Description

@xswxm

Hi, bro, i used your lib to encrypt a msg first and then decrypt it, finding that part of the msg are not decrypted correctly.

Output:

Booting...
Let's encrypt:
 IV b64: MDAwMDAwMDAwMDAwMDAwMA==
 Message: {"data":{"value":300}, "SEQN":700 , "msg":"IT WORKS!!" }
 Message in B64: eyJkYXRhIjp7InZhbHVlIjozMDB9LCAiU0VRTiI6NzAwICwgIm1zZyI6IklUIFdPUktTISEiIH0=
 The lenght is:  76
Encryption done!
Cipher size: 80
Encrypted data in base64: Jdq1ko6TKPewmlSdn3zTaEP2yGWw10UXdansMHHuYbZdSDFWWH5cTq6LZO5Qey1JuY2ppygYicILzp8haWSS75oigJSZZo9uPXxuTwEO2mQ=
Done...
 Plain in B64: 蟢���6D&.HxP�<bHVlIjozMDB9LCAiU0VRTiI6NzAwICwgIm1zZyI6IklUIFdPUktTISEiIH0�����
 Plain size: 80
 Decrypted data in base64: 﨩�����悄�縧ue":300}, "SEQN":700 , "msg":"IT WORKS!!" }���

Code:

#include "AES.h"
#include "base64.h"

AES aes;

void setup() {
  Serial.begin(115200);
  Serial.println("\nBooting...");

  char b64data[200];
  byte cipher[1000];
  byte iv [N_BLOCK] ;

  Serial.println("Let's encrypt:");
  // Our AES key. Note that is the same that is used on the Node-Js side but as hex bytes.
  byte key[] = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 };

  // The unitialized Initialization vector
  //byte my_iv[N_BLOCK] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  byte my_iv[N_BLOCK] = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 };

  // Our message to encrypt. Static for this example.
  String msg = "{\"data\":{\"value\":300}, \"SEQN\":700 , \"msg\":\"IT WORKS!!\" }";

  aes.set_key( key , sizeof(key));  // Get the globally defined key

  // Print the IV
  base64_encode( b64data, (char *)my_iv, N_BLOCK);
  Serial.println(" IV b64: " + String(b64data));

  Serial.println(" Message: " + msg );

  int b64len = base64_encode(b64data, (char *)msg.c_str(), msg.length());
  Serial.println (" Message in B64: " + String(b64data) );
  Serial.println (" The lenght is:  " + String(b64len) );

  // For sanity check purpose
  //base64_decode( decoded , b64data , b64len );
  //Serial.println("Decoded: " + String(decoded));

  // Encrypt! With AES128, our key and IV, CBC and pkcs7 padding
  aes.do_aes_encrypt((byte *)b64data, b64len , cipher, key, 128, my_iv);

  Serial.println("Encryption done!");

  Serial.println("Cipher size: " + String(aes.get_size()));

  b64len = base64_encode(b64data, (char *)cipher, aes.get_size() );
  Serial.println ("Encrypted data in base64: " + String(b64data) );

  Serial.println("Done...");

  byte plain[1000];
  b64len = base64_decode(b64data, b64data, b64len);
  aes.do_aes_decrypt((byte *)b64data, b64len, plain, key, 128, my_iv);
  
  base64_decode(b64data, (char *)plain, aes.get_size());
  Serial.println (" Plain in B64: " + String((char *)plain) );
  Serial.println(" Plain size: " + String(aes.get_size()));
  Serial.println (" Decrypted data in base64: " + String(b64data) );
}

void loop() {
  // put your main code here, to run repeatedly:
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions