A Simple Encryption and Decryption Program [Cryptography]

In this post you can learn the basic concept of Cryptography. This program accepts a string as the input and performs encryption with the key provided by the programmer. This is a basic Encryption method so it just involves changing each single character of the String with some simple algebric operations with the key. It can be added with each and every single character of the string, so can be subtracted or divided. But note Your have to apply reverse method.

Sample Outputs:

The Code:

#include<iostream>
#include<cstring>
using namespace std;
void encrypt(string *s,int key)
{
int i=0;
while((*s)[i]!=NULL)
{
(*s)[i]=(int)(*s)[i]-key;
//cout<<(*s)[i]<<endl; //Remove The Comments to perform step by step analysis !
i++;
}
}
void decrypt(string *s,int key)
{
int i=0;
while((*s)[i]!=NULL)
{
(*s)[i]=(int)(*s)[i]+key;
//cout<<(*s)[i]<<endl; //Remove The Comments to perform step by step analysis !
i++;
}
}
int main()
{
string s;
int key=12;
cout<<"Enter The String: ";cin>>s;
cout<<"Encrypted String is: ";
encrypt(&s,key);
cout<<s<<endl;
cout<<"Decrypted String is: ";
decrypt(&s,key);
cout<<s<<endl;
}

Description:
Lets say you have given “blabla” as input and here the key is 12. So according to the above logic, for encryption it will basically subtract the key from each single character and provide the output string.
i.e ‘b’-key, ‘l’-key and so on.. and for decryption it will just add the key. The actual process of encryption is a lot more complicated work. it takes 128 bit key and performs XOR or OR operations with it !
Stay tuned with us, we gonna publish them all 🙂

Related posts

Leave a Comment