⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 text.h

📁 RSA算法的VC实现
💻 H
字号:
这是草稿,和不成文的设计文档;保存大量冗余数据,有利于数据恢复;
	char* temp;//[17]
	temp=new char[17];
	int i=7;
	_itoa(i,temp,2);
	cout<<temp<<endl;
	cout<<strlen(temp)<<endl;
	delete temp;
	int x=9726;
	int b=3533;
	int n=11413;
	int p,q;
    cout<<Square_and_Multiply(x,b,n)<<endl;
	p=FactoringAlgorithm(n);
	q=n/p;
    cout<<n<<"="<<p<<"*"<<q<<endl;
	return 1;
	int b=1261;
	int n=18923;
	int i=17575;
	cout<<temp<<endl;
	cout<<strlen(temp)<<endl;
	cout<<Decode('p')<<endl;
6340 8309 14010 8936 27358 25023 16481 25809
23614 7135 
	long x=9726;
	long y;//=8936
	long b=4913;
	long a;
	long n=31313;
	long p,q;
int main(){
	//int l;
	//int i;
	long x=9726;
	//long y;//=8936
	long b=;
	long a;
	long n=18923;
	long p,q;
    //cout<<Square_and_Multiply(x,b,n)<<endl;
	p=FactoringAlgorithm(n);
	q=n/p;
    cout<<n<<"="<<p<<"*"<<q<<endl;
	a=Inverse(b,(p-1)*(q-1));
	cout<<a<<endl;
	cout<<MOD(a*b,(p-1)*(q-1))<<endl;
	/*char* temp;//[17]
	temp=new char[4];
	ifstream fin("cipher.txt");
	if(!fin){
		cout<<"cipher.txt cannot be opened"<<endl;exit(1);
	}
	fin>>y;
	cout<<y<<endl;
	while(y!=0)
	{
	x=Square_and_Multiply(y,a,n);
	_itoa(x,temp,26);
	l=strlen(temp);
	if(l<3){
		char middle[4];
		for (i=0;i<4;i++)
			middle[i]=temp[i];
		//cout<<"middle="<<middle<<endl;
		for(i=3;l>0;l--,i--)
			temp[i-1]=middle[l-1];//这就是所谓的中间变量的使用;
		for(;i>0;i--)
			temp[i-1]='0';
		//cout<<"temp="<<temp<<endl;
	}
	//cout<<temp<<endl;
	//temp[3]='\0';
	Decode_Tri(temp);
	cout<<temp;//<<endl
	fin>>y;
	//cout<<y<<endl;
	}
	//x=1371;
	//_itoa(x,temp,26);
	//Decode_Tri(temp);
	cout<<temp<<endl;
	//int x=17575;
	delete temp;
	fin.close();*/
	//cout<<Inverse(15,101)<<endl;
	cout<<MOD(56*27,101)<<endl;
	//cout<<MOD(14387,27)<<endl;
    cout<<Square_and_Multiply(4,48,97)<<endl;
	return 1;
}
	//int l;
	//int i;
	Long x=9726;
	//long y;//=8936
	Long b=1261;
	Long a;
	Long n=18923;
	Long p,q;
    //cout<<Square_and_Multiply(x,b,n)<<endl;
	//p=FactoringAlgorithm(n);
	q=n/p;
    cout<<n<<"="<<p<<"*"<<q<<endl;
int main(){
	Long a=28,n=75;
	Long b=3533;
	char str[18];
	cout<<Inverse(a,n)<<endl;
	b.to_Binary(str);
	cout<<str<<endl;
	return 1;
}
	//j=o;
	//cout<<j<<endl;
	int o=0;
	Long j=1;
	i=9;
	//cout<<j<<endl;
	//for(i=1;i<91;i++)
	{
	//j=Jacobi(7411,9283);
	//84 cout<<j<<endl;
	}
密码实验三号
                  (要求在期末考试前以电子邮件方式传到我的信箱,
                      邮件主题命名方式同前实验作业)

1.编程检验课本P148的陈述:
               若n是任意的奇合数,则
                      “n是对于基底a的欧拉伪素数”
               至多对于一半的整数a?Zn*成立。
2.编程实现p148算法5.6Solovay-Strassen(n).
   记事件a: “一个特定长度的随机奇数n是合数。”
   记事件b: “算法连续m次均判别n是素数。”
   编程检验:p151条件概率P[b|a]?2-m.
                               条件概率P[a|b] ? (Ln(n) -2) / (Ln(n)-2+2m+1) 
3.编程建立一个素数发生器。
   在网上下载一批素数询问上述发生器作为检验。
4.编制一个RSA加密解密程序,字符映射方法仿照p186#12。
   建立两个解密模块,一个是常规的模幂计算,另一个是p187#13介
   绍的中国剩余定理方法。
 
   素数库:
                          100                           25
                        1,000                          168
                       10,000                        1,229
                      100,000                        9,592
                    1,000,000                       78,498
                   10,000,000                      664,579
                  100,000,000                    5,761,455
                1,000,000,000                   50,847,534
               10,000,000,000                  455,052,511
              100,000,000,000                4,118,054,813
            1,000,000,000,000               37,607,912,018
           10,000,000,000,000              346,065,536,839
          100,000,000,000,000            3,204,941,750,802
        1,000,000,000,000,000           29,844,570,422,669
       10,000,000,000,000,000          279,238,341,033,925
      100,000,000,000,000,000        2,623,557,157,654,233
    1,000,000,000,000,000,000       24,739,954,287,740,860
   10,000,000,000,000,000,000      234,057,667,276,344,607
  100,000,000,000,000,000,000    2,220,819,602,560,918,840
Long::Long(const  int a)//long//这里解决了一个大问题,编译系统只人0作为int,而不是long int
{
template<class Type>
Type Jacobi(Type a,Type n){
	int o=0;
	Type one=1;//
	Type zero=o;
	Type two=2;//
	//Type result;//在这里定义临时变量在作为结果返回有问题;
	Type b=a+1;//
	Type flg=n%2;//(n&1)判断n是否为正奇数;
	//Type even=a&one;//牺牲一下效率吧;
	Type even=a%2;
	//Type flg=n%2;
	if(a==0)return zero;//0这里是判断合数的出口;
	else if((a==2)&&(flg==1))//这里的顺序呢?
	{
		if(((n%8)==1)||((n%8)==7))
			return 1;
		else return -1;
	}
	else if(even==0&&flg==1)//(a&1)
	{
		b=a/2;
		return Jacobi(two,n)*Jacobi(b,n);//a/2编译系统真奇怪;
		// result=result;
	}
	else if(a==1&&flg==1) return 1;//(n&1)调转一下次序不知道行不行
	else if(a>n&&flg==1)
		return Jacobi(a%n,n);
	else if(even==1&&flg==1)
	{
		if((a%4)==3&&(n%4)==3)
			return -Jacobi(n,a);
		else return Jacobi(n,a);
	}
	else 
	{
		return zero;
	}
}
	//int i;
	/*cout<<Jacobi(7411,9283)<<endl;
	Long a=3,n=103;
	Long b=3533;
	Long c=9726,d=11413;
	char str[18];
	Long j=1;
	cout<<Inverse(a,n)<<endl;
	b.to_Binary(str);
	cout<<str<<endl;
    cout<<Square_and_Multiply(c,str,d)<<endl;
	a=10;
	n=91;
	c=10;b=45;d=91;
	b.to_Binary(str);
	cout<<b<<endl;
	cout<<str<<endl;
    //cout<<Square_and_Multiply(c,str,d)<<endl;
    j=Solovay_Strassen(a,n);//
	cout<<j<<endl;//
	//a=2,n=91;//
	//cout<<Jacobi(a,n)<<endl;*/
	//Long a=999961;
	/*Long i,j;
	for(i=3;i<10;i=i+2)
		for(j=i+2;j<12;j=j+2)
	Verify(i*j);
	//Verify(999961);*/
	Long a=3,n=997;
	/*int ver;
	for(a=1;a<n;a++){
	ver=Solovay_Strassen(a,n);
	if(ver!=1)
    cout<<ver<<endl;
	}*/
	Long a,b=1234;
	cout<<b.Odd()<<endl;
	a=7411;
	b=9283;
	cout<<Jacobi(a,b)<<endl;	
	Long a=3,n=997;
	int ver;
	for(a=1;a<n;a++){
	ver=Solovay_Strassen(a,n);
	if(ver!=1)
    cout<<ver<<endl;
	}
	Long i,j;
	for(i=3;i<100;i=i+2)
		for(j=i+2;j<120;j=j+2)
	Verify(i*j);
	Long i,j;
	for(i=3;i<10;i=i+1)
		for(j=i+2;j<12;j=j+1)
	Verify(i*j);//
	cin>>i;
	cout<<i<<endl;
    char a[]="kmdfsdlds";
	cin>>a;
	cout<<a;
	Long i,j;
	for(i=3;i<10;i=i+2)
		for(j=i+2;j<12;j=j+2)
	Verify(i*j);
	//Verifyfile(Primefile);
	Long l;
	char str[4]="ZZZ";
    Code_Tri(str,l);
	cout<<l<<endl;
	//long b=1261;
	//long a;
	//long n=18923;
	/*Type p,q;
	Type s,t;
 	p=FactoringAlgorithm(n);
	q=n/p;
	t=(p-1)*(q-1);
    cout<<n<<"="<<p<<"*"<<q<<endl;
	a=Inverse(b,t);
	s=a*b;
	cout<<a<<endl;
	//cout<<MOD(s,t)<<endl;*/
	int a[3]={2,3,2};
	int m[3]={3,5,7};
	cout<<sunzi(a,m,3)<<endl;
	long a,n;
	while(1){
		cin>>a>>n;
	cout<<Jacobi(a,n)<<endl;
	}
	//double b[1000000][10000000000];

	r.EnInitialKey(1261,18923);
	r.Encipher(Mingfile,Cipherfile);
	r.DeInitialKey(5797,18923);
	r.Decipher(Cipherfile,Decipherfile);
	cout<<endl;
	r.CRTInitialKey(127,149,1261,18923);
	r.CRT_Optimized(Cipherfile,Decipherfile);
	r.CRTInitialKey(p,q,d,n);
	x=r.CRT_Optimized(y);
	cout<<x<<endl;
	RSA<unsigned long> r;
	unsigned long b,n,p,q,d,y,x,a;
	p=1511;
	q=2003;
	n=p*q;
	a=d=1234577;
	y=152702;
	r.DeInitialKey(a,n);
	x=r.Decipher(y);
	cout<<x<<endl;
	b=Inverse(a,(p-1)*(q-1));
	cout<<MOD(a*b,(p-1)*(q-1))<<endl;
	r.EnInitialKey(b,n);
	cout<<r.Encipher(x)<<endl;
	RSA<Long> r;
	Long b,n,p,q,d,y,x,a;
	p=127;
	q=149;
	n=p*q;
	a=d=5797;
	y=12423;//152702
	r.DeInitialKey(a,n);
	x=r.Decipher(y);
	cout<<x<<endl;
	b=Inverse(a,(p-1)*(q-1));
	cout<<b<<endl;
	cout<<MOD(a*b,(p-1)*(q-1))<<endl;
	r.EnInitialKey(b,n);
	cout<<r.Encipher(x)<<endl;
	RSA<Long> r;
	Long b,n,p,q,d,y,x,a;
	p=1511;
	q=2003;
	n=p*q;
	a=d=1234577;
	y=152702;//12423
	r.DeInitialKey(a,n);
	x=r.Decipher(y);
	cout<<x<<endl;
	b=Inverse(a,(p-1)*(q-1));
	cout<<b<<endl;
	cout<<MOD(a*b,(p-1)*(q-1))<<endl;
	r.EnInitialKey(b,n);
	cout<<r.Encipher(x)<<endl;
	Long b,n,p,q,d,y,x,a;
	p=1511;
	q=2003;
	n=p*q;
	a=d=1234577;
	y=152702;//12423
	r.DeInitialKey(a,n);
	x=r.Decipher(y);
	cout<<x<<endl;
	b=Inverse(a,(p-1)*(q-1));
	cout<<b<<endl;
	cout<<MOD(a*b,(p-1)*(q-1))<<endl;
	r.EnInitialKey(b,n);
	cout<<r.Encipher(x)<<endl;
	r.CRTInitialKey(p,q,d,n);
	x=r.CRT_Optimized(y);
	cout<<x<<endl;
void Long_to_26(Type x,char* str,int base);//统一下面两个操作;
template<class Type>
void Long_to_26(Type x,char* str,int base=26){//这种写法要记住;
		else
		{
			str[i]=t-10+'a';//这里真经典;
		}
	if(l<3){
		char middle[4];
		for (i=0;i<4;i++)
			middle[i]=temp[i];
		//cout<<"middle="<<middle<<endl;
		for(i=3;l>0;l--,i--)
			temp[i-1]=middle[l-1];//这就是所谓的中间变量的使用;
		for(;i>0;i--)
			temp[i-1]='0';
		//cout<<"temp="<<temp<<endl;
	}、、完全省略;
	l=strlen(temp);
	if(l<3){
		char middle[4];
		for (i=0;i<4;i++)
			middle[i]=temp[i];
		//cout<<"middle="<<middle<<endl;
		for(i=3;l>0;l--,i--)
			temp[i-1]=middle[l-1];//这就是所谓的中间变量的使用;
		for(;i>0;i--)
			temp[i-1]='0';
		//cout<<"temp="<<temp<<endl;
	}		
	friend long int& operator =(Long& y){
		int i=0;
		return i;
	}
	Long a=123456;
	cout<<a.Long_to_long()<<endl;
	cout<<a<<endl;
	char str[4];
	Long_to_26(3*26*26+10*26+6,str);//,26
	cout<<str<<endl;
	RSA<Long> r;
	r.EnInitialKey(1261,18923);
	r.Encipher(Mingfile,Cipherfile);
	r.DeInitialKey(5797,18923);
	r.Decipher(Cipherfile,Decipherfile);
	cout<<endl;
	r.CRTInitialKey(127,149,1261,18923);
	r.CRT_Optimized(Cipherfile,Decipherfile);
	while((*a)!='\0'&&(*a)!=13&&(*a)!=10)//||粘贴就会出现这样的错误;
	int i=0;
	cin.get(a[i]);
	while(*a=='\0'||*a==13||*a==10)
	cin.get(*a);
	cin.get(*(++a));
	while((*a)!='\0'&&(*a)!=13&&(*a)!=10)//||粘贴就会出现这样的错误;
		cin.get(*(++a));
	*a='\0';
    Long a;
	cin>>a;
	cout<<a;
	int i=0;
	cin.get(a[i]);
	while(a[i]==32||a[i]==13||a[i]==10)//32 represent space;
	cin.get(a[i]);
	cin.get(a[++i]);
	while(a[i]!=32&&a[i]!=13&&a[i]!=10)//||粘贴就会出现这样的错误;
		cin.get(a[++i]);
	a[i]='\0';
    /*Long a;
	cin>>a;
	cout<<a;*/
	RSA<Long> r;
	r.EnInitialKey(1261,18923);
	r.Encipher(Mingfile,Cipherfile);
	r.DeInitialKey(5797,18923);
	r.Decipher(Cipherfile,Decipherfile);
	cout<<endl;
	r.CRTInitialKey(127,149,5797,18923);
	r.CRT_Optimized(Cipherfile,Decipherfile);
	cout<<endl;
	for(i=num;i<2*num;i++)//检验范围为N-2N;//这里也有错;
		r=rand()%n;//这里有大大的问题啊;需要吗?
	Long m=1,N=1021;
    cout<<Answer(m,N)<<endl;
	//for(;m<N;m++)
	Pr_ab(m,N);
	Pr_ba(m,N);
	Long m=2,N=1021;
    cout<<Answer(m,N)<<endl;
	//for(;m<N;m++)
	Pr_ab(m,N);
	Pr_ba(m,N);
	cout<<Answer(,999961)<<endl;
	long a=5,m=100,n=999961;//这里类型要匹配好;
	//for(a=1;a<100;a++)
	//cout<<Solovay_Strassen(a,n)<<endl;
    cout<<Answer(m,n)<<endl;
	Long a=5,m=1,n1="9999618234729447",n2="238472947247";
	//for(a=1;a<100;a++)
	//cout<<Solovay_Strassen(a,n)<<endl;
    cout<<Answer(m,n2)<<endl;
	cout<<FactoringAlgorithm(n2)<<end
/*int Solovay_Strassen(Long a,Long n){//素性检验:返回1为素数,0为合数;
	Long x;//x=
	x=Jacobi(a,n);
	//cout<<"fdsfds"<<x<<endl;
	if(x==0)return 0;
	Long b=(n-1)/2;
	char str[513];
	b.to_Binary(str);
    //_itoa(b,str,2);
	Long y=Square_and_Multiply(a,str,n);
	//cout<<MOD(x,n)<<"haha"<<endl;
	//cout<<y<<endl;
	if(MOD(x,n)==y)
		return 1;
	else return 0;
}*/
	//long b=1261;
	//long a;
	//long n=18923;
	/*Type p,q;
	Type s,t;
 	p=FactoringAlgorithm(n);
	q=n/p;
	t=(p-1)*(q-1);
    cout<<n<<"="<<p<<"*"<<q<<endl;
	a=Inverse(b,t);
	s=a*b;
	cout<<a<<endl;
	//cout<<MOD(s,t)<<endl;*/
    long bogusprime=0;
    long T=0;
 	long i,j;
	for(i=3;i<10;i=i+2)//00
		for(j=i+2;j<12;j=j+2)//两个奇数相乘结果为奇整数;
		{
			Verify(i*j,bogusprime,T);
			cout<<"(Bogus/Zn*)=("<<bogusprime<<"/"<<T<<")"<<" for "<<i*j<<endl;
		}
	long i;
	long n=11;
	for(i=1;i<n;i++){
		cout<<Jacobi(i,n)<<" ";
	long b=(n-1)/2;
	char str[513];
    Long_to_binary(b,str);
    //_itoa(b,str,2);//粘贴产生的错误; 执果索因
	long y=Square_and_Multiply(i,str,n);
	cout<<i<<" "<<y<<endl;
	}//这里又是一个检验模块;
	Long l="1235116486503105156123112301",h="3483247128472384738248234324";
	Long m=1;
    Prime_Generator(l,h,m);
	Long l="1234567890",h="1234567899";
	Long m=100;
	Long l=1234567890,h=1234567899;
    Prime_Generator(l,h,m);
	cout<<FactoringAlgorithm(1234567891);
	long m=100;
    Verifyfile(Primefile,m);
a=100
b=100
n in [3,10000]
max=0.5 for n=1729
N=20000
m;Pr[b|a];1/pow(2,m)
1;23/16205;0.5
2;0/16205;0.25
5;0/16205;0.03125
10;0/16205;0.000976563
20;0/16205;9.53674e-007
50;0/16205;8.88178e-016
100;0/16205;7.88861e-031

m;Pr[a|b];(ln(n)-2)/(ln(n)-2+pow(2,m+1))
1;15/2823;0.692358
2;2/2647;0.52947
5;0/2574;0.123313
10;0/2547;0.00437633
20;0/2541;4.29253e-006
50;0/2538;3.99774e-015
100;0/2530;3.55071e-030

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -