Encryption is a familiar sounding word which means to convert readable data in such a form that it becomes un-understandable or un-meaningful. It is employed almost everywhere where any confidential data is needed to be kept or transferred.
Encryption goes hand in hand with decryption which means to convert un-meaningful encrypted data to its original meaningful form.
Here in this article we are going to design two functions, one for encryption and other for decryption, to illustrate the basic concept of encryption and decryption.
Please note that the example program provided in this article is for illustrative purpose only, there are a few limitations in the program which limits its practical use.
How encryption and decryption works?
The main concept behind encryption is to convert the readable data into something which looks un-meaningful to us. It could be achieved in various ways but the simplest one is to change the ASCII code of the data.
Ex.
#include<iostream.h> void main(void) { int i; char str[20]="I like C++"; for(i=0;str[i]!='\0';i++) str[i]+=10; cout<<"Encrypted:\n"; cout<<str; cout<<endl<<endl; for(i=0;str[i]!='\0';i++) str[i]-=10; cout<<"Decrypted:\n"; cout<<str; cout<<endl; }
OUTPUT:
Encrypted: S*vsuo*M55
Decrypted: I like C++ Press any key to continue
In the above example we increased the ASCII code of each character of the string by 10, notice how un-meaningful the encrypted data looks!
While decrypting we need to reverse the process by decreasing the ASCII code of each character by 10, which would give us the original data.
This concept will form the basis of encryption and decryption in for our program which is listed below:
// A simple c++ program to // illustrate basic encryption // and decryption #include<iostream.h> #define FACTOR 95 void encrypt(char *); void decrypt(char *); void main(void) { char str[20]="I like C++"; cout<<"Original String:\n"; cout<<str; cout<<endl<<endl; encrypt(str); cout<<"After Encryption:\n"; cout<<str; cout<<endl<<endl;; cout<<"After Decryption:\n"; decrypt(str); cout<<str; cout<<endl; } void encrypt(char *str) { while(*str!='\0') { *str+=FACTOR; str++; } } void decrypt(char *str) { while(*str!='\0') { *str-=FACTOR; str++; } }
Good-Bye!
Related Articles: