//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;
}
Saturday, July 27, 2013
Simple Multiplicative Cipher
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment