BASE64 - not working

Hi, I need to implement XXTEA encryption in Arduino and send base64 encoded ecrypted data to serial port.

I'm using these libraries:

There is my code:

#include <XXTEA.h>
#include <Base64.h>
#include <string.h>

// Online XXTEA Encrypt - Online tools
// Online XXTEA Decrypt - Online tools

void setup() {
Serial.begin(9600);

XXTEA xxtea("SecretKey123");

char* s = "Data 123456789 ...";

Serial.print("INPUT: ");
Serial.println(s);

Serial.print("BASE64 INPUT: ");
char encoded1[base64_enc_len(strlen(s))];
base64_encode(encoded1, s, strlen(s));
Serial.println(encoded1);

Serial.print("BINARY INPUT: ");
xxtea.encrypt(s);
Serial.println(s);

Serial.print("BASE64 ENCRYPTED: ");
char encoded2[base64_enc_len(strlen(s))];
base64_encode(encoded2, s, strlen(s));
Serial.println(encoded2);

Serial.print("DECRYPTED OUTPUT: ");
xxtea.decrypt(s);
Serial.println(s);

Serial.print("BASE64 DECODED DECRYPTED OUTPUT: ");
char decoded[base64_dec_len(s, strlen(s))];
base64_decode(decoded, s, strlen(s));
Serial.println(decoded);
}

void loop() {}

But program doesn't work good - see attachement.

BASE64 INPUT is good.
DECRYPTED OUTPUT is good.
But base64 is wrong.

If I put key and BASE64 ENCRYPTED to Online XXTEA Decrypt - Online tools it fails.

What I do bad?

arduino.png

You have to declare storage space before you set a pointer to it, so you can't do this:
char* s = "Data 123456789 ...";

You should do something like this:
char s[] = "Data 123456789 ...";

Does the library come with examples or a tutorial ?

6v6gt:
You have to declare storage space before you set a pointer to it, so you can't do this:
char* s = "Data 123456789 ...";

Nonsense.

AWOL:
Nonsense.

Oops. In that case I retract my advice forthwith.

I use this for BASE64 encryption and it works ok. It is in a short test sketch that takes input from the serial monitor. Insure the buffers are large enough to handle the input. edit: Note baud rate is 115200. Insure the "No line ending" at the bottom of the serial monitor is changed to "Both NL & CR".

char b64Chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
char tBuf[64];
char b64Buf[64];
byte bufCount;

void setup() {
  Serial.begin(115200);
}

void loop() {
  if(Serial.available()) {
    char ch = Serial.read();
    
    if(ch == '\n') {
      Serial.println(tBuf);
      doB64();
      bufCount = 0;
    }
    else if(ch != '\r' && bufCount < 63) {
      tBuf[bufCount] = ch;
      bufCount++;  
      tBuf[bufCount] = 0;
    }
  }
}

void doB64() {
  byte remainder = bufCount%3;
  byte b64Count = bufCount/3;
  
  memset(b64Buf,0,64);
  
  for(int x=0;x<b64Count;x++) {
    encodeblock((byte*)&tBuf[x*3],(byte*)&b64Buf[x*4],3);
  }    

  if(remainder) {
    encodeblock((byte*)&tBuf[b64Count*3],(byte*)&b64Buf[b64Count*4],remainder);
  }

  Serial.println(b64Buf);
}

void encodeblock( byte *in, byte *out, int len )
{
    out[0] = (byte) b64Chars[ (in[0] >> 2) ];
    out[1] = (byte) b64Chars[ (in[0] & 0x03) << 4 | (in[1] & 0xf0) >> 4 ];
    out[2] = (byte) (len > 1 ? b64Chars[ (in[1] & 0x0f) << 2 | (in[2] & 0xc0) >> 6 ] : '=');
    out[3] = (byte) (len > 2 ? b64Chars[ (in[2] & 0x3f) ] : '=');
}

What you can't do is write to s, which xxtea.encrypt(s) might be doing.

Also, you seem to be decoding s - base64_decode(decoded, s, strlen(s)); - which of course was never encoded to begin with.

6v6gt:
You have to declare storage space before you set a pointer to it, so you can't do this:
char* s = "Data 123456789 ...";

You should do something like this:
char s[] = "Data 123456789 ...";

Does the library come with examples or a tutorial ?

It doesn't work. See attachment.
It write forever these binary chars to serial port.

Yes, there are examples:
https://github.com/alessandro1105/XXTEA_Arduino_Library/blob/master/Arduino%20XXTEA/sketch_base64/sketch_base64.ino

https://github.com/alessandro1105/XXTEA_Arduino_Library/blob/master/Arduino%20XXTEA/test_libreria_xxtea/test_libreria_xxtea.ino

1.png

It could be you are overflowing an array. The encoded string will be longer than the original string. Allow room in the array for the extra characters.

char s[64]  = "Data 123456789 ...";

SurferTim:
It could be you are overflowing an array. The encoded string will be longer than the original string. Allow room in the array for the extra characters.

char s[64]  = "Data 123456789 ...";

My code:

#include <XXTEA.h>
#include <Base64.h>
#include <string.h>

// https://www.tools4noobs.com/online_tools/xxtea_encrypt/
// https://www.tools4noobs.com/online_tools/xxtea_decrypt/
// https://github.com/alessandro1105/XXTEA_Arduino_Library
// https://github.com/adamvr/arduino-base64/blob/master/examples/base64/base64.ino

void setup() {  
  Serial.begin(9600);
  
  XXTEA xxtea("SecretKey123");
  
  char s[255]  = "Data 123456789 ...";

  Serial.print("INPUT: ");
  Serial.println(s);
  
  Serial.print("BASE64 INPUT: ");
  char encoded1[base64_enc_len(strlen(s))];
  base64_encode(encoded1, s, strlen(s)); 
  Serial.println(encoded1);
  
  Serial.print("BINARY INPUT: ");
  xxtea.encrypt(s);
  Serial.println(s);
  
  Serial.print("BASE64 ENCRYPTED: ");
  char encoded2[base64_enc_len(strlen(s))];
  base64_encode(encoded2, s, strlen(s)); 
  Serial.println(encoded2);
  
  Serial.print("DECRYPTED OUTPUT: ");
  xxtea.decrypt(s);  
  Serial.println(s);
  
  Serial.print("BASE64 DECODED DECRYPTED OUTPUT: ");
  char decoded[base64_dec_len(s, strlen(s))];
  base64_decode(decoded, s, strlen(s));
  Serial.println(decoded);
}

void loop() {}

It still doesn't work.

abc.png

Did you see my post?
You decode with this:
base64_decode(decoded, s, strlen(s));

But encode with this:
base64_encode(encoded2, s, strlen(s));

encoded2 is the base 64 encoded string, s is the original untouched string.

The serial output in your posted image labeled as BASE 64 INPUT is your text BASE 64 encoded, so your problem is not BASE64 encoding. It is with the xxtea_encrypt section.

#include <XXTEA.h>
#include <Base64.h>
#include <string.h>

// https://www.tools4noobs.com/online_tools/xxtea_encrypt/
// https://www.tools4noobs.com/online_tools/xxtea_decrypt/

void setup() {
  Serial.begin(9600);

  XXTEA xxtea("SecretKey123");

  char s[32]  = "Data 123456789 ...";

  Serial.print(F("    INPUT: "));
  Serial.println(s);

  Serial.print(F("   BASE64: "));
  char encoded1[base64_enc_len(strlen(s)) + 1];
  base64_encode(encoded1, s, strlen(s));
  Serial.println(encoded1);

  Serial.print(F("ENCRYPTED: "));
  xxtea.encrypt(s);
  Serial.println(s);

  Serial.print(F("-> BASE64: "));
  char encoded2[strlen(s) + 1];
  base64_encode(encoded2, s, strlen(s));
  Serial.println(encoded2);

  Serial.print(F("<- BASE64: "));
  char decoded[base64_dec_len(encoded2, strlen(encoded2)) + 1];
  base64_decode(decoded, encoded2, strlen(encoded2));
  Serial.println(decoded);

  Serial.print(F("DECRYPTED: "));
  xxtea.decrypt(decoded);
  Serial.println(decoded);
}

void loop() {}
    INPUT: Data 123456789 ...
   BASE64: RGF0YSAxMjM0NTY3ODkgLi4u
ENCRYPTED: ó¢]tº"�è�ßG$ݯµìÕĽ1!q,B
-> BASE64: 86JddLoigeiB30ck3a+17NXEvTEhcSxC
<- BASE64: ó¢]tº"�è�ßG$ݯµìÕĽ1!q,B
DECRYPTED: Data 123456789 ...