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

📄 desdlg.cpp

📁 使用DES算法;可以指定使用DES、2DES或者3DES算法;可以对任意的文件进行加密和解密
💻 CPP
📖 第 1 页 / 共 2 页
字号:


void ByteToBit(bool *Out, const char *In, int bits)
{
    for(int i=0; i<bits; ++i)
        Out[i] = (In[i>>3]>>(i&7)) & 1;
}
void BitToByte(char *Out, const bool *In, int bits)
{
    memset(Out, 0, bits>>3);
    for(int i=0; i<bits; ++i)
        Out[i>>3] |= In[i]<<(i&7);
}

void CDesDlg::Oncbc() 
{
	// TODO: Add your control notification handler code here
    	OnChangemiyao();    //密钥
	    //char skey[8]={1,1,1,1,1,1,1,1};
		char inbuff[8];   //输入  
		char oubuff[8];   //输出 
		int length,length1;
		CFile  file1;
		CFile  file2;
		char IV[8]={1,1,1,10,2,3,6,3};
		int flag=1;
		DES jm; //实例化加密类,得到对象jm
		bool IV1[64];
		bool inbuff1[64];		
		
			file1.Open ("mingwen.txt",CFile::modeReadWrite);		
			length=file1.GetLength();
			int m=length/8;			
			if(length%8!=0)
			file1.SetLength((m+1)*8);
			length1=file1.GetLength();
            m=length1/8;
			file2.Open("miwen.txt",CFile::modeCreate | CFile::modeWrite);
			for(int n=0;n<m;n++)
			{				
				file1.Read(inbuff,8);
				ByteToBit(IV1,IV,64);
				ByteToBit(inbuff1,inbuff,64);
				Xor(IV1, inbuff1,64);
                BitToByte(inbuff, IV1, 64);			 
				jm.Des_Go(oubuff, inbuff, 8, skey, 8, ENCRYPT,flag);
				file2.Write(oubuff,8); 
				for(int i=0;i<8;i++)
				{
					IV[i]=oubuff[i];
				}
				for(int j=0;j<8;j++)
				{
					inbuff[j]=inbuff[j+8];
				}
					for(int k=0;k<8;k++)
				{
					oubuff[k]=oubuff[k+8];
				}
			    
				//(CString)inbuff=inbuff+8;
				//(CString)oubuff=oubuff+8;
				

			}
			       file1.Close();
				   file2.Close();	
					
					
	

}

void CDesDlg::Oncfb() 
{
	    OnChangemiyao();    //密钥
		char inbuff[8];   //输入
		char oubuff[8];   //输出
		char ming[10000];
		int length;
		CFile  file1,file2;
		char IV[8]={5,1,7,9,2,3,6,3};
		int flag=1;
		DES jm; //实例化加密类,得到对象jm
		bool h1[8];//加密后的高8位
		bool plain1[8];	//明文8位

		file1.Open("mingwen.txt",CFile::modeRead);		
		length=file1.GetLength();
		file1.Read(ming,length);
		file1.Close();
		file2.Open("miwen.txt",CFile::modeCreate | CFile::modeWrite);
	
		for(int n=0;n<length;n++)
		{
			for(int j=0;j<8;j++)
				inbuff[j]=IV[j];
			jm.Des_Go(oubuff, inbuff, 8, skey, 8, ENCRYPT,flag);
			char h[1];
			h[0]=oubuff[7];
			char plain[1];
			plain[0]=ming[n];
			ByteToBit(h1,h,8);
			ByteToBit(plain1,plain,8);
			Xor(h1,plain1,8);
			BitToByte(h, h1, 8);
			file2.Write(h,1);
            
			for(int k=0;k<7;k++)
			IV[k+1]=IV[k];		
		    IV[0]=h[0];
		}
		    file2.Close();


	
}

