Encrypt and Decrypt Text File Using C
Encrypt and Decrypt Text File Using C
// 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;
}
}
}
Example 2
#include<iostream>
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
def caesar_cipher_decrypt(ciphertext, shift):
plaintext = ""
for c in ciphertext:
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')
plaintext += chr(ascii_code)
else:
plaintext += c
return plaintext
Please note that this algorithm is limited and can be broken by a cryptanalyst with relative ease. it is not
recommended to use it in any real-world applications, it's commonly used as a learning tool in the field of
cryptography.
Example
Here is an example of encoding and decoding a message using the Caesar Cipher with a shift of 3 −
plaintext = "HELLO WORLD"
shift = 3
ciphertext = caesar_cipher_encrypt(plaintext, shift)
print("Encrypted message:", ciphertext) # Encrypted message: KHOOR ZRUOG
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int i, x;
char str[100];
default:
cout << "\nInvalid Input !!!\n";
}
return 0;
}
Example
//C++ program for encryption and decryption
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<string.h>
using namespace std;
int x, y, n, t, i, flag;
long int e[50], d[50], temp[50], j;
char en[50], m[50];
char msg[100];
int prime(long int); //function to check for prime number
void encryption_key();
long int cd(long int);
void encrypt();
void decrypt();
int main()
{
cout << "\nENTER FIRST PRIME NUMBER\n";
cin >> x;
//checking whether input is prime or not
flag = prime(x);
if(flag == 0)
{
cout << "\nINVALID INPUT\n";
exit(0);
}
cout << "\nENTER SECOND PRIME NUMBER\n";
cin >> y;
flag = prime(y);
if(flag == 0 || x == y)
{
cout << "\nINVALID INPUT\n";
exit(0);
}
encryption_key();
cout << "\nPOSSIBLE VALUES OF e AND d ARE\n";
encrypt();
decrypt();
return 0;
} //end of the main program
while(i != len)
{
pt = m[i];
pt = pt - 96;
k = 1;
for(j = 0; j < key; j++)
{
k = k * pt;
k = k % n;
}
temp[i] = k;
ct= k + 96;
en[i] = ct;
i++;
}
en[i] = -1;
cout << "\n\nTHE ENCRYPTED MESSAGE IS\n";
for(i=0; en[i] != -1; i++)
cout << en[i];
}
//function to decrypt the message
void decrypt()
{
long int pt, ct, key = d[0], k;
i = 0;
while(en[i] != -1)
{
ct = temp[i];
k = 1;
for(j = 0; j < key; j++)
{
k = k * ct;
k = k % n;
}
pt = k + 96;
m[i] = pt;
i++;
}
m[i] = -1;
cout << "\n\nTHE DECRYPTED MESSAGE IS\n";
for(i = 0; m[i] != -1; i++)
cout << m[i];
cout << endl;
}