Ciphering operation on an array

I have written a code which shifts the pressed key by +13 characters.
i.e. If i press “A” on my keyboard it will serially print “N” on serial monitor ,if i press “B” it will print “O” and so on.

How will i implement the same operation on a character Array. Like " char array[6]=“Hello”; "

Cipher Program:

int inByte = 0;             // serial input and output character

void setup() {

  // put your setup code here, to run once:

  Serial.begin(9600);   // initialize the serial port at 9600 baud


  } /* setup */
  
  
  
void loop() {

  

  if (Serial.available() > 0)        // if you have data input

         {

         inByte = Serial.read();        // read one byte of input

         // A to M get converted to N to Z

         if (inByte >= 'A' && inByte <= 'M')

            {

            inByte += 13;

            } /* if upper case A-M */

         // N to Z get converted to A to M

         else if (inByte >= 'N' && inByte <= 'Z')

            {

            inByte -= 13;

            } /* if upper case N-Z */

         // Lower case a to m get converted to n to z

         else if (inByte >= 'a' && inByte <= 'm')

            {

            inByte += 13;

            } /* if lower case a-m */

         // Lower case n to z get converted to a to m

         else if (inByte >= 'n' && inByte <= 'z')

            {

            inByte -= 13;

            } /* if lower case n-z */

         Serial.write(inByte);   // write the encrypted character back

         } // if Serial.available() > 0

}

How will i implement the same operation on a character Array.

One character at a time.
Iterate through the array with a for loop changing one character each time through the loop:-

for(int i=0; i<6;i++){
array[i]=array[i] + 13;
}

Ah, the famous ROT-13 still alive :wink:

A bit more cryptic with half the comparisons (untested):

char lower = inByte | 0x20;
if (lower >= 'a' && lower <= 'm') inByte += 13;
...

Thanks a lot for your suggestions DrDiettrich and Grumpy_Mike

Ive implemented this code for ROT13,but problem i am facing is the lower half of the array,i.e the last 13 characters (n to z & N to Z) are not rotated,instead some garbage value gets printed.

Ive attached the Serial output monitor below,please check

char array[27]="abcdefghijklmnopqrstuvwxyz";
char inByte;
int i=0;
void setup() {

  // put your setup code here, to run once:

  Serial.begin(9600);   // initialize the serial port at 9600 baud


  } /* setup */

void loop() {

          inByte = array[i];
         
          if (inByte >= 'A' && inByte <= 'M')
         {
           
          for( i=0; i<26;i++){
          array[i]=array[i] + 13;
          
          Serial.print(array[i]);
         }
         
         }
         
         else if (inByte >= 'N' && inByte <= 'Z')
         {
           
          for( i=0; i<26;i++){
          array[i]=array[i] + 13;
          Serial.print(array[i]);
         }
         
         }
         
        else if (inByte >= 'a' && inByte <= 'm')
         {
           
          for( i=0; i<26;i++){
          array[i]=array[i] + 13;
          Serial.print(array[i]);
         }
         
         }
         
        
         
        else if (inByte >= 'n' && inByte <= 'z')
         {
           
          for( i=0; i<26;i++){
          array[i]=array[i] + 13;
          Serial.print(array[i]);
         }
         
         } 
}

Take a look at the ASCII table and you will understand why :slight_smile:

lg, couka

But then,this program works just fine.
There is something wrong in my for loop i guess.

Thanks though. :slight_smile:

int inByte = 0;             // serial input and output character

void setup() {


  Serial.begin(9600);     
  } 

void loop() {


  if (Serial.available() > 0)       

         {

         inByte = Serial.read();        // read one byte of input

         // A to M get converted to N to Z

         if (inByte >= 'A' && inByte <= 'M')

            {

            inByte += 13;

            } /* if upper case A-M */

         // N to Z get converted to A to M

         else if (inByte >= 'N' && inByte <= 'Z')

            {

            inByte -= 13;

            } /* if upper case N-Z */

         // Lower case a to m get converted to n to z

         else if (inByte >= 'a' && inByte <= 'm')

            {

            inByte += 13;

            } /* if lower case a-m */

         // Lower case n to z get converted to a to m

         else if (inByte >= 'n' && inByte <= 'z')

            {

            inByte -= 13;

            } /* if lower case n-z */

         Serial.write(inByte);   // write the encrypted character back

         } // if Serial.available() > 0

}  /* loop */

Again, search for the ASCII table and try it yourself. You won’t end up at a latin character if you go 13 steps, starting at z. There is fundamental difference between the snippets from Grumpy_Mike and DrDiettrich.
If someone solves a problem for you, at least try to understand how they did it. We are here to teach each other. :slight_smile:

lg, couka

I changed it successfully couka. :slight_smile:

Can you please give me a hint on how will i do it if i want to this to happen through Serial.read().
Characters recieved from Keyboards must be stored in an array and that array must be Ciphered.

Thank You.

Running Code:

char array[27]="abcdefghijklmnopqrstuvwxyz";
char inByte;
int i=0;
void setup() {

  // put your setup code here, to run once:

  Serial.begin(9600);   // initialize the serial port at 9600 baud


  } /* setup */

void loop() {

          inByte = array[i];
          
          if (inByte >= 'A' && inByte <= 'M')
         {
           
          for( i=0; i<13;i++){
          array[i]=array[i] + 13;
          
          Serial.print(array[i]);
         }
         
         }
         
        
         
        else if (inByte >= 'a' && inByte <= 'm')
         {
           
          for( i=0; i<13;i++){
          array[i]=array[i] + 13;
          Serial.print(array[i]);
         }
         
         }
         
          else if (inByte >= 'N' && inByte <= 'Z')
         {
           
          for( i=13; i<27;i++){
          array[i]=array[i] - 13;
          Serial.print(array[i]);
         }
         
         }
         
        
         
        else if (inByte >= 'n' && inByte <= 'z')
         {
           
          for( i=13; i<27;i++){
          array[i]=array[i] - 13;
          Serial.print(array[i]);
         }
         
         } 
         
}

Please learn more about arrays, see e.g. Learning → Reference → arrays.

Untested:

char array[27]="abcdefghijklmnopqrstuvwxyz";

void rot13(int *array; int maxIndex) {
    for (int i = 0; i <= maxIndex; ++i) {  
       char inByte = array[i];
       if (!inByte) return; //end of string reached
          
        if (inByte >= 'A' && inByte <= 'M')
            inByte +=13;
        else if (inByte >= 'a' && inByte <= 'm')
            inByte +=13;
        else if (inByte >= 'N' && inByte <= 'Z')
            inByte -=13;
        else if (inByte >= 'n' && inByte <= 'z')
            inByte -=13;

        array[i] = inByte;
     } 
}

void setup() {
  Serial.begin(9600);
  Serial.println("Before");
  Serial.println(array);
  rot13(array, 25);
  Serial.println("After");
  Serial.println(array);
}

void loop() {
}

You can omit the maxIndex parameter, if all your strings are zero terminated (recommended).

XOR might be a friend?

XOR is not applicable, because the required increment is 13.

I was giving them a hint at what they could look at next to allow them to explore other options and learn. I get the original question was to "shift by 13", but seeming as the XOR operation is used extensively in ciphering, the OP having a go at XORing a string with a "passkey" may be a nice exercise to aim for.

If his aim was serious encryption, he wouldn't use ROT13. At least, that's what I hope.

lg, couka

man ROT13. This is a special (simple, symmetric) encryption standard, often used in e-mail to post a riddle and solution at the same time. The answer is encrypted in ROT13, so that it is not immediately readable. When the receiver resigns or has found a solution, he can ROT13 the entire mail to read the solution.

So if this were for that application, a bash script and or/python would be a far simpler way to do it that an external MCU.

I was assuming the OP was learning about encryption and has just had a go at the more basic version...a Caeser type shift.

The next step would be looking at basic uses of "pass keys" such as XORing some bits right?

I don’t see how what you are saying is related to the topic.

lg, couka

Well,this all concerns my school Project,
1)I write text on using a PS2 Keyboard and store it in an array.
2)Cipher it,store this ciphered text in another array
3)Add a KEY array.

  1. wireless Transmission -->Frequency Hopping used

  2. Ask for KEY, if key correct --> Real text
    key incorrect---> Encrypted text

So, for now ROT13 was a good choice,for explanation of cipher with easy evidence (Ciphering back gives real message).

But,i further look to build my own Enigma machine. That would be enticing :slight_smile: