📄 main.cpp
字号:
# include <iostream.h>
# include <string.h>
# include <fstream.h>
# include <math.h>
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
# define ul unsigned long
ul isPrime(ul n)
{
ul b=1;
for(ul i=2;i<n;i++)
if(n%i==0) { b=0; break; }
return b;
}
ul Power(ul x,ul y)
{
ul ans=1;
if(x==1) return x;
for(ul i=1;i<=y;i++)
ans*=x;
return(ans);
}
char * ToBin(int x)
{
char *ans=new char[20];
int i=0;
while(x>=1)
{
if(x%2==1) ans[i]='1'; else ans[i]='0';
x/=2; i++;
}
if(x==1) ans[i]='1',i++;
ans[i]='\0';
strrev(ans);
return(ans);
}
ul Crypt(int x,int key,int n) // (x^y)%z //Method 2
{
char *B=new char[20];
B=ToBin(key);
ul ans=1;int c=1;
for(int i=0;i<strlen(B);i++)
{
c=2*c;
ans=(ans*ans)%n;
if(B[i]=='1')
{
c=c+1;
ans=(ans*x)%n;
}
}
return(ans);
}
void Fun1(int,int,int);
int Bin2Num(char *s)
{
int ans=0; int i,j;
for(j=0,i=7;i>=0;j++,i--)
{
if(s[i]=='1') ans+=Power(2,j);
}
return ans;
}
char * Num2Bin(int n)
{
char *s=new char[20];
int i=0;
for(i=0;i<8;i++) s[i]='0';
while(n>1)
{
if(n%2==1) s[i]='1';
else s[i]='0';
n/=2; i++;
}
if(n%2==1) s[i]='1'; else s[i]='0';
s[8]='\0';
strrev(s);
return s;
}
void KeyEnc(int,int);
void KeyDec(int,int);
void main()
{
clrscr();
randomize();
/* K E Y G E N E R A T I O N */
ul p,q,n,n1,e,d;
Beg: // Reading 2 random prime-numbers from 20 to 50
p=2+rand()%200;
q=2+rand()%200;
//p=17;q=11;
if(p==q||!isPrime(p)||!isPrime(q)) goto Beg;
n = p * q;
n1 = (p-1) * (q-1);
int x=n1+1;
for(e=2;e<=x/2;e++)
{
if(x%e==0) break;
}
d=x/e;
if(p==1||q==1||e==1||d==1||e==d||
q==e||q==d||p==e||p==d) goto Beg;
// if(e>30||d>30) goto Beg;
ul P1=18,P2,Cypher;
B1:
Cypher=Crypt(P1,e,n); //Encrypting using public-key 'e'
// if(Cypher>160||Cypher<d*2||Cypher==P1) goto Beg;
P2=Crypt(Cypher,d,n);//Decrypting using private key 'd'
cout<<"p = "<<p<<endl;
cout<<"q = "<<q<<endl;
cout<<"n =(p*q) = "<<n<<endl;
cout<<"n1=(p-1)*(q-1)= "<<n1<<endl;
cout<<"e = "<<e<<endl;
cout<<"d = "<<d<<endl<<endl;
cout<<"Public Key : { "<<e<<","<<n<<" }"<<endl;
cout<<"Private Key : { "<<d<<","<<n<<" }"<<endl;
cout<<"PlainText Before Encryption is : "<<P1<<endl;
cout<<"Cypher is : "<<Cypher<<endl;
cout<<"PlainText After Decryption is : "<<P2<<endl;
getch(); randomize(); clrscr();goto Beg;
// KeyEnc(e,n);
// KeyDec(d,n);
getch();
}
void KeyEnc(int e,int n)
{
ifstream fin("key.txt");
ofstream fout("keyEnc.txt");
char str[10]; int i,X,Y,j;
//while(!fin.eof())
for(j=0;j<8;j++)
{
for(i=0;i<8&&!fin.eof();i++) { fin.get(str[i]); cout<<str[i]; }
str[8]='\0';
cout<<str<<",";
X=Bin2Num(str);
Y=Crypt(X,e,n);
cout<<X<<","<<Y<<endl;
fout<<Y<<endl;
getch();
}
fin.close(); fout.close();
}
void KeyDec(int d,int n)
{
ifstream fin("keyEnc.txt");
ofstream fout("keyDec.txt");
char ch; int i,X,Y;
char *str=new char[20];
cout<<"Decrypt...\n";
int Ch;
while(!fin.eof())
{
fin>>Ch; if(fin.eof()) return;
cout<<Ch;
Y=Crypt(Ch,d,n);
cout<<","<<Y;
str=Num2Bin((int)Y);
cout<<","<<str<<endl;
fout<<str;
getch();
}
fin.close(); fout.close();
}
void Fun1(int e,int d,int n)
{
char str[30];
cout<<"Enter some Text : ";
gets(str);
ofstream fout("d:\\cyfer.txt",ios::binary);
int M,C; char ch;
for(int i=0;i<strlen(str);i++)
{
C=Crypt(str[i],e,n);
fout.put(C);
} fout.close();
ifstream fin("d:\\cyfer.txt");
while(!fin.eof())
{
fin.get(ch);
ch=Crypt(ch,d,n);
cout<<ch;
}
fin.close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -