www.pudn.com > cryptology.rar > desmain.cpp


#include 
#include 
#include  
 
#include "DES.h" 
 
using namespace std; 
 
#define MAX_FILENAME 80 
 
void geneKey (bool* key); 
void genePlain(unsigned long  n ,bool* first , bool* second ); 
void printarray(bool* a, unsigned long l); 
void xue(); 
void NDS();//差分分析 
 
int main() 
{ 
 
	int i; 
	int numread; 
 
	FILE* file; 
	FILE* file2; 
    char SouFileName[MAX_FILENAME];   
    char DesFileName[MAX_FILENAME];   
	char* buffer; 
	char sel; 
 
	Des key; 
    Des plain; 
	Des cipher; 
 
	buffer = new char[8]; 
 
	cout<<"程序:DES 加密解密并分析雪崩效应及对S盒进行差分分析."<>sel; 
 
	if( (sel - '1') <0  && (sel - '1' ) > 4) 
	{ 
		cout << "您的选择有误,请重新输入!"; 
		goto   BEGIN; 
	} 
	 
	if (sel == '3') 
	{ 
		xue(); 
		goto	BEGIN; 
	//	return 0; 
	} 
	if(sel == '4') 
	{ 
		NDS(); 
		goto    BEGIN; 
	//	return 0; 
	} 
	if (sel == '5')  
	{ 
		return 0; 
	} 
 
START: 
    cout << "请输入需要\"处理\"的文本文件名(注意包括其路径及扩展名):"; 
    cin >> SouFileName ;	 
	if((file = fopen(SouFileName,"r+b")) == NULL) 
	{ 
		cout<<"无法打开文件。"<> DesFileName;  
	file2 = fopen(DesFileName,"w+b"); 
	if(file2 == NULL) { 
         cout<<"您的输入有误,请重新输入。"<> buffer[i]; 
	 key.ByteToDes(buffer,64); 
	 		memset( buffer, (char)0, 8 ); 
 
	 cout <<"密钥串为:" <0) 
	{// 
 
		plain.ByteToDes(buffer,64); 
 
		if(sel == '1') 
 
		{  
			cipher = plain.Encrpty(key); 
		} 
		else if(sel == '2') 
		{ 
			cipher = plain.Decrpty(key); 
		//cipher = plain ; 
		} 
		//cipher.print(); 
		if((sel == '1') || ( sel == '2')) 
		{ 
			buffer = cipher.DesToByte(); 
			fwrite(buffer,sizeof(char),8,file2); 
		} 
  		memset( buffer, (char)0, 8 ); 
	 
	} 
 
	delete [] buffer; 
    fclose(file);           
    fclose(file2);   
	cout << "处理结束,请查看。谢谢!" << endl; 
 
 
 
	return 0; 
} 
 
void xue() 
{ 
	unsigned long len = 0; 
	bool* first , * second ,*k; 
	k = new bool [64]; 
	 
	srand(time(NULL)); 
	while(len <=0 || len >= 1024*8) 
	{ 
		cout << "请输入明文长度(0 - 9192)(作为测试先取64的整数倍))"; 
		cin  >>  len; 
	} 
	first  = new bool [len]; 
	second = new bool [len]; 
 
	genePlain(len, first, second); 
	Des fir(first,len); 
	Des sec(second,len); 
 
	cout<<"两个明文串分别为:\n"; 
	printarray(first,len); 
	printarray(second,len); 
	 
	cout<<"产生的64比特随机密钥为:\n"; 
	geneKey(k); 
	Des key(k,64); 
	printarray(k,64); 
 
	delete [] first; 
	delete [] second; 
	delete [] k; 
 
	int i; 
 
	unsigned long count[18] = {0}; 
 
	for( i = 0 ; i < (len/64) ; i++) 
	{ 
		Des plain1(fir.getptr(),64); 
		Des plain2(sec.getptr(),64); 
 
		plain1.xuebeng(key, plain2,  count); 
 
		fir << 64; 
		sec << 64; 
	} 
	for(i = 1 ; i < 17 ; i++ ) 
		cout<<"第"<若要对DES的S盒进行差分分析请选择0-7."<若要对随机的S盒进行差分分析盒号请选择8."<若要对基于线性变换进行差分分析盒号请选择9."<>n; 
	 
 
	if(n <0 || n>9) 
	{//S8为作为与前8个盒的对比 
		cout<<"您的输入有误。"<