C++ Code
C++ Code
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 −
For decryption:
Enter the message:
wxwruldo
Enter key: 3
Enter your choice
1. Encryption
2. Decryption
2
Decrypted message: tutorial
Encrypt and decrypt text file using C++
Encryption in cryptography is a process by which a plain text or a piece of information is
converted into ciphertext or a text which can only be decoded by the receiver for whom the
information was intended. The algorithm that is used for the process of encryption is known as
a cipher. It helps protect consumer information, emails, and other sensitive data from
unauthorized access to it and secures communication networks. Presently there are many options
to choose from and find the most secure algorithm which meets our requirements.
Decryption: Decryption is the process of converting a meaningless message (Ciphertext) into its
original form (Plaintext). It works by applying the conversion algorithm opposite of the one that
is used to encrypt the data. The same key is required to decrypt the information back to its
normal form.
Types of Cryptography: There are two types of cryptography:
Symmetric Cryptography: It is an encryption system where the sender and receiver of a
message use a single common key to encrypt and decrypt messages. Symmetric Key Systems are
faster and simpler, but the sender and receiver have to somehow exchange keys securely. The
most popular symmetric-key cryptography system is Data Encryption System(DES).
Asymmetric Cryptography: Under this system, a pair of keys is used to encrypt and decrypt
information. A public key is used for encryption and a private key is used for decryption. The
public key and the private key are different. Even if the public key is known by everyone, the
intended receiver can only decode it because he alone knows the private key.
In this article, symmetric cryptography is used to encrypt and decrypt data.
Approach: Let’s discuss the approach in detail before proceeding to the implementation part:
A class encdec is defined with two member functions: encrypt() and decrypt(). The name of the
file to be encrypted is the member variable of the class.
encrypt() function is used to handle the encryption of the input file. The file handling code is
included in the encrypt() function to read the file and write to the file. A new encrypted file
called encrypt.txt is generated with all the encrypted data in it. The encrypted file is encrypted
using a key that is being inputted by the user.
decrypt() function is used to read the encrypted file and decrypt the data and generate a new
file decrypt.txt. To decrypt a file, a key is requested from the user. If the correct key is entered,
then the file is successfully decrypted.
The input stream fin is used to read from the file and the output stream fout is used to write to the
file.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
public:
void encrypt();
void decrypt();
};
// Input stream
fstream fin, fout;
fin.close();
fout.close();
}
// Driver Code
int main()
{
encdec enc;
char c;
cout << "\n";
cout << "Enter Your Choice : -> \n";
cout << "1. encrypt \n";
cout << "2. decrypt \n";
cin >> c;
cin.ignore();
switch (c) {
case '1': {
enc.encrypt();
break;
}
case '2': {
enc.decrypt();
break;
}
}
}
Output:
Video Player
00:00
01:1
#include<iostream>
#include<math.h>
using namespace std;
float en[3][1], de[3][1], a[3][3], b[3][3], msg[3][1], m[3][3];
void getKeyMatrix() { //get key and message from user
int i, j;
char mes[3];
cout<<"Enter 3x3 matrix for key (should have inverse):\n";
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++) {
cin>>a[i][j];
m[i][j] = a[i][j];
}
cout<<"\nEnter a string of 3 letter(use A through Z): ";
cin>>mes;
for(i = 0; i < 3; i++)
msg[i][0] = mes[i] - 65;
}
void encrypt() { //encrypts the message
int i, j, k;
for(i = 0; i < 3; i++)
for(j = 0; j < 1; j++)
for(k = 0; k < 3; k++)
en[i][j] = en[i][j] + a[i][k] * msg[k][j];
cout<<"\nEncrypted string is: ";
for(i = 0; i < 3; i++)
cout<<(char)(fmod(en[i][0], 26) + 65); //modulo 26 is taken for each element of the matrix
obtained by multiplication
}
void inversematrix() { //find inverse of key matrix
int i, j, k;
float p, q;
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++) {
if(i == j)
b[i][j]=1;
else
b[i][j]=0;
}
for(k = 0; k < 3; k++) {
for(i = 0; i < 3; i++) {
p = m[i][k];
q = m[k][k];
for(j = 0; j < 3; j++) {
if(i != k) {
m[i][j] = m[i][j]*q - p*m[k][j];
b[i][j] = b[i][j]*q - p*b[k][j];
}
}
}
}
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
b[i][j] = b[i][j] / m[i][i];
cout<<"\n\nInverse Matrix is:\n";
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++)
cout<<b[i][j]<<" ";
cout<<"\n";
}
}
void decrypt() { //decrypt the message
int i, j, k;
inversematrix();
for(i = 0; i < 3; i++)
for(j = 0; j < 1; j++)
for(k = 0; k < 3; k++)
de[i][j] = de[i][j] + b[i][k] * en[k][j];
cout<<"\nDecrypted string is: ";
for(i = 0; i < 3; i++)
cout<<(char)(fmod(de[i][0], 26) + 65); //modulo 26 is taken to get the original message
cout<<"\n";
}
int main() {
getKeyMatrix();
encrypt();
decrypt();
}
Example
#include <iostream>
#include <string>
using namespace std;
class Vig {
public:
string k;
Vig(string k) {
for (int i = 0; i < k.size(); ++i) {
if (k[i] >= 'A' && k[i] <= 'Z')
this->k += k[i];
else if (k[i] >= 'a' && k[i] <= 'z')
this->k += k[i] + 'A' - 'a';
}
}
string encryption(string t) {
string output;
for (int i = 0, j = 0; i < t.length(); ++i) {
char c = t[i];
if (c >= 'a' && c <= 'z')
c += 'A' - 'a';
else if (c < 'A' || c > 'Z')
continue;
output += (c + k[j] - 2 * 'A') % 26 + 'A'; //added 'A' to bring it in range of ASCII alphabet [
65-90 | A-Z ]
j = (j + 1) % k.length();
}
return output;
}
string decryption(string t) {
string output;
for (int i = 0, j = 0; i < t.length(); ++i) {
char c = t[i];
if (c >= 'a' && c <= 'z')
c += 'A' - 'a';
else if (c < 'A' || c > 'Z')
continue;
output += (c - k[j] + 26) % 26 + 'A';//added 'A' to bring it in range of ASCII alphabet [ 65-
90 | A-Z ]
j = (j + 1) % k.length();
}
return output;
}
};
int main() {
Vig v("WELCOME");
string ori ="Thisistutorialspoint";
string encrypt = v.encryption(ori);
string decrypt = v.decryption(encrypt);
cout << "Original Message: "<<ori<< endl;
cout << "Encrypted Message: " << encrypt << endl;
cout << "Decrypted Message: " << decrypt << endl;
}
The Caesar Cipher is a simple substitution cipher named after Julius Caesar, who reportedly
used it to communicate with his officials. The technique involves shifting each letter in a
message by a fixed number of positions in the alphabet. For example, with a shift of 3, A would
be replaced by D, B would become E, and so on.
The Caesar Cipher is relatively easy to break and is considered to be a very weak form of
encryption, but it served its purpose for Julius Caesar. It's still used for educational and
recreational purposes.
Algorithm for Caesar Cipher
Here is a basic algorithm for encoding a message using the Caesar Cipher with a shift of k −
Initialize a variable shift to the value of k.
Iterate through each character c in the message −
If c is a letter (uppercase or lowercase), shift it by shift positions in the alphabet.
To shift an uppercase letter, subtract 'A' from the letter, add the shift value, and take the modulus
26. Then add 'A' back to get the shifted letter.
To shift a lowercase letter, subtract 'a' from the letter, add the shift value, and take the modulus
26. Then add 'a' back to get the shifted letter.
b. Append the shifted letter to the encoded message.
Return the encoded message.
To decode an encoded message, the same algorithm can be used with a shift of -k.
Example
def caesar_cipher_encrypt(plaintext, shift):
ciphertext = ""
for c in plaintext:
if c.isalpha():
ascii_code = ord(c)
if c.isupper():
ascii_code = (ascii_code - ord('A') + shift) % 26 + ord('A')
else:
ascii_code = (ascii_code - ord('a') + shift) % 26 + ord('a')
ciphertext += chr(ascii_code)
else:
ciphertext += c
return ciphertext
Block Cipher uses both "confusion" and Stream Cipher uses only
Principle "diffusion" principle for the conversion confusion principle for the
required for encryption. conversion.
For encryption of plain text Block Stream Cipher uses CFB (Cipher
Cipher uses Electronic Code Book Feedback) and OFB (Output
Algorithm
(ECB) and Cipher Block Chaining Feedback) algorithm.
(CBC) algorithm.
As a combination of more bits get Stream Cipher uses XOR for the
encrypted in case of Block Cipher, so encryption which can be easily
Decryption the reverse encryption or decryption is reversed to the plain text.
comparatively complex as compared to
that of Stream Cipher.