Community post
C++ Tutorial: Caesar cipher | encryption and decryption (Very easy)
Hey!
Hope you will like this short tutorial. :)
Programs used:
IDE: http://www.eclipse.org/downloads/eclipse-packages/
Compiler: http://www.mingw.org/
Let's start!
We are looking for an encryption like this:
https://i.imgur.com/h3RIx7k.jpg
Therefore we need nothing special.
Only 3 variables.
https://i.imgur.com/8ZphUmi.png
We can also write the code to input our variables.
https://i.imgur.com/EruUOiE.png
Now we need a for-loop to go through every letter.
Therefore we declared the variable " inputLength " wich gives us the length of our input with:
https://i.imgur.com/FfHm54o.png
In our for-loop we are checking if the current charactere is alphanumeric.
https://i.imgur.com/2E1pkni.png
Now we are converting our upper case letters into lower case letters.
https://i.imgur.com/oLCtLHd.png
In our loop we need another for-loop wich is our displacement.
Inside there we check if the current letter is " z " and if it's true, then convert it to " a ".
That's because we don't want to run out of letters if we reach " z ".
We also add an else statement. In there we are just counting up our input variable.
https://i.imgur.com/d7PtTFj.png
For the decrypting we are doing almost the same, but instead of counting our input variable up, we are counting it down.
https://i.imgur.com/xjGkPbY.png
That's it! Hope you like it :)
Replies (2)
Yay. Caesar Cipher, one of most basic shift ciphers. Unfortunately this cipher is very, very weak especially for longer texts, because you can easily deduce which letter has the place of another.
Still - I like your post and fact that you implemented that in C++. Cheers!
Great that you like it :)