Custom Text Encoding

Hi All,

I'm making a data logger using Arduino Uno Rev3 and Adafruit datalogger shield.

I'm writing files to a SD card but I don't want them to read by anything other my program.

I'm thinking about writing my own text encoding so for example if I used an alphabet swap so A = Z to Z = A, then the string "hello world" would be swapped to "svool dliow".

I also want to be able to swap numbers so 0=9 to 9=0.

I will then write a program on my PC so the encoded files on the SD card can be decoded.

I understand that this "code" is easily cracked but I would like to know how to do this first before I move on to move complex stuff like real encoding and CRCs.

So does anyone know how to encode a string like this?

Cheers

Steve

megamef: So does anyone know how to encode a string like this?

The Romans about two thousand years ago did: http://en.wikipedia.org/wiki/Caesar_cipher

Yeah but apart from Caesar ciphers what have the Romans ever done for us?

This is what I'm looking for I'd just like some idea of how to implement this in Arduino.

megamef:
Yeah but apart from Caesar ciphers what have the Romans ever done for us?

I think “Arduino” is invented by Italians, which are the descendants of the Romans.

megamef:
This is what I’m looking for I’d just like some idea of how to implement this in Arduino.

If you need a complete example sketch providing encoding and decoding:

char CaesarCipher[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789abcdefghijklmnopqrstuvwxyz";

void enDecode(char* str)
// encode and decode is the same function
{
  for (int i=0;i<strlen(str);i++)
  {
    char* charPtr=strchr(CaesarCipher,str[i]);
    if (charPtr!=NULL)
    {
      int pos=charPtr-CaesarCipher;
      str[i]=CaesarCipher[strlen(CaesarCipher)-pos];
    }
  }
}

void setup()
{
  Serial.begin(9600); // Initialize Serial for debugging messages
  char text[]="The quick brown fox jumps over the lazy dog";
  Serial.print("Text:    ");Serial.println(text);
  enDecode(text); // encode
  Serial.print("Encoded: ");Serial.println(text);
  enDecode(text); // decode
  Serial.print("Decoded: ");Serial.println(text);
}

void loop() {
}

Encoding and decoding is actually the same, so if you call the “enDecode(char* str)” for an odd number of times, the string is encoded. And if you call the function an even number of times, the string is decoded.

Encoding and decoding are done “in place” of the RAM variable, so no extra buffer is needed.

Do you like it that way?

Thank you! That's just pure magic.

megamef:
Thank you! That’s just pure magic.

That’s simple programming logic which was already used more than 2000 years ago, when such things like ‘computers’ and ‘Arduino’ and ‘C programming’ not even had been invented.

Don’t underestimate the Roman inventions!
:wink: