trying a light crypto sketch

Hi guys,

I´ve been reading the forum for few weeks since I got my arduino… :slight_smile: and now I´m stucked in a place that none has asked before… :))

I got a problem and I dont know what I´m doing wrong… Is anyone could help me I will appreciate.

I´m trying to send over Bluetooth a message with a little obfuscation/crypto in it.

It´s a data and have many repetition on it…“0000,0,00,0000,1,00” so I tried the rot47 but we get so fast that there are repetition on the message, so I´ve created a little up and down counter to add to the data and after that I encode with base64…

I´m satisfied with the result, but I´m having a little problem in the end of the decrypted message…

I get the message correctly but sometimes in the end of the message decrypted there are some few characters more than I wish… I believe that is a string and null terminator problem, but I not sure and don’t know how to fix it…

the functions to encrypt and decrypt must work with strings since I doesnt know the size of the message…

Here is the code:

#include <Base64.h>

unsigned long m_w = 7;
unsigned long m_z = 3; 


char *rot47(char *s){
  char *p = s;
  while(*p) {
    if(*p >= '!' && *p <= 'O')
      *p = ((*p + 47) % 127);
    else if(*p >= 'P' && *p <= '~')
      *p = ((*p - 47) % 127);
    p++;
  }
  return s;
}

unsigned long getRandom() 
{
  m_z = 36969L * (m_z & 65535L) + (m_z >> 16);
  m_w = 18000L * (m_w & 65535L) + (m_w >> 16);
  return (m_z << 16) + m_w;  /* 32-bit result */
}


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

void loop() {

  Serial.println("Enter message");
  while (Serial.available () < 1)  {;}  
  Serial.println("ok");
  delay(300);
  String toencrypt = Serial.readString();
  encrypt_send_bluetooth(toencrypt);



  Serial.println("Enter encrypted message");
  while (Serial.available () < 2)  {;}  //espera ver se confirma ou reseta...
  Serial.println("ok");
  delay(300);
  String todecrypt = Serial.readString();
  String decoded;
  decrypt_bluetooth(todecrypt, &decoded );
  Serial.print("Message:");Serial.println(decoded);
  delay (5000);
}



char decrypt_bluetooth(String received, String *decoded1){
  
  char dados[10];
  int count;





  int receivedsize = received.length()+1;
  char charBuf[receivedsize];


  received.toCharArray(charBuf,receivedsize);


  int input2Len = sizeof(charBuf);
  int decodedLen = base64_dec_len(charBuf, input2Len);
  char decoded[decodedLen];
  base64_decode(decoded, charBuf, input2Len);

  rot47(decoded);

  *decoded1 = decoded;
}


void encrypt_send_bluetooth(String toencrypt){
  
  int toencryptsize = toencrypt.length()+1;
  char dados[toencryptsize];
  toencrypt.toCharArray(dados,tamanhorecibido);

  int countup=1;
  int countdown=0;
  int count;
  int countstart;
  countstart = (getRandom()% 10); //from 0 to 9...
  count=countstart;


// trying to scramble the message using a random number (this number is added to the begginng of the message to descrable after

  int inputLen = sizeof(dados);
  

  rot47(dados); //do rot47
  int encodedLen = base64_enc_len(inputLen);
  char encoded[encodedLen];
  base64_encode(encoded, dados, inputLen); 

  int  tamanho =  sizeof(encoded);
  char buffer[tamanho];

  buffer[0] = c; //put in front of message the digit used to scramble the message
  for (int i=1; i<=tamanho; i++){
    buffer[i] = encoded[i-1];
  }


  Serial.print("Sent:"); Serial.println(buffer);

}

I´m using the base64 from here:GitHub - adamvr/arduino-base64: A base64 library for the arduino platform, written in C

Thanks in advance!

So you’re testing with the Arduino serial monitor? What do you have set for “line ending” right down at the bottom of the window? I suspect that you should change this to “none.”

Hi, I´ve already changed it... including trying another tool as term pro... but I get sometimes garbage after the end of the message...

I know that is something related to the code, but I don´t find where the problem is...

But thanks for you tip! :slight_smile: