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

📄 numbertheory.h

📁 RSA算法的VC实现
💻 H
字号:
#ifndef Numbertheory_h//数论知识;
#define Numbertheory_h
#include "Long.h"
#include<stdlib.h>
#include<iostream.h>
#include<string.h>
void Long_to_binary(Long l,char* str){
	l.to_Binary(str);
}
void Long_to_binary(long l,char* str){
   _itoa(l,str,2);
}
template<class Type> Type Square_and_Multiply(Type x,Type b,Type n){//反复平方乘;Type
	Type z=1;
	char* str=new char[513];
	int i;
	Long_to_binary(b,str);
	int l=strlen(str);
	for(i=0;i<l;i++)
	{
		z=(z*z)%n;
		if (str[i]=='1')
			z=(z*x)%n;
	}
	delete []str;
	return z;
}
template<class Type> Type FactoringAlgorithm(Type n){//素数因子分解;
	Type sub=2;
	for(sub=2;sub*sub<=n;sub++)
		if((n%sub)==0)
			return sub;
	return 1;
}
template<class Type>
Type MOD(Type x, Type n){//求模运算,在Zn范围内
	Type result=x%n;
	if (result<0)
		result=result+n;
	return result;
}
template<class Type>
Type Inverse(Type a,Type n){//求逆运算,存在则返回,不存在则返回0
	if(a>n)
		a=MOD(a,n);
	Type r_2=a,r_1=n,r=1;
	Type q,w;
	Type u_2=1;
	Type u_1=0;
	Type u;
	Type v_2=0;
	Type v_1=1;
	Type v;
	while(r!=0)
	{
		
		q=r_2/r_1;r=r_2-q*r_1;
		if(r!=0)
		{u=u_2-q*u_1;v=v_2-q*v_1;r_2=r_1;r_1=r;
		u_2=u_1;u_1=u;v_2=v_1;v_1=v;}
	}
    w=r_1;
	if(w==1)return MOD(u,n);
	else return 0;
}
template<class Type> 
Type sunzi(Type* a,Type* m,int n){//孙子定理
	Type x=1,result=0;
	int i;
	Type* M=new Type[n];
	Type* G=new Type[n];
	for(i=0;i<n;i++)
		x=x*m[i];
	for(i=0;i<n;i++){
		M[i]=x/m[i];
		G[i]=Inverse(M[i],m[i]);
	}
    for(i=0;i<n;i++)
		result+=M[i]*G[i]*a[i];
	result=MOD(result,x);
	delete []M;
	delete []G;
	return result;
}
/*void main(){//主函数调用孙子定理的方法;
	//int a[3]={2,3,2};
	//int m[3]={3,5,7};
	//cout<<sunzi(a,m,3)<<endl;
	cout<<MOD(17*22+15*4+9*1,26)<<endl;
	cout<<Inverse(5,26)<<endl;
}*/
int Odd(long int b){//判断是否为奇数;
	return b&1;
}
int Odd(Long b){
	return b.Odd();
}
template<class Type>
int Jacobi(Type a,Type n){//计算Jacobi符号;
	Type two=2;
	Type b;
	Type flg=Odd(n);//(n&1)判断n是否为正奇数;
	Type odd=Odd(a);
	if(a==0)return 0;//0这里是判断合数的出口;
	else if((a==2)&&(flg==1))//这里的顺序呢?
	{
		if(((n%8)==1)||((n%8)==7))
			return 1;
		else return -1;
	}
	else if(odd==0&&flg==1)//(a&1)
	{
		b=a/2;
		return Jacobi(two,n)*Jacobi(b,n);//a/2不能识别,编译系统真奇怪;
	}
	else if(a>n&&flg==1)
		return Jacobi(a%n,n);//暂时检查不到错误;
	else if(a==1&&flg==1) return 1;//(n&1)调转一下次序不知道行不行
	//else if(a>n&&flg==1)
		//return Jacobi(a%n,n);//达唔达?
	else if(odd==1&&flg==1)
	{
		if((a%4)==3&&(n%4)==3)
			return -Jacobi(n,a);
		else return Jacobi(n,a);
	}
	else return 0;
}
#endif

⌨️ 快捷键说明

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