Where is the problem in my code?

Hi my friend,

I'm newbie for arduino programming. I am trying for encrytion some string with xor algorithm. But when i uploaded my to arduino and after then open serial monitor i see nothing happen.

I don't find my problem where it is.

This is my code. I found it one website :

#include <SD.h>
void setup()

{

String str, encrypted, decrypted;

str ="write_anything";

encrypted = XOREncryption(str, 12); // storing the encrypted string
decrypted = XORDecryption(encrypted, 12);

Serial.println(encrypted);

}

String XOREncryption(String str, int key)
{
String enc("");
for (unsigned int i(0); i < str.length(); i++) // iterates through the string to encrypt
enc += str ^ key; // ^ - XOR operator in C++

  • return enc;*
    }
    String XORDecryption(String str, int key)
    {
  • String dec("");*
    for (unsigned int i(0); i < str.length(); i++)// iterates through the string to decrypt
    _ dec += str ^ key; // ^ - XOR operator in C++_
    * return dec;*
    }
    void loop()
    {

}
Thanks for your attention.
Best regards.

Serial.begin(9600) would be a good place to start.

Adding Serial.println("Hello world"); after that would be a good next move. It will verify that your basic connections is working, before you start worrying about your stoopid encryption.

Thank you so much michinyon. It is working very well with your help.

for (unsigned int i(0); i < str.length(); i++)// iterates through the string to decrypt
        dec += str ^ key; // ^ - XOR operator in C++

I think you want to xor the i'th character, not the string object!

I wouldn't call this encryption, its encoding. Encryption uses a secret key.