Code:
// security.cpp : This file contains the 'main' function. Program execution begins and ends
there.
//
#include <iostream>
#include<string>
using namespace std;
char MsgAutoKey[1000], cip[36], enc[1000], decy[1000];
int keyAK[1000];
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;
}
};
string encryption(string m)
{
int a = 1, b = 2;
cout << "enter 1 key " << endl;
cin >> a;
cout << "enter 2 key " << endl;
cin >> b;
//Cipher Text initially empty
string c = "";
for (int i = 0; i < m.length(); i++)
{
// Avoid space to be encrypted
if (m[i] != ' ')
// added 'A' to bring it in range of ASCII alphabet [ 65-90 | A-Z ]
c = c + (char)((((a * (m[i] - 'A')) + b) % 26) + 'A');
else
//else append space character
c += m[i];
}
return c;
};
int findIndexAK(char ch)
{
int x;
for (int i = 0; i < 36; i++)
{
if (cip[i] == ch)
{
x = i;
i = 36;
}
}
return x;
}
void selectiontable(string k, string s)
{
int i, l = 0, x;
cout << "----key select from these words----"<<endl;
for (i = 0; i < 10; i++)
{
cip[l] = i + 48;
cout << cip[l];
l++;
}
cout <<" "<< endl;
for (i = 0; i < 26; i++)
{
cip[l] = i + 97;
cout << cip[l];
l++;
}
cout << " " << endl;
cout << endl;
cout << "************Key Code : " ;
for (i = 0; i < s.size(); i++)
{
x = findIndexAK(k[i]);
keyAK[i] = x;
cout << k[i];
}
cout << endl;
}
int EncryptAK(string s, string k)
{
int i, l = 0, x, y = 0;
for (i = 0; i < s.size(); i++)
{
if ((s[i] > 47 && s[i] < 58) || (s[i] > 96 && s[i] < 123))
{
x = findIndexAK(s[i]);
enc[i] = cip[(keyAK[y] + x) % 36];
y++;
if (y > k.size())
{
y = 0;
}
}
cout << enc[i];
}
return i;
}
void DecryptAK(char s[], string k, int j)
{
int i, l = 0, x, z, y = 0;
for (i = 0; i < j; i++)
{
if ((s[i] > 47 && s[i] < 58) || (s[i] > 96 && s[i] < 123))
{
x = findIndexAK(s[i]);
z = x - keyAK[y];
if (z < 0)
{
z = 36 + z;
}
decy[i] = cip[z];
y++;
if (y > k.size())
{
y = 0;
}
}
cout << decy[i];
}
return;
}
int main()
{
string s, k;
string UserWord, Enc, Dec, Plantext, Codetext;
int EncDecNumber, Cipher, key,number;
do {
cout << " Enter Cipher U want to use" << endl;
cout << "1: Ceaser Cipher n2: PlayFair Ciphern3: Affine n4: AutoKey Cipher
n5: Vigener " << endl;
cin >> Cipher;
if (Cipher == 1)
{
char msg[100];
cout << "Ceaser Cipher" << endl;
cout << "Enter the message:n";
cin >> msg;
//cin.getline(msg, 100); //take the message as input
int i, j, length, choice, key;
cout << "Enter key: ";
cin >> key; //take the key as input
length = strlen(msg);
cout << "Enter your choice n1. Encryption n2. Decryption n";
cin >> choice;
if (choice == 1) //for encryption
{
char ch;
for (int i = 0; msg[i] != '0'; ++i)
{
ch = msg[i];
//encrypt for lowercase letter
if (ch >= 'a' && ch <= 'z')
{
ch = ch + key;
if (ch > 'z') {
ch = ch - 'z' + 'a' - 1;
}
msg[i] = ch;
}
//encrypt for uppercase letter