1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//encryption function
int EncVSRSA(int num,int n,int e)
{
int i, j, val;
val = 1;
for(j = 1;j <= e;j++)
{
val*= num; // calculating num^e mod n
val %= n;
//cout<<val<<endl;
}
return(val);
}
//decryption function
int DecVSRSA(int num,int n,int d)
{
int i,j,val;
val = 1;
for(j = 1; j <= d; j++)
{
val *= num; // calculating num^e mod n
val %= n;
//cout<<val<<endl;
}
return(val);
}
//main function
int main()
{
ifstream ins("results.txt");
int p,q,n,d,e,i,msg;
char op;
cout << "Enter value for p and q:";
cin >> p >> q;
n = p * q;
cout << "Enter value for e:";
cin >> e;
cout << "Enter value for d:";
cin >> d;
//cout<<"Enter msg:";
//cin>>msg;
//enter choice
cout << "Do you want to encrypt(E) or decrypt(D)?";
cin >> op;
char fname[50];
cout << "Enter file name:";
cin >> fname;
//input file name
ifstream file_in(fname);
//output file
ofstream file_out("results.txt");
if(file_in.is_open())
{
while(!file_in.eof())
{
file_in >> msg;
//Performing encryption
if(op == 'E')
{
int cyper = EncVSRSA(msg,n,e);
file_out << cyper <<" ";
}
//Performing decryption
else
{
int plain = DecVSRSA(msg,n,d);
file_out << plain << " ";
}
}
}
file_in.close();
file_out.close();
}
|