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

📄 rsa.h

📁 RSA算法的VC实现
💻 H
字号:
#ifndef RSA_H
#define RSA_H
#include "Numbertheory.h"
#include "Cipher.h"
#include "Long.h"
void Dec_to_26(int n,int* str);//十进制转换为26进制;
char Decode(char c);// 解码单个字符;
void Decode_Tri(char* str);//解码三个字符;
template<class Type>
void Code_Tri(char* str,Type& l);//编码3个字符;
template<class Type>
void Long_to_26(Type x,char* str,int base);//统一下面两个操作;
template<class Type> class RSA:public Cipher{
	public:
	   RSA(){str=new char[513];}//预留成512bit长度;
	   ~RSA(){delete []str;}
	   void EnInitialKey(Type k,Type N);//加密密钥初始化;
	   void DeInitialKey(Type k,Type N);//普通解密密钥初始化;
	   void CRTInitialKey(Type k1,Type k2,Type d,Type N);// 中国剩余定理解密密钥初始p,q
	   Type Encipher(Type x);//加密单个明文;
	   void Encipher(char* mfile,char* cfile);//加密文件;
	   Type Decipher(Type y);//单个解密;
	   void Decipher(char* cfile,char* dfile);//解密文件;
       Type CRT_Optimized(Type y);//中国剩余定理解密单个密文;
	   void CRT_Optimized(char* cfile,char* dfile);//中国剩余定理解密文件;
	private:
		Type a;//解密指数;
		Type b;//加密指数;
		Type n;//
		Type p;//
		Type q;//
	    char* str;//[513]幂指数二进制表示;
};

template<class Type>
void RSA<Type>::EnInitialKey(Type k,Type N){
	b=k;
	n=N;
}
template<class Type>
void RSA<Type>::DeInitialKey(Type k,Type N){
	a=k;
	n=N;
}
template<class Type>
void RSA<Type>::CRTInitialKey(Type k1,Type k2,Type d,Type N){
	p=k1;
	q=k2;
	a=d;
	n=N;
}
template<class Type>
Type RSA<Type>::Encipher(Type x){
   Long_to_binary(b,str);
   return Square_and_Multiply(x,str,n);
}
template<class Type>
void RSA<Type>::Encipher(char* mfile,char* cfile){
	ifstream fin(mfile);
	if(!fin){
		cout<<mfile<<"cannot be opened"<<endl;exit(1);
	}
	ofstream fout(cfile);
	char ming[4];
	Type y,x;
	int i;
	cout<<"密文:"<<endl;
    for(i=0;i<3;i++)
		fin.get(ming[i]);
	ming[i]='\0';
	while(ming[0]!=EOF)//!fin.eof()
	{
    Code_Tri(ming,x);//对明文编码;
	y=Square_and_Multiply(x,b,n);
	cout<<y<<" ";//输出密文;
	fout<<y<<" ";
    for(i=0;i<3;i++)
		fin.get(ming[i]);
	ming[i]='\0';
	}
	cout<<0<<endl;//结束标志;
	fout<<0<<endl;
	fin.close();
	fout.close();
}
template<class Type>
Type RSA<Type>::Decipher(Type y){
   Long_to_binary(a,str);
   return Square_and_Multiply(y,str,n);
}
template<class Type>
void RSA<Type>::Decipher(char* cfile,char* dfile){//,Type b,Type n
	ifstream fin(cfile);//"cipher.txt"
	if(!fin){
		cout<<cfile<<"cannot be opened"<<endl;exit(1);
	}
	ofstream fout(dfile);
	Type y,x;
	char* temp;//[17]
	temp=new char[4];
	fin>>y;
	cout<<endl<<"普通解密得到的明文:"<<endl;
	while(y!=0)
	{
	x=Square_and_Multiply(y,a,n);
	Long_to_26(x,temp,26);
	Decode_Tri(temp);
	cout<<temp;
	fout<<temp;
	fin>>y;
	}
	delete []temp;
	fin.close();
	fout.close();
}
template<class Type>
Type RSA<Type>::CRT_Optimized(Type y){
	Type m[2];
	Type r[2];//residual
	Type d[2];
	char* str0;
	char* str1;
	char* temp;//[17]
	str0=new char[513];
	str1=new char[513];
	m[0]=p;
	m[1]=q;
	d[0]=a%(p-1);
	d[1]=a%(q-1);
    Long_to_binary(d[0],str0);
    Long_to_binary(d[1],str1);  
    Long_to_binary(a,str);//
    r[0]=Square_and_Multiply(y,str0,p);
    r[1]=Square_and_Multiply(y,str1,q);
	delete str0;
	delete str1;
	return sunzi(r,m,2);//调用孙子定理;
}
template<class Type>
void RSA<Type>::CRT_Optimized(char* cfile,char* dfile){//中国剩余定理解密;

	ifstream fin(cfile);//"cipher.txt"
	if(!fin){
		cout<<cfile<<"cannot be opened"<<endl;exit(1);
	}
	Type m[2];
	Type r[2];//residual
	Type d[2];
	char* temp;//[17]
	temp=new char[4];
	m[0]=p;
	m[1]=q;
	d[0]=a%(p-1);
	d[1]=a%(q-1);
	ofstream fout(dfile);
 	Type y,x;
    Long_to_binary(a,str);//
	cout<<endl<<"中国剩余定理解密得到的明文:"<<endl;
	fin>>y;
	//cout<<y<<endl;
	while(y!=0)
	{
    r[0]=Square_and_Multiply(y,d[0],p);
    r[1]=Square_and_Multiply(y,d[1],q);
	x=sunzi(r,m,2);
	Long_to_26(x,temp,26);
	Decode_Tri(temp);
	cout<<temp;
	fout<<temp;
	fin>>y;
	}
	delete []temp;
	fin.close();
	fout.close();
}
void Dec_to_26(int n,int* str){
	n=n/(26*26);
	str[0]=n%(26*26);
}
char Decode(char c){// 解码单个字符;
	if(c>='0'&&c<='9')
		return c-'0'+'A';//a可以变为大写+32;
	else if(c>='a'&&c<='z')
	return c+10-32;//
	else {cout<<"******"<<c<<endl;return '#';}//给出错误提示;
}
void Decode_Tri(char* str){//解码三个字符;
	int i;
	for(i=0;i<3;i++)
	str[i]=Decode(str[i]);
}
template<class Type>
void Code_Tri(char* str,Type& l){//编码三个字符;
	int i;
	Type t=0;
	Type base=26*26;
	Type coef;
	for(i=0;i<3;i++){
		if(str[i]>='A'&&str[i]<='Z')//这里假定明文是大写,这当然不是关键
		coef=str[i]-'A';
		else coef=str[i]-'a';//,小写字母也行的;
		t=t+coef*base;
		base=base/26;
	}
	l=t;
}
long int Int(Long x,Long s){//超长整型转换为普通长整型;
	return x.Long_to_long()/s.Long_to_long();//这里要保证转换不超过范围;
}
long int Int(long int x,long int s){
	return x/s;
}
template<class Type>
void Long_to_26(Type x,char* str,int base=26){//这种写法要记住;
	int i;
	long int t=0;
	Type s=base*base;//Type 26*26
	for(i=0;i<3;i++){
		t=Int(x,s);
		x=x%s;
		if(t>=0&&t<=9)
		{
			//cout<<t<<endl;
			str[i]=t+'0';
		}
		else
		{
			str[i]=t-10+'a';//这里真经典;
		}
		s=s/base;
	}
	str[i]='\0';
}

#endif

⌨️ 快捷键说明

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