#include <bits/stdc++.h>
using namespace std;
#define SHIFT 3
int main(int argc, char * argv[]) {
string str, crypt;
cout << "Enter string: ";
getline(cin, str);
for (int i=0; i < str.length(); i++) {
if (islower(str[i]))
crypt.push_back((str[i]-'a'+SHIFT)%26+ (int)'A');
else
crypt.push_back(str[i]);
}
cout << crypt << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int keys[26];
int main(int argc, char * argv[]) {
srand(time(0));
string str, crypt;
for (int i=0; i<26; i++)
keys[i] = i;
random_shuffle(keys, keys+26);
cout << "Enter string: ";
getline(cin, str);
for (int i=0; i < str.length(); i++) {
if (islower(str[i]))
crypt.push_back(keys[(int)str[i]-'a']+'A');
else
crypt.push_back(str[i]);
}
cout << crypt << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const string key="monarchy";
char poly[5][5];
map<char, pair<int, int> > look;
void fill_poly() {
int arr[26];
for (int i=0; i<26; i++)
arr[i]=0;
int in = 0;
for (int i=0; i<key.length(); i++) {
if (arr[key[i]-'a'] == 0) {
if (key[i] == 'i' || key[i] == 'j') {
poly[in/5][in%5] = key[i] == i ? key[i]+32 : key[i]+31;
arr['i'-'a'] = arr['j'-'a'] = 1;
} else {
poly[in/5][in%5] = key[i]-32;
arr[key[i]-'a'] = 1;
}
in++;
}
}
for (int i=0; i<26; i++) {
if (arr[i] == 0) {
if (i==8){
poly[in/5][in%5] = 'I';
arr[i] = 0;
arr[++i] = 1;
}
poly[in/5][in%5] = 'A'+i;
in++;
}
}
// creating map of char to co-ordinates
for (int i=0; i<5; i++)
for (int j=0; j<5; j++)
look.insert(make_pair(poly[i][j]+32, make_pair(i,j)));
}
string cipher(string s) {
if (s[0] == 'i') s[0] = 'j';
if (s[1] == 'i') s[1] = 'j';
pair<int, int> p1 = look[s[0]];
pair<int, int> p2 = look[s[1]];
string c="00";
if (p1.first == p2.first) { // same row
c[0] = poly[p1.first][(p1.second+1)%5];
c[1] = poly[p2.first][(p2.second+1)%5];
} else if (p1.second == p2.second) { // same col
c[0] = poly[(p1.first+1)%5][p1.second];
c[1] = poly[(p2.first+1)%5][p2.second];
} else { // else anywhere on board
c[0] = poly[p1.first][p2.second];
c[1] = poly[p2.first][p1.second];
}
return c;
}
int main(int argc, char * argv[]) {
string str, crypt="";
fill_poly();
for (int i=0; i<5; i++) {
for (int j=0; j<5; j++)
cout << poly[i][j] << " ";
cout << "\n";
}
cout << "Enter string: ";
getline(cin, str);
for (int i=0; i<str.length()-1; i+=2) {
if (str[i] == str[i+1]) {
string s;
s.push_back(str[i]);
s.push_back((str[i]+1)%97+'a');
crypt += cipher(s);
i--;
} else {
string s;
s.push_back(str[i]);
s.push_back(str[i+1]);
crypt += cipher(s);
}
}
cout << crypt << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define SHIFT 3
const string key = "computing";
int look[SHIFT][SHIFT];
void make_look() {
for (int i=0; i<key.length(); i++) {
look[i/SHIFT][i%SHIFT] = key[i]-'a';
}
}
string cipher(string x) {
int arr[SHIFT][1] = {{x[0]-'a'}, {x[1]-'a'}, {x[2]-'a'}};
//int res[SHIFT][1] = {{0},{0},{0}};
string c="";
int res;
for (int i=0; i < SHIFT; i++) {
for (int j=0; j < 1; j++) {
res = 0;
for (int k=0; k<SHIFT; k++) {
res += look[i][k]*arr[k][j];
}
c.push_back('a'+res%26);
}
}
return c;
}
int main(int argc, char * argv[]) {
string str, crypt="";
make_look();
for (int i=0; i<SHIFT; i++) {
for (int j=0; j<SHIFT; j++) {
cout << setw(4) << (int)look[i][j];
}
cout << endl;
}
cout << "Enter string: ";
getline(cin, str);
for (int i=0; i < str.length(); i+=SHIFT) {
string s = str.substr(i, SHIFT);
crypt += cipher(s);
}
cout << crypt << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define ALPHA 26
char lookup[ALPHA][ALPHA];
void init() {
for (int i=0; i<ALPHA; ++i) {
for (int j = 0; j < ALPHA; ++j) {
lookup[i][j] = 'A'+ (i%ALPHA+j)%ALPHA;
}
}
}
int main() {
init();
string key, text, cipher="";
cout << "Enter Text: "; getline(cin, text);
cout << "Enter Key: "; getline(cin, key);
for (int i = 0; i < text.length(); ++i) {
if (islower(text[i])) {
cipher += lookup[text[i]-'a'][key[i]-'a'];
}
}
cout <<"Cipher : ";
cout << cipher << "\n";
return 0;
}
// Key: deceptivedeceptivedeceptive
// Text: wearediscoveredsaveyourself
// Cipher: ZICVTWQNGRZGVTWAVZHCQYGLMGJ
#include <bits/stdc++.h>
using namespace std;
#define ALPHA 27
int main() {
string key, text, cipher="";
cout << "Enter Text: "; getline(cin, text);
cout << "Enter Key: "; getline(cin, key);
int len = min(text.length(), key.length());
int textlen = text.length();
int keylen = key.length();
int i;
for (i = 0; i < len; ++i) {
if (islower(text[i]) && islower(key[i])) {
cipher += (text[i]-'a'+key[i]-'a')%ALPHA+'A';
} else if (text[i] == 32) {
cipher += key[i] == 'a' ? 'Z' : key[i]-33;
} else if (key[i] == 32) {
cipher += text[i] == 'a' ? 'Z' : text[i]-33;
}
}
for (int j=i; j < keylen; ++j) {
cipher += key[j] == 'a' ? 'Z' : key[j]-33;
}
for (int j=i; i < textlen; ++j) {
cipher += text[j] == 'a' ? 'Z' : text[j]-33;
}
cout <<"Cipher : ";
cout << cipher << "\n";
return 0;
}
/*
ciphertext: ANKYODKYUREPFJBYOJDSPLREYIUNOFDOIUERFPLUYTS
key: pxlmvmsydofuyrvzwc tnlebnecvgdupahfzzlmnyih
plaintext: mr mustard with the candlestick in the hall
*/