void CDesDlg::OnJ_cbc() 
{
	// TODO: Add your control notification handler code here
		OnChangemiyao();    //密钥
	    //char skey[8]={1,1,1,1,1,1,1,1};
		char inbuff[8];   //输入  
		char oubuff[8];   //输出 
		int length;
		CFile  file1;
		CFile  file2;
		char IV[8]={1,1,1,10,2,3,6,3};
		int flag=1;
		DES jm; //实例化加密类,得到对象jm
		bool IV1[64];
		bool oubuff1[64];		
		file1.Open ("miwen.txt",CFile::modeRead);			
		length=file1.GetLength();
		int m;
		m=length/8;	
		file2.Open("jiemi.txt",CFile::modeCreate | CFile::modeWrite);
			for(int n=0;n<m;n++)
			{				
				file1.Read(inbuff,8);							 
				jm.Des_Go(oubuff, inbuff, 8, skey, 8, DECRYPT,flag);		
            	ByteToBit(IV1,IV,64);
				ByteToBit(oubuff1,oubuff,64); 
				Xor(IV1, oubuff1,64); 
                BitToByte(oubuff, IV1, 64);				
			//	if(file2.Open("miwen.txt",CFile::modeCreate | CFile::modeWrite))
				//{
					file2.Write(oubuff,8);
					//file.Close();
				//}
				for(int i=0;i<8;i++)
				{
					IV[i]=inbuff[i];
				}
				for(int j=0;j<8;j++)
				{
					inbuff[j]=inbuff[j+8];
				}
					for(int k=0;k<8;k++)
				{
					oubuff[k]=oubuff[k+8];
				}
			    
				//(CString)inbuff=inbuff+8;
				//(CString)oubuff=oubuff+8;
				

			}
			       file1.Close();
				   file2.Close();	
					
					
	
		
}

void CDesDlg::OnJ_cfb() 
{
	// TODO: Add your control notification handler code here
    	OnChangemiyao();    //密钥
		char inbuff[8];   //输入
		char oubuff[8];   //输出 
		char mi[10000];
		int length;
		CFile file1,file2;
		char IV[8]={5,1,7,9,2,3,6,3};
		int flag=1;
		DES jm; //实例化加密类,得到对象jm
		bool h1[8];//加密后的高8位
		bool crypt1[8];	//密文8位

		file1.Open ("miwen.txt",CFile::modeRead);
		length=file1.GetLength();
		file1.Read(mi,length);
		file1.Close();
		file2.Open("jiemi.txt",CFile::modeCreate | CFile::modeWrite);
        
		for(int n=0;n<length;n++)
		{
			for(int j=0;j<8;j++)
			inbuff[j]=IV[j];
			jm.Des_Go(oubuff, inbuff, 8, skey, 8, ENCRYPT,flag);
			char h[1];
			h[0]=oubuff[7]; 
			char crypt[1];
			crypt[0]=mi[n];			
			ByteToBit(h1,h,8);
			ByteToBit(crypt1,crypt,8);
			Xor(h1,crypt1,8);
			BitToByte(h, h1, 8);
			file2.Write(h,1);
			for(int k=0;k<7;k++)
			IV[k+1]=IV[k];
			IV[0]=crypt[0];
		}
		file2.Close();
}



void CDesDlg::Onofb() 
{
	// TODO: Add your control notification handler code here
		OnChangemiyao();  
		char inbuff[8];   //输入  
		char oubuff[8];   //输出
		char ming[10000];
		int length;
		CFile  file1,file2;
		char IV[8]={5,1,7,9,2,3,6,3};
		int sign=1;
		DES jm; //实例化加密类,得到对象jm
		bool h1[8];//加密后的高8位
		bool plain1[8];	//明文8位	

		file1.Open("mingwen.txt",CFile::modeRead);			
		length=file1.GetLength();
		file1.Read(ming,length);
		file1.Close();
		file2.Open("miwen.txt",CFile::modeCreate | CFile::modeWrite);
	
		for(int n=0;n<length;n++)
		{
			for(int j=0;j<8;j++)
				inbuff[j]=IV[j];
			jm.Des_Go(oubuff, inbuff, 8, skey, 8, ENCRYPT,sign);
			char h[1];
			h[0]=oubuff[7];
			for(int k=0;k<7;k++)
				IV[k+1]=IV[k];	
			IV[0]=h[0];
			char plain[1];
			plain[0]=ming[n];
			ByteToBit(h1,h,8);
			ByteToBit(plain1,plain,8);
			Xor(h1,plain1,8);
			BitToByte(h, h1, 8);
			file2.Write(h,1);
		}
		file2.Close();		
	
}

void CDesDlg::OnJ_ofb() 
{
	// TODO: Add your control notification handler code here
		OnChangemiyao();  
	    char inbuff[8];   //输入  
		char oubuff[8];   //输出 
		char mi[10000];
		int length;
		CFile  file1,file2;
		char IV[8]={5,1,7,9,2,3,6,3};
		int flag=1;
		DES jm; //实例化加密类,得到对象jm
		bool h1[8];//加密后的高8位
		bool crypt1[8];	//密文8位
		file1.Open ("miwen.txt",CFile::modeRead);
		length=file1.GetLength();
		file1.Read(mi,length);
		file1.Close();
		file2.Open("jiemi.txt",CFile::modeCreate | CFile::modeWrite);

		for(int n=0;n<length;n++)
		{
			for(int j=0;j<8;j++)
				inbuff[j]=IV[j];
			jm.Des_Go(oubuff, inbuff, 8, skey, 8, ENCRYPT,flag);
			char h[1];
			h[0]=oubuff[7];
			for(int k=0;k<7;k++)
				IV[k+1]=IV[k];
			IV[0]=h[0];
			char crypt[1];
			crypt[0]=mi[n];
			ByteToBit(h1,h,8);
			ByteToBit(crypt1,crypt,8);
			Xor(h1,crypt1,8);
			BitToByte(h, h1, 8);
			file2.Write(h,1);
		}
		file2.Close();  
	
}

void CDesDlg::OnJ_ctr() 
{
	// TODO: Add your control notification handler code here
	OnChangemiyao();    //密钥
    char inbuff[8];
	char oubuff[8];	
	char crypt[8];  
	char mi[10000];
	int length;
	int sign=1;
	long l = 0L;
	bool oubuff1[64];
	bool crypt1[64];	
	CFile  file1, file2;
	DES jm; //实例化加密类,得到对象jm

	file1.Open("miwen.txt",CFile::modeRead);			
	length=file1.GetLength();
	file1.Read(mi,length);
	file1.Close();
	file2.Open("jiemi.txt",CFile::modeCreate | CFile::modeWrite);
	int m=length/8;
	bool t=(!(length%8==0));
	
	for(int n=0;n<(m+t);n++,l++)
	{
		_itoa( l, inbuff, 10 );
		jm.Des_Go(oubuff, inbuff, 8, skey, 8, ENCRYPT,sign);
		for(int i=0;i<8;i++)
		crypt[i]=mi[i+8*n];
		ByteToBit(crypt1, crypt, 64);
		ByteToBit(oubuff1, oubuff, 64);
		Xor(oubuff1, crypt1 ,64);
		BitToByte(oubuff, oubuff1, 64);
		file2.Write(oubuff,8);
	}
}

void CDesDlg::Onctr() 
{
	// TODO: Add your control notification handler code here
	OnChangemiyao();    //密钥	    
	char inbuff[8];
	char oubuff[8];	
	char plain[8];  
	char ming[10000];
	int length;
	int sign=1;
	long l = 0L;
	bool oubuff1[64];
	bool plain1[64];	
	DES jm; //实例化加密类,得到对象jm
	CFile  file1, file2;

	file1.Open("mingwen.txt",CFile::modeRead);			
	length=file1.GetLength();
	int m=length/8;
	if(length%8!=0)file1.SetLength((m+1)*8);
	int length1=file1.GetLength();
    m=length1/8;
	file1.Read(ming,length1);
	file1.Close();
	file2.Open("miwen.txt",CFile::modeCreate | CFile::modeWrite);
	for(int n=0;n<m;n++,l++)
	{
		_itoa( l, inbuff, 10 );
		jm.Des_Go(oubuff, inbuff, 8, skey, 8, ENCRYPT,sign);
		for(int i=0;i<8;i++)
			plain[i]=ming[i+8*n];
		ByteToBit(plain1, plain, 64);
		ByteToBit(oubuff1, oubuff, 64);
		Xor(oubuff1, plain1, 64);
		BitToByte(oubuff, oubuff1, 64);
		file2.Write(oubuff,8);
	}
}

⌨️ 快捷键说明

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