//columnar_transposition
#include <iostream>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cctype>
#include <map>
using namespace std;
#define siz 100
char as[siz][siz];
int a[siz];
map <int, int> mp;
map <int, int>::iterator it;
int main()
{
string plainText1, plainText, key, cipherText;
freopen("input.txt", "r", stdin);
while(getline(cin, plainText1) && getline(cin, key)) {
cout << "Plain Text: " << plainText1 <<endl;
cout << "Key: " << key <<endl;
for(int i=0; i<key.size(); i++)
mp[(key[i]-'A')] = i;
for(int i=0; i<plainText1.size(); i++)
if(isalpha(plainText1[i]))
plainText += plainText1[i];
int k = 0, row = 0;
for(int i=0; ; i++){
for(int j=0; j<key.size(); j++) {
if(k >= plainText.size()) {
as[i][j] = rand()%26 + 'A';
} else {
as[i][j] = plainText[k];
}
k++;
}
row++;
if(k >= plainText.size()) break;
}
puts("Grid:");
for(int i=0; i<row; i++){
for(int j=0; j<key.size(); j++) {
cout<<as[i][j]<<" ";
}
puts("");
}
for(it = mp.begin(); it != mp.end(); it++) {
for(int i=0; i<row; i++){
cipherText += as[i][(*it).second];
}
cipherText += " ";
}
cout<<"Cipher Text: "<<cipherText<<endl;
}
return 0;
}
Sunday, August 25, 2013
Columnar Transposition Cipher Encryption in C++
Vigenere Cipher Encryption in C++
//Vigenere Cipher
#include <iostream>
#include <cstdio>
#include <string>
#include <cctype>
using namespace std;
#define siz 100
int P[siz], K[siz], C[siz];
int main()
{
string plainText, key, cipherText;
//freopen("input.txt", "r", stdin);
while(getline(cin, plainText) && getline(cin, key)){
cout << "Plain Text: " << plainText << endl;
cout << "Key: " << key << endl;
cout << "Cipher Text: ";
int len = key.size();
int j = 0;
for(int i=0; i<plainText.size(); i++){
if(isalpha(plainText[i])){
P[i] = plainText[i] - 'a';
K[j] = tolower(key[j % len]) - 'a';
C[j] = (P[i] + K[j]) % 26;
cout<<(char)toupper(C[j] + 'a')<<" ";
j++;
}
}
puts("");
}
return 0;
}
Saturday, August 24, 2013
Playfair Cipher Encryption in C++
//playfair_cipher
#include <iostream>
#include <string>
#include <cstdio>
#include <cctype>
#include <map>
#include <utility>
using namespace std;
#define siz 5
map <char, int> mp;
pair<int, int> p1;
pair<int, int> p2;
map < char, pair<int, int> > pa;
char a[siz][siz], res[siz][siz];
string as="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
bool ischar(char c){
if((c>='a' && c<='z') || (c>='A' && c<='Z'))
return true;
return false;
}
int main()
{
string key, message, msg;
//freopen("input.txt", "r", stdin);
while(getline(cin, key)){
int k = 0, l = 0;
int len = key.size();
for(int i=0;i<len;i++){
if(ischar(key[i])) {
if(mp[toupper(key[i])] == 0) {
a[k][l] = toupper(key[i]);
pa.insert(make_pair(toupper(key[i]), make_pair(k, l)));
mp[toupper(key[i])] = 1;
l++;
}
if(l==5) {
k++;
l = 0;
}
}
}
//mp['J'] = 1;
for(int i=0; i<as.size();i++){
if((toupper(as[i])=='I'||toupper(as[i])=='J') && (mp['I'] == 1||mp['J'] == 1))
continue;
if(mp[toupper(as[i])] == 1)
continue;
else {
a[k][l] = toupper(as[i]);
pa.insert(make_pair(toupper(as[i]), make_pair(k, l)));
l++;
}
if(l==5) {
k++;
l = 0;
}
}
//printing the grid
for(int i=0; i<5; i++) {
for(int j=0; j<5; j++) {
if(j) cout<< " ";
cout << a[i][j];
res[i][j]='0';
}
puts("");
}
//get the message
getline(cin, msg);
cout<<msg<<endl;
for(int i=0; i<msg.size(); i++)
if(ischar(msg[i]))
message += msg[i];
for(int i=0; i<message.size()-1; i++) {
char x,y;
if(message[i] != message[i+1]) {
x = toupper(message[i]);
y = toupper(message[i+1]);
p1 = pa.find(x)->second;
p2 = pa.find(y)->second;
i += 1;
} else {
x = toupper(message[i]);
y = 'X';
p1 = pa.find(x)->second;
p2 = pa.find(y)->second;
}
//columns are same
if(p1.second == p2.second) {
if(p1.first+1 < 5)
cout << a[p1.first + 1][p1.second];
else
cout << a[0][p1.second];
if(p2.first+1 < 5)
cout << a[p2.first + 1][p2.second];
else
cout << a[0][p2.second];
} else if(p1.first == p2.first) { //rows are same
if(p1.second+1 < 5)
cout << a[p1.first][p1.second + 1];
else
cout << a[p1.first][0];
if(p2.second+1 < 5)
cout << a[p2.first][p2.second + 1];
else
cout << a[p2.first][0];
} else {
cout << a[p1.first][p2.second]
<< a[p2.first][p1.second];
}
printf(" ");
}
//clearing
mp.clear();
}
return 0;
}
Saturday, July 27, 2013
Auto Key Cipher Encryption
//auto_key_cipher
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
#define siz 100
int p[siz], c[siz], k[siz];
int main()
{
int key, x;
string plainText, cipherText;
freopen("input.txt", "r", stdin);
while(cin>>plainText>>key){
//encryption
int len = plainText.size();
for(int i=0; i<len; i++){
p[i] = plainText[i] - 'a';
if(i==0) k[i] = key;
else k[i] = p[i-1];
c[i] = (p[i] + k[i]) % 26;
cipherText += c[i] + 'A';
}
cout<<"Cipher Text: "<<cipherText<<endl;
}
return 0;
}
Affine Cipher Encryption and Decryption
//Affine Cipher
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int multiplicative_inverse(int, int);
int main()
{
int key_1, key_2, x;
string plainText, cipherText;
while(cin>>plainText>>key_1>>key_2){
//encryption
cipherText = "";
int len = plainText.size();
for(int i=0; i<len; i++){
x = ((plainText[i] - 'a') * key_1 + key_2) % 26;
cipherText += x + 'A';
}
cout<<"Cipher Text: "<<cipherText<<endl;
//decryption
plainText = "";
int keyInverse = multiplicative_inverse(26, key_1);
cout<<"Multiplicative Inverse: "<<keyInverse<<endl;
for(int i=0; i<len; i++){
x = ((cipherText[i] - 'A' - key_2) * keyInverse) % 26;
if(x < 0) x += 26;
plainText += x + 'a';
}
cout<<"Plain Text: "<<plainText<<endl;
}
return 0;
}
int multiplicative_inverse(int x, int y){
int q, r, r1, r2, t1, t2, t;
r1 = x;
r2 = y;
t1 = 0;
t2 = 1;
while(r2 != 0){
q = r1 / r2;
r = r1 % r2;
t = t1 - t2*q;
r1 = r2;
r2 = r;
t1 = t2;
t2 = t;
}
if(r1 != 1) return 0;
else {
if(t1<0) return t1+x;
else return t1;
}
}
Rail Fence Cipher
//rail_fence_cipher
#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
using namespace std;
int main()
{
int j,k;
string as, bs, cipherText;
//freopen("input.txt","r",stdin);
while(cin>>cipherText){
int len = cipherText.size();
int flag = 0;
for(int i=0; i < len; i++){
if(i == ceil(len / 2.0)) flag = 1;
if(flag == 0) as += cipherText[i];
else bs += cipherText[i];
}
j = k = 0;
cout<<"Cipher Text: "<<cipherText<<endl;
cout<<"Plain Text: ";
for(int i=0; i < len; i++){
if(i % 2 == 0) cout<<as[k++];
else cout<<bs[j++];
}
puts("");
//clearing
as.clear();
bs.clear();
}
return 0;
}
Simple Multiplicative Cipher
//Multiplicative Cipher
#include <iostream>
#include <cstdio>
#include <cctype>
using namespace std;
int multiplicative_inverse(int x, int y){
int q, r, r1, r2, t1, t2, t;
r1 = x;
r2 = y;
t1 = 0;
t2 = 1;
while(r2 != 0){
q = r1 / r2;
r = r1 % r2;
t = t1 - t2*q;
r1 = r2;
r2 = r;
t1 = t2;
t2 = t;
}
if(r1 != 1) return 0;
else {
if(t1<0) return t1+x;
else return t1;
}
}
int main()
{
int key;
string plainText, cipherText;
freopen("input.txt","r",stdin);
while(cin>>key>>plainText){
//encryption
cipherText = "";
int len = plainText.size();
for(int i=0; i<len; i++){
int x = ((plainText[i] - 'a') * key) % 26;
cipherText += toupper(x + 'a');
}
cout<<"Key: "<<key<<endl;
cout<<"Cipher Text: "<<cipherText<<endl;
//decrption
plainText = "";
int keyInverse = multiplicative_inverse(26, key);
cout<<"Multiplicative Inverse: "<<keyInverse<<endl;
for(int i=0; i<len; i++){
int x = ((cipherText[i] - 'A') * keyInverse ) % 26;
plainText += (x + 'a');
}
cout<<"Plain Text: "<<plainText<<endl;
}
return 0;
}
Subscribe to:
Posts (Atom)