trying a light crypto sketch


hi guys,

i´ve been reading forum few weeks since got arduino... :) , i´m stucked in place none has asked before... :))


i got problem , dont know i´m doing wrong.. me appreciate.

i´m trying send on bluetooth message little obfuscation/crypto in it.

it´s data , have many repetition on it..."0000,0,00,0000,1,00" tried rot47 fast there repetition on message, i´ve created little , down counter add data , after encode base64...

i´m satisfied result, i´m having little problem in end of decrypted message...

i message correctly in end of message decrypted there few characters more wish... believe string , null terminator problem, not sure , don't know how fix it...


the functions encrypt , decrypt must work strings since doesnt know size of message...

here code:


code: [select]

#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 9...
  count=countstart;


// trying scramble message using random number (this number added begginng of message 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 digit used scramble message
  for (int i=1; i<=tamanho; i++){
    buffer[i] = encoded[i-1];
  }


  serial.print("sent:"); serial.println(buffer);

}



i´m using base64 here:https://github.com/adamvr/arduino-base64

thanks in advance!

so you're testing arduino serial monitor? have set "line ending" right down @ bottom of window? suspect should change "none."


Arduino Forum > Using Arduino > Programming Questions > trying a light crypto sketch


arduino

Comments