写了一个RSA的程序,但是连接的时候出错了
#include <iostream.h>#include <math.h>
#include <stdio.h>
struct PublicK // 公钥
{
int e;
int n;
} puk;
struct PrivateK //私钥
{
int d;
int n;
} prk;
int p,q,b;//p,q分别为两大素数,b为加密指数
int fn;//fn为欧拉数
int min,mi;//min为明文,mi为密文
int flag = 0;
bool test_prime(int prime)//检测一个数是否为素数
{
int i;
int limit= (int)sqrt( (double)prime );
for(i=2; i <= limit; i++)
{
if((prime%i)==0)
{
return false;
break;
}
}
return 1;
}
void switch_to_bit(int b, int bin[32]) //将十进制数据转化为二进制数组
{
int n = 0;
while( b > 0)
{
bin[n] = b % 2;
n++;
b /= 2;
}
}
int CountCommonData(int a, int b)//求最大公约数
{
int c ;
if( c= a%b )
return CountCommonData(b,c);
else
return b;
}
int CountOula(int p, int q, int &fn)//生成欧拉数
{
if( !test_prime(p) )
return 0;
if( !test_prime(q) )
return 0;
fn= (p-1)*(q-1);
return fn;
}
void order(int &in1, int &in2) /* 将两个数排序,大的在前面*/
{
int a = ( in1 > in2 ? in1 : in2);
int b = ( in1 < in2 ? in1 : in2);
in1 = a;
in2 = b;
}
int extend_euclid(int m, int bin) // 用扩展的欧几里得算法求乘法逆元
{
order(m,bin);
int a[3],b[3],t[3];
a[0] = 1, a[1] = 0, a[2] = m;
b[0] = 0, b[1] = 1, b[2] = bin;
if (b[2] == 0)
{
return a[2] = CountCommonData(m, bin);
}
if (b[2] ==1)
{
return b[2] = CountCommonData(m, bin);
}
while(true)
{
if (b[2] ==1)
{
return b[1];
break;
}
int q = a[2] / b[2];
for(int i=0; i<3; i++)
{
t[i] = a[i] - q * b[i];
a[i] = b[i];
b[i] = t[i];
}
}
}
int modular_multiplication(int a, int b, int n) //快速模幂算法
{
int f = 1;
int bin[32];
switch_to_bit(b,bin);
for(int i=31; i>=0; i--)
{
f = (f * f) % n;
if(bin[i] == 1)
{
f = (f * a) % n;
}
}
return f;
}
// 产生密钥
void produce_key() {
cout<<"input two primes p and q:";
cin>>p>>q;
int a;
fn=CountOula(p, q, a);
prk.n = p * q;
puk.n = p * q;
cout<<"fn = "<<fn<<endl;
cout<<"input b :";
cin>>b;
while((CountCommonData(fn,b)!=1))
{
cout<<"b is error,try again!";
cout<<"input b :";
cin>>b;
}
prk.d = (extend_euclid(fn,b) + fn) % fn;
puk.e = b;
flag = 1;
cout<<"PrivateK.d: "<<prk.d<<"PrivateK.n: "<<prk.n<<endl;
cout<<"PublicK.e: "<<puk.e<<" Publick.n: "<<prk.n<<endl;
}
void encrypt() //加密
{
if(flag == 0)
{
cout<<"setkey first:"<<endl;
produce_key();
}
cout<<"input minwen:";
cin>>min;
mi = modular_multiplication(min,puk.e,puk.n);
cout<<"miwen is:"<<mi<<endl;
}
// 解密
void decrypt() {
if(flag == 0) {
cout<<"setkey first:"<<endl;
produce_key();
}
cout<<"input miwen:";
cin>>mi;
min = modular_multiplication(mi,prk.d,prk.n);
cout<<"minwen is:"<<min<<endl;
}
int mian()
{
cout<<"…………………菜单……………………"<<endl<<endl;
cout<<"1.encrypt加密……………………"<<endl;
cout<<"2.decrypt解密……………………"<<endl;
cout<<"3.setkey生成密钥…………………………"<<endl;
int s;
char y='y';
while(y='y')
{
cout<<"请选择菜单:"<<endl;
cin>>s;
switch(s)
{
case (1):{
encrypt();
}break;
case (2):{
decrypt();
}break;
case (3):{
produce_key();
}break;
}
cout<<endl<<"是否继续?y/n:";
cin>>y;
if(y=='n')
break;
}
return 0;
}
不知道怎么回事出了一个连接上的错误Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/aaa.exe : fatal error LNK1120: 1 unresolved externals
请各位大神指点迷津,小弟不甚感激