📄 main.c
字号:
#include "windows.h"
#define MAXSIZE 0x10000
#define MAXLEN 160
#define DEC 10
#define HEX 16
//#define system 10
#pragma comment( lib, "xmlParser4Etri.lib" )
char *srcfile="c:\\test.xml";
char *resfile="c:\\result.xml";
#pragma warning(disable:4700)//局矾皋矫瘤甫 唱鸥唱瘤 臼霸 窃
#pragma warning(disable:4101)
typedef struct BInt{
int Length;
int Value[MAXLEN];
}BInt;
BInt Q,*Qp,P,*Pp,N,*Np,N2,*N2p,D,*Dp,E,*Ep,F1,F2,F3,*F1P,*F2P,*F3P;
char word[1000];
const static int PrimeTable[550]=
{ 3, 5, 7, 11, 13, 17, 19, 23, 29, 31,
37, 41, 43, 47, 53, 59, 61, 67, 71, 73,
79, 83, 89, 97, 101, 103, 107, 109, 113, 127,
131, 137, 139, 149, 151, 157, 163, 167, 173, 179,
181, 191, 193, 197, 199, 211, 223, 227, 229, 233,
239, 241, 251, 257, 263, 269, 271, 277, 281, 283,
293, 307, 311, 313, 317, 331, 337, 347, 349, 353,
359, 367, 373, 379, 383, 389, 397, 401, 409, 419,
421, 431, 433, 439, 443, 449, 457, 461, 463, 467,
479, 487, 491, 499, 503, 509, 521, 523, 541, 547,
557, 563, 569, 571, 577, 587, 593, 599, 601, 607,
613, 617, 619, 631, 641, 643, 647, 653, 659, 661,
673, 677, 683, 691, 701, 709, 719, 727, 733, 739,
743, 751, 757, 761, 769, 773, 787, 797, 809, 811,
821, 823, 827, 829, 839, 853, 857, 859, 863, 877,
881, 883, 887, 907, 911, 919, 929, 937, 941, 947,
953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019,
1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087,
1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153,
1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229,
1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297,
1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381,
1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453,
1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523,
1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597,
1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663,
1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741,
1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823,
1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901,
1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993,
1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063,
2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131,
2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221,
2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293,
2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371,
2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437,
2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539,
2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621,
2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689,
2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749,
2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833,
2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909,
2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001,
3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083,
3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187,
3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259,
3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343,
3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433,
3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517,
3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581
};
//**************************************************************************
BInt* init(BInt *x);
int comp(BInt *x, BInt *y);
BInt addi(BInt *x, int b);
BInt adds(BInt *x, BInt *y);
BInt subi(BInt *x, int y);
BInt subs(BInt *x, BInt *y);
BInt muli(BInt *x, int y);
BInt muls(BInt *x, BInt *y);
BInt divi(BInt *x,int y);
BInt divs(BInt *x, BInt *y);
int modi(BInt *x,int y);
BInt mods(BInt *x, BInt *y);
//**************************************************************************
int RaMe(BInt *x);
BInt Mongmery(BInt *m, BInt *e, BInt *n);
BInt get();
void put(BInt *x);
BInt getprime(int k);
BInt euc(BInt *x, BInt *y);
BInt euc2(BInt *x, BInt *y);
//**************************************************************************
BInt euc2(BInt *x, BInt *y)
{
BInt A,B,C,*Ap,*Bp,*Cp;
Ap=&A;Bp=&B;Cp=&C;
A=*x;
B=*y;
while(!(B.Length==1 && B.Value[0]==0))
{
C=mods(Ap,Bp);
A=B;
B=C;
}
return A;
}
BInt euc(BInt *x, BInt *y)
{
BInt M,E,X,Y,I,J,*Mp,*Ep,*Xp,*Yp,*Ip,*Jp;
int a,b;
Mp=&M;Ep=&E;Xp=&X;Yp=&Y;Ip=&I;Jp=&J;
Xp=init(Xp);Yp=init(Yp);Ip=init(Ip);Jp=init(Jp);
M=*y;E=*x;
Y.Value[0]=1;
a=b=1;
while((E.Length!=1)||(E.Value[0]!=0))
{
I=divs(Mp,Ep);
J=mods(Mp,Ep);
M=E;
E=J;
J=Y;
Y=muls(Yp,Ip);
if(a==b)
{
if(comp(Xp,Yp)!=2)Y=subs(Xp,Yp);
else{Y=subs(Yp,Xp);b=0;}
}
else{Y=adds(Xp,Yp);a=1-a;b=1-b;}
X=J;
}
if(a==0)X=subs(y,Xp);
return X;
}
BInt getprime(int k)
{
BInt x,*xp,y,*yp,zero,*zerop;
int a,b,size;
unsigned c;
xp=&x;yp=&y;zerop=&zero;zerop=init(zerop);yp=init(yp);
a=b=c=0;
size=40;
if(k==1)c=(unsigned)time(NULL);
else c=(unsigned)(time(NULL)*2);
first:
srand(c);
xp=init(xp);
for(a=1;a<size-1;a++) x.Value[a]=abs((rand()*rand())%MAXSIZE);
x.Value[0]=(rand()%1000)*10+1;
x.Value[size-1]=rand()%MAXSIZE;
xp->Length=size;
//ViewBInt(xp);
for(a=0;a<100;a++)
{
b=modi(xp,PrimeTable[a]);
if(b==0){ c=c+100;goto first;}
}
//put(xp);
b=RaMe(xp);
//printf("%d\n",b);
if(b==0) {c=c+1000;goto first;}
//put(xp);
return x;
}
void put(BInt *x)
{
int a,b,system;
char t[16]="0123456789ABCDEF",ch;
BInt X,*Xp;
Xp=&X;
Xp=init(Xp);
X=*x;
system=16;
for(a=0;a<1000;a++)word[a]='\0';
a=0;
while(X.Value[X.Length-1]>0)
{
b=modi(Xp,system);
ch=t[b];
word[a]=ch;
X=divi(Xp,system);
a++;
}
strrev(word);
printf("\n");
printf("%s\n", word);
}
BInt get()
{
BInt x,*xp;
char word[1000],*prime="17475243319617581867";
int a,len,system,k;
xp=&x;
system=16;
for(a=0;a<1000;a++)word[a]='\0';
//printf("Input Big Integer!\n");
//scanf("%s",word);
len=strlen(word);
xp=init(xp);
for(a=0;a<len;a++)
{
x=muli(xp,system);
if((word[a]>='0')&&(word[a]<='9'))k=word[a]-48;
else if((word[a]>='A')&&(word[a]<='F'))k=word[a]-55;
else if((word[a]>='a')&&(word[a]<='f'))k=word[a]-87;
else k=0;
x=addi(xp,k);
}
//printf("%s\n",word);
return x;
}
BInt Mongmery(BInt *m, BInt *e, BInt *n)
{
BInt x,y,z,*xp,*yp,*zp,res,*resp,two,*twop,zero,*zerop;
int com1,com2;
xp=&x;yp=&y;zp=&z,resp=&res,twop=&two,zerop=&zero;
x=*m;y=*e;z=*n;
twop=init(twop);
resp=init(resp);
zerop=init(zerop);
res.Value[0]=1;
two.Value[0]=2;
//printf("矫累!\n");
com1=comp(yp,zerop);
while(com1!=0)
{
while(y.Value[0]%2==0)
{
y=divs(yp,twop);
x=muls(xp,xp);
x=mods(xp,zp);
if(y.Length==1 && y.Value[0]==1)break;
}
y=subi(yp,1);
res=muls(resp,xp);
res=mods(resp,zp);
//put(yp);
com1=comp(yp,zerop);
}
return res;
}
int RaMe(BInt *x)
{
BInt b,m,j,v,i,zero;
BInt a,c;
BInt *pb,*pm,*pj,*pv,*pi,*pzero,*pa,*pc,test,*ptest;
int czero,rn,com1,com2,com3;
pb=&b;
pm=&m;
pj=&j;
pv=&v;
pi=&i;
ptest=&test;
pzero=&zero;
pa=&a;
pc=&c;
ptest=init(ptest);
pzero=init(pzero);
pc=init(pc);
pa=init(pa);
pb=init(pb);
pm=init(pm);
pj=init(pj);
pv=init(pv);
pi=init(pi);
a.Value[0]=2;
c.Value[0]=1;
m=subi(x,1);
test=divi(pm,2);
czero=comp(ptest,pzero);//函拳且鳖?
while(czero==0)
{
j=addi(pj,1);
m=divs(pm,pa);
test=divi(pm,2);
czero=comp(ptest,pzero);
}
rn=2;
b.Value[0]=rn;
v=Mongmery(pb,pm,x);
//put(pv);
com1=comp(&v,&c);
if(com1==0)
{
return 1;
}
i.Value[0]=1;
test=subi(x,1);
com2=comp(pv,ptest);
while(com2!=0)
{
if(i.Value[0]==1)
com3=comp(pi,pj);
if(com3==0)
{
return 0;
}
if(i.Value[0]==10)
return 0;
v=Mongmery(pv,pa,x);
i=addi(&i,1);
test=subi(x,1);
com2=comp(pv,ptest);
}
return 1;
}
//**************************************************************************
BInt mods(BInt *x, BInt *y)
{
BInt X,Y,*Xp,*Yp;
int i,div,num,carry=0,len;
Xp=&X;Yp=&Y;
X=*x;
Yp=init(Yp);
while(comp(Xp,y)!=2)
{
div=X.Value[X.Length-1];
num=y->Value[y->Length-1];
len=X.Length-y->Length;
if((div==num)&&(len==0)){X=subs(Xp,y);break;}
if((div<=num)&&len){len--;div=div*MAXSIZE+X.Value[X.Length-2];}
div=div/(num+1);
Yp=init(Yp);
Y.Value[0]=div;
Y=muls(y,Yp);
if(len)
{
Y.Length+=len;
for(i=Y.Length-1;i>=len;i--)Y.Value[i]=Y.Value[i-len];
for(i=0;i<len;i++)Y.Value[i]=0;
}
X=subs(Xp,Yp);
}
return X;
}
int modi(BInt *x,int y)
{
int i;
unsigned div,carry=0;
if(x->Length==1)return(x->Value[0]%y);
for(i=x->Length-1;i>=0;i--)
{
div=x->Value[i];
div+=carry*MAXSIZE;
carry=div%y;
}
return carry;
}
BInt divs(BInt *x, BInt *y)
{
BInt X,Y,Z,*Xp,*Yp,*Zp,m,*mp;
int i,len,num,div;
Xp=&X;Yp=&Y;Zp=&Z;mp=&m;
if(y->Length==1)return divi(x,y->Value[0]);
Y=*x;
Xp=init(Xp);
while(comp(Yp,y)!=2)
{
div=Y.Value[Y.Length-1];
num=y->Value[y->Length-1];
len=Y.Length-y->Length;
if((div==num)&&(len==0)){X=addi(Xp,1);break;}
if((div<=num)&&len){len--;div=(div*MAXSIZE)+Y.Value[Y.Length-2];}
div=div/(num+1);
Zp=init(Zp);
Z.Value[0]=div;
if(len)
{
Z.Length+=len;
for(i=Z.Length-1;i>=len;i--)Z.Value[i]=Z.Value[i-len];
for(i=0;i<len;i++)Z.Value[i]=0;
}
X=adds(Xp,Zp);
m=muls(y,Zp);
Y=subs(Yp,mp);
}
return X;
}
BInt divi(BInt *x,int y)
{
BInt res,*resp;
unsigned div,mul,carry=0;
int i;
resp=&res;
resp=init(resp);
res=*x;
if(res.Length==1){res.Value[0]=res.Value[0]/y;return res;}
for(i=res.Length-1;i>=0;i--)
{
div=carry;
div=(div*MAXSIZE)+res.Value[i];
res.Value[i]=(div/y);
mul=(div/y)*y;
carry=div-mul;
}
if(res.Value[res.Length-1]==0)res.Length--;
return res;
}
BInt muli(BInt *x, int y)
{
BInt res,*resp;
unsigned mul,carry;
int i;
resp=&res;
resp=init(resp);
res=*x;
carry=0;
for(i=0;i<x->Length;i++)
{
mul=x->Value[i];
mul=mul*y+carry;
res.Value[i]=mul%MAXSIZE;
carry=mul>>16;
}
if(carry){res.Length++;res.Value[res.Length-1]=carry;}
return res;
}
BInt muls(BInt *x, BInt *y)
{
BInt res,*resp;
unsigned i,j,sum,mul=0,carry=0;
if(y->Length==1)return muli(x,y->Value[0]);
resp=&res;
resp=init(resp);
res.Length=x->Length+y->Length-1;
for(i=0;i<res.Length;i++)
{
sum=carry;
carry=0;
for(j=0;j<y->Length;j++)
{
if(((i-j)>=0)&&((i-j)<x->Length))
{
mul=x->Value[i-j];
mul*=(y->Value[j]);
carry+=mul>>16;
mul=mul%MAXSIZE;
sum+=mul;
}
}
carry+=sum>>16;
res.Value[i]=sum%MAXSIZE;
}
if(carry){res.Length++;res.Value[res.Length-1]=carry;}
return res;
}
BInt subs(BInt *x, BInt *y)
{
int carry=0,num,i;
BInt res,*resp;
resp=&res;
resp=init(resp);
i=comp(x,y);
if(i!=1)return res;
res=*x;
for(i=0;i<res.Length;i++)
{
if((x->Value[i]>y->Value[i])||((x->Value[i]==y->Value[i])&&(carry==0)))
{
res.Value[i]=x->Value[i]-carry-y->Value[i];
carry=0;
}
else
{
num=MAXSIZE+x->Value[i];
res.Value[i]=num-carry-y->Value[i];
carry=1;
}
}
while(res.Value[res.Length-1]==0)res.Length--;
return res;
}
BInt subi(BInt *x, int y)
{
BInt res,*resp;
int num,i;
resp=&res;
resp=init(resp);
res=*x;
if(res.Value[0]>=y){res.Value[0]=res.Value[0]-y;return res;}
if(res.Length==1){res.Value[0]=0;return res;}
num=MAXSIZE+res.Value[0];
res.Value[0]=num-y;
i=1;
while(res.Value[i]==0){res.Value[i]=MAXSIZE-1;i++;}
res.Value[i]--;
if(res.Value[i]==0)res.Length--;
return res;
}
BInt adds(BInt *x, BInt *y)
{
unsigned carry,middle,sum;
int a;
BInt res,*resp;
carry=middle=0;
resp=&res;
resp=init(resp);
res=*x;
if(res.Length<y->Length)res.Length=y->Length;
for(a=0;a<res.Length;a++)
{
sum=y->Value[a];
sum=sum+res.Value[a]+carry;
res.Value[a]=sum%MAXSIZE;
carry=sum>>16;
}
res.Value[res.Length]=carry;
res.Length=res.Length+carry;
return res;
}
BInt addi(BInt *x, int b)
{
BInt res, *resp;
int a,sum;
resp=&res;
resp=init(resp);
res=*x;
sum=res.Value[0];
sum=sum+b;
res.Value[0]=sum;
if(res.Value[0]>MAXSIZE-1)
{
res.Value[0]=res.Value[0]%MAXSIZE;
a=1;
while(res.Value[a]==MAXSIZE-1){res.Value[a]=0;a++;}
res.Value[a]++;
if(res.Length==a)res.Length++;
}
return res;
}
int comp(BInt *x, BInt *y)
{
int i=0;
if(x->Length>y->Length)return 1;
if(x->Length<y->Length)return 2;
for(i=x->Length; i>=0; i--)
{
if(x->Value[i]>y->Value[i])return 1;
if(x->Value[i]<y->Value[i])return 2;
}
return 0;
}
BInt* init(BInt *x)
{
int length;
for(length=0;length<MAXLEN;length++)
x->Value[length]=0;
x->Length=1;//辨捞绰 攫力唱 硅凯狼 辨捞 +1
return x;
}
/*
unsigned char* DecryptRSAKey(XMLNode* docNode, char* RSAKey1, char* RSAKey2)
{
FILE *Df, *Ff;
unsigned char* buf;
int i;
Df=fopen("c:\\D.txt", "rb");
fseek(Df,0,2);
i=ftell(Df);
buf=(unsigned char*)malloc(i);
fread(buf, i, 1, Df);
printf("%s, \n", buf);
fclose(Dg);
}
*/
void main()
{
char* hello="hello";
char* dec;
int a,e;
int len,i,k;
Qp=&Q;Pp=&P;Np=&N;N2p=&N2;Dp=&D,Ep=&E;F1P=&F1;F2P=&F2;F3P=&F3;
Qp=init(Qp);
Q=getprime(1);
put(Qp);
dec=(char*)malloc(160);
memset(dec, '\0', 160);
Pp=init(Pp);
P=getprime(2);
put(Pp);
/*-------------------------------------------------------------------------------------*/
Np=init(Np);
N=muls(Qp,Pp);
put(Np);
P=subi(Pp,1);
Q=subi(Qp,1);
N2p=init(N2p);
N2=muls(Pp,Qp);
put(N2p);
a=100;
do
{
if(modi(N2p,PrimeTable[a])!=0){e=PrimeTable[a];break;}
a++;
}while(1);
Ep=init(Ep);
E.Value[0]=e;
put(Ep);
Dp=init(Dp);
D=euc(Ep,N2p);
put(Dp);
Pp=init(Pp);
P=euc2(Np,Dp);
put(Pp);
F1P=init(F1P);
//printf(" Input key! \n");
//scanf("%s, hello", hello);
len=strlen(hello);
for(i=0;i<len;i++)
{
F1.Value[i]=(unsigned int)hello[i];
}
F1.Length=len;
F2P=init(F2P);
F3P=init(F3P);
F2=Mongmery(F1P,Ep,Np);
put(F1P);
printf("鞠龋拳等 巩辑!\n");
put(F2P);
F3=Mongmery(F2P,Dp,Np);
k=160;
for(i=159;i>=0;i--)
{
if(F3.Value[i]!=0)k=k-1;
else break;
}
for(i=0;i<k;i++)
{
dec[i]=(char)F3.Value[i];
}
put(F3P);
printf("汗龋拳等 巩辑!\n");
printf("%s \n", dec);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -