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
}
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]);
}
}
}
But then,this program works just fine.
There is something wrong in my for loop i guess.
Thanks though.
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.
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]);
}
}
}
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.
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.
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.
wireless Transmission -->Frequency Hopping used
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