C++ Program to Encrypt and Decrypt a String
Hello everyone!
Let’s discuss the string encryption and decryption and implement it in C++. Encrypting a string basically means changing it from one form to another i.e plain text to ciphertext. By changing it to a ciphertext, it has now transformed into a non-readable / non-understandable form.
Encrypted or encoded strings are most of the time used during transmission over the internet. By doing this it becomes a little bit safe to avoid the MITM(Man in the Middle) attacks and also prevent us from many more information leaks or data exposure.
Encryption and Decryption of a String in C++
There are various types of cipher for Encryption and Decryption such as :
- Caesar Cipher
- Monoalphabetic Cipher
- Homophonic Substitution Cipher
- Polygram Substitution Cipher
- Polyaphabetic Substitution Cipher
- Playfair Cipher
- Hill Cipher.
In this tutorial, we will see how to encrypt and decrypt a string using the Caesar cipher in C++. Caesar is one of the easiest and simplest encryption technique yet one of the weakest technique for the encryption of data. In this type of encrypting technique, each character in the string is replaced by a character which is some fixed number of positions down to it. The position is decided with the help of the key provided.
For example, if the key is 2 then we have to replace the character by another character that is 2 down to it.
Below I have shared the program for the same.
#include<iostream> #include<string.h> using namespace std; void encryptyfunc() { char text[100]; char temp; int i, key; cout <<"Enter a message to encrypt: " <<endl; cin >>text; cout <<"Enter key: " <<endl; cin >> key; for(i = 0; text[i] != '\0'; ++i){ temp = text[i]; //If the message to be encypted is in lower case if(temp >= 'a' && temp <= 'z'){ temp = temp + key; if(temp > 'z'){ temp = temp - 'z' + 'a' - 1; } text[i] = temp; } //If the message to be encypted is in upper case else if(temp >= 'A' && temp <= 'Z'){ temp = temp + key; if(temp > 'Z'){ temp = temp - 'Z' + 'A' - 1; } text[i] = temp; } } cout << "Encrypted message:" << text <<endl; } void decryptfunc() { char text[100]; char temp; int i, key; cout <<"Enter a message to decrypt: " <<endl; cin >>text; cout <<"Enter key: " <<endl; cin >> key; for(i = 0; text[i] != '\0'; ++i){ temp = text[i]; //If the message to be decypted is in lower case. if(temp >= 'a' && temp <= 'z'){ temp = temp - key; if(temp < 'a'){ temp = temp + 'z' - 'a' + 1; } text[i] = temp; } //If the message to be decypted is in upper case. else if(temp >= 'A' && temp <= 'Z'){ temp = temp - key; if(temp < 'A'){ temp = temp + 'Z' - 'A' + 1; } text[i] = temp; } } cout << "Decrypted message:" <<text << endl; } int main() { int choice; cout << "Choose any one !" <<endl; cout << "1.Encryption \t 2.Decryption"<<endl; cin >> choice; switch(choice) { case 1: encryptyfunc(); break; case 2: decryptfunc(); break; default:cout << "Please select valid option"; break; } return 0; }
Outputs:
For Encryption:
Enter a message to encrypt: Dog Enter key: 3
Encrypted message:Grj
For Decryption:
Enter a message to decrypt: Grj Enter key: 3
Decrypted message:Dog
I hope I have made my explanation clear and simple.
I would like to know if there is anything you didn’t understand please comment in the comment section.
Also, please mention if you have got any suggestions for me.
You may also visit:
Thank You.