Encryption and Decryption
Encryption and Decryption
Encryption and Decryption
string
In this example, you will learn simple C++ program to encrypt and decrypt the
string using two different encryption algorithms i.e. Caesar Cypher and RSA.
Similarly, for decrypting a string, key-value ‘2’ is subtracted from the ASCII value of the
characters.
#include <iostream>
int main()
int i, x;
char str[100];
cin >> x;
//using switch case statements
switch(x)
case 1:
break;
case 2:
break;
default:
return 0;
Output
#Encrypting
#Decrypting
Explanation
In the above program, if you change the value of key then the encrypted value will be
different.
RSA algorithm is bit complex than Ceaser Cypher. It involves the use of public and
private key, where the public key is known to all and used for encryption.
On the other hand, Private key is only used to decrypt the encrypted message.
2: Encrypting Message
Messages are encrypted using the Public key generated and is known to all.
C = M ^ n( mod n )
3: Decrypting Message
M = C ^ d ( mod n )
You might be wondering how to write a source code for this program.
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<string.h>
char msg[100];
void encryption_key();
void encrypt();
void decrypt();
int main()
cin >> x;
flag = prime(x);
if(flag == 0)
exit(0);
}
cin >> y;
flag = prime(y);
if(flag == 0 || x == y)
exit(0);
m[i] = msg[i];
n = x * y;
t = (x - 1) * (y - 1);
encryption_key();
cout << "\nPOSSIBLE VALUES OF e AND d ARE\n";
encrypt();
decrypt();
return 0;
int i;
j = sqrt(pr);
if(pr % i == 0)
return 0;
return 1;
}
//function to generate encryption key
void encryption_key()
int k;
k = 0;
if(t % i == 0)
continue;
flag = prime(i);
e[k] = i;
flag = cd(e[k]);
if(flag > 0)
d[k] = flag;
k++;
if(k == 99)
break;
long int k = 1;
while(1)
k = k + t;
if(k % a == 0)
return(k/a);
void encrypt()
i = 0;
len = strlen(msg);
while(i != len)
pt = m[i];
pt = pt - 96;
k = 1;
k = k * pt;
k = k % n;
temp[i] = k;
ct= k + 96;
en[i] = ct;
i++;
en[i] = -1;
void decrypt()
i = 0;
while(en[i] != -1)
ct = temp[i];
k = 1;
k = k * ct;
k = k % n;
pt = k + 96;
m[i] = pt;
i++;
m[i] = -1;
cout << "\n\nTHE DECRYPTED MESSAGE IS\n";
Output
Caesar Cipher in Cryptography
Difficulty Level : Easy
Last Updated : 12 Aug, 2019
The Caesar Cipher technique is one of the earliest and simplest method of encryption
technique. It’s simply a type of substitution cipher, i.e., each letter of a given text is
replaced by a letter some fixed number of positions down the alphabet. For example with
a shift of 1, A would be replaced by B, B would become C, and so on. The method is
apparently named after Julius Caesar, who apparently used it to communicate with his
officials.
Thus to cipher a given text we need an integer value, known as shift which indicates the
number of position each letter of the text has been moved down.
The encryption can be represented using modular arithmetic by first transforming the
letters into numbers, according to the scheme, A = 0, B = 1,…, Z = 25. Encryption of a
letter by a shift n can be described mathematically as.
Examples :
Text : ABCDEFGHIJKLMNOPQRSTUVWXYZ
Shift: 23
Cipher: XYZABCDEFGHIJKLMNOPQRSTUVW
Text : ATTACKATONCE
Shift: 4
Cipher: EXXEGOEXSRGI
Algorithm for Caesar Cipher:
Input:
1. A String of lower case letters, called Text.
2. An Integer between 0-25 denoting the required shift.
Procedure:
Traverse the given text one character at a time .
For each character, transform the given character as per the rule, depending on
whether we’re encrypting or decrypting the text.
Return the new string generated.
Program that receives a Text (string) and Shift value( integer) and returns the encrypted
text.
C++
Java
Python
C#
PHP
filter_none
edit
play_arrow
brightness_4
#include <iostream>
if (isupper(text[i]))
else
}
return result;
int main()
string text="ATTACKATONCE";
int s = 4;
return 0;
Output:
Text : ATTACKATONCE
Shift: 4
Cipher: EXXEGOEXSRGI
How to decrypt?
We can either write another function decrypt similar to encrypt, that’ll apply the given
shift in the opposite direction to decrypt the original text. However we can use the cyclic
property of the cipher under modulo , hence we can simply observe
Cipher(n) = De-cipher(26-n)
Hence, we can use the same function to decrypt, instead we’ll modify the shift value such
that shift = 26-shift (Refer this for a sample run in C++).
This article is contributed by Ashutosh Kumar. If you like GeeksforGeeks and would
like to contribute, you can also write an article and mail your article to
contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main
page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more
information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory
concepts for SDE interviews with the CS Theory Course at a student-friendly price and
become industry ready.
Caesar Cipher Algorithm Program in C/C++
BY JAZIB
We will use C++ to write this algorithm due to the standard template library
support. Hence, we will write the program of Caesar Cipher algorithm in C++,
although, it’s very similar to C.
Encryption
INPUT:
line 1: key (key)
line 2: message (s)
OUTPUT:
line 1: Encrypted message (t)
Decryption
INPUT:
line 1: key (key)
line 2: message (s)
OUTPUT:
line 1: decrypted message (t)
1.#include <iostream>
2.#include <string>
3.using namespace std;
4.char caesar(char);
5.int main()
6.{
7. string input;
8. do
9. {
10. cout << "Enter cipertext and press enter to continue."
<< endl;
11. cout << "Enter blank line to quit." << endl;
12. getline(cin, input);
13. string output = "";
14. for (int x = 0; x < input.length(); x++)
15. {
16. output += caesar(input[x]);
17. }
18. cout << output << endl;
19. }
20. while (!input.length() == 0);
21. } //end main
22.
23. char caesar(char c)
24. {
25. if (isalpha(c))
26. {
27. c = toupper(c); //use upper to keep from having to use
two seperate for A..Z a..z
28. c = (((c - 65) + 13) % 26) + 65;
29. }
30. //if c isn't alpha, just send it back.
31. return c;
32. }
Output:
$ g++ CaesarCipher.cpp
$ a.out
Enter cipertext and press enter to continue.
Enter blank line to quit.
Sanfoundry
FNASBHAQEL
Enter cipertext and press enter to continue.
Enter blank line to quit.
------------------
(program exited with code: 0)
Press return to continue
Process
In order to encrypt a plaintext letter, the sender positions the sliding ruler
underneath the first set of plaintext letters and slides it to LEFT by the number of
positions of the secret shift.
The plaintext letter is then encrypted to the ciphertext letter on the sliding ruler
underneath. The result of this process is depicted in the following illustration for
an agreed shift of three positions. In this case, the plaintext ‘tutorial’ is encrypted
to the ciphertext ‘wxwruldo’. Here is the ciphertext alphabet for a Shift of 3 −
On receiving the ciphertext, the receiver who also knows the secret shift,
positions his sliding ruler underneath the ciphertext alphabet and slides it to
RIGHT by the agreed shift number, 3 in this case.
He then replaces the ciphertext letter by the plaintext letter on the sliding ruler
underneath. Hence the ciphertext ‘wxwruldo’ is decrypted to ‘tutorial’. To decrypt
a message encoded with a Shift of 3, generate the plaintext alphabet using a
shift of ‘-3’ as shown below −
Input: tutorial.
Output: wxwruldo
For decryption
Input: wxwruldo
Output: tutorial
For encryption
Begin
For i = 0 to msg[i] != '\0'
ch = msg[i]
//encrypt for lowercase letter
If (ch >= 'a' and ch <= 'z')
ch = ch + key
if (ch > 'z')
ch = ch - 'z' + 'a' - 1
done
msg[i] = ch
//encrypt for uppercase letter
else if (ch >= 'A' and ch <= 'Z')
ch = ch + key
if (ch > 'Z')
ch = ch - 'Z' + 'A' - 1
done
msg[i] = ch
done
done
Print Encrypted message
End
For decryption
Begin
For i = 0 to msg[i] != '\0'
ch = msg[i]
//decrypt for lowercase letter
if(ch >= 'a' and ch <= 'z')
ch = ch - key
if (ch < 'a')
ch = ch +'z' - 'a' + 1
done
msg[i] = ch
//decrypt for uppercase letter
else if (ch >= 'A' and ch <= 'Z')
ch = ch + key
if (ch < 'A')
ch = ch + 'Z' - 'A' + 1
done
msg[i] = ch
done
done
Print decrypted message
End
Example
#include<iostream>
#include<string.h>
int main() {
char msg[100];
int i, j, length,choice,key;
length = strlen(msg);
cin>>choice;
char ch;
ch = msg[i];
}
msg[i] = ch;
}
ch = ch + key;
}
msg[i] = ch;
}
}
}
char ch;
ch = msg[i];
ch = ch - key;
msg[i] = ch;
}
ch = ch - key;
}
msg[i] = ch;
}
}
}
Output
For encryption:
Enter the message:
tutorial
Enter key: 3
Enter your choice
1. Encryption
2. Decryption
1
Encrypted message: wxwruldo
For decryption:
Enter the message:
wxwruldo
Enter key: 3
Enter your choice
1. Encryption
2. Decryption
2
Decrypted message: tutorial
Caesar Cipher in C and C++ [Encryption &
Decryption]
Get program for caesar cipher in C and C++ for encryption and decryption.
It is one of the simplest encryption technique in which each character in plain text is
replaced by a character some fixed number of positions down to it.
For example, if key is 3 then we have to replace character by another character that is
3 position down to it. Like A will be replaced by D, C will be replaced by F and so on.
Encryption
1 #include<stdio.h>
2
3 int main()
4 {
6 int i, key;
19
if(ch > 'z'){
20
ch = ch - 'z' + 'a' - 1;
21
}
22
23
message[i] = ch;
24
}
25
else if(ch >= 'A' && ch <= 'Z'){
26
ch = ch + key;
27
28 if(ch > 'Z'){
29 ch = ch - 'Z' + 'A' - 1;
30 }
31
32 message[i] = ch;
33 }
34 }
35
38 return 0;
39 }
Output
Enter a message to encrypt: axzd
Enter key: 4
Encrypted message: ebdh
Decryption
1 #include<stdio.h>
2
3 int main()
4 {
6 int i, key;
12
14 ch = message[i];
15
17 ch = ch - key;
18
20 ch = ch + 'z' - 'a' + 1;
21 }
22
23 message[i] = ch;
24 }
26 ch = ch - key;
27
32 message[i] = ch;
33 }
34 }
35
38
return 0;
39
}
Output
Enter a message to decrypt: ebdh
Enter key: 4
Decrypted message: axzd
Encryption
1 #include<iostream>
2
4
5 int main()
6 {
8 int i, key;
21
if(ch > 'z'){
22 ch = ch - 'z' + 'a' - 1;
23 }
24
25 message[i] = ch;
26 }
28 ch = ch + key;
29
31 ch = ch - 'Z' + 'A' - 1;
32 }
33
34 message[i] = ch;
35 }
36 }
37
40 return 0;
41 }
Output
Enter a message to encrypt: asd zf
Enter key: 3
Encrypted message: dvg ci
Decryption
1 #include<iostream>
2
3 using namespace std;
4
5 int main()
6 {
8 int i, key;
21
if(ch < 'a'){
22
ch = ch + 'z' - 'a' + 1;
23
}
24
25
message[i] = ch;
26
}
27
else if(ch >= 'A' && ch <= 'Z'){
28
ch = ch - key;
29
30
if(ch > 'a'){
31
ch = ch + 'Z' - 'A' + 1;
32
}
33
34 message[i] = ch;
35 }
36 }
37
40 return 0;
41 }
Output
Enter a message to decrypt: az GjK
Enter key: 2
Decrypted message: yx EhI
You may even use this as an assignment or mini project in B. Tech. or network security
subject by adding little gui and improving the source code. Feel free to use, modify and share
the code... Knowledge is always free !!!
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class Caesar
{
public: void encrypt(char *inp,char *out,int key);
void decrypt(char *inp,char *out,int key);
void readText(char *inp);
};
void Caesar::encrypt(char *inp,char *out,int key)
{
ifstream input;
ofstream output;
char buf;
input.open(inp);
output.open(out);
buf=input.get();
while(!input.eof())
{
if(buf>='a'&&buf<='z')
{
buf-='a';
buf+=key;
buf%=26;
buf+='A';
}
output.put(buf);
buf=input.get();
}
input.close();
output.close();
readText(inp);
readText(out);
}
void Caesar::decrypt(char *inp,char *out,int key)
{
ifstream input;
ofstream output;
char buf;
input.open(inp);
output.open(out);
buf=input.get();
while(!input.eof())
{
if(buf>='A'&&buf<='Z')
{
buf-='A';
buf+=26-key;
buf%=26;
buf+='a';
}
output.put(buf);
buf=input.get();
}
input.close();
output.close();
readText(inp);
readText(out);
}
void Caesar::readText(char *inp)
{
ifstream input;
char buf;
input.open(inp);
cout<<"\n\n <--- "<<inp<<" --->\n";
buf=input.get();
while(!input.eof())
{
cout<<buf;
buf=input.get();
}
input.close();
}
void main()
{
Caesar a;
int choice,key;
char inp[30],out[30];
clrscr();
cout<<"\n Enter input file: ";
cin>>inp;
cout<<"\n Enter output file: ";
cin>>out;
cout<<"\n Enter key: ";
cin>>key;
cout<<"\n\n 1. Encrypt\n 2. Decrypt\n\n Select choice(1 or 2): ";
cin>>choice;
if(choice==1)
a.encrypt(inp,out,key);
else if(choice==2)
a.decrypt(inp,out,key);
else cout<<"\n\n Unknown choice";
getch();
}