?? desmain.cpp
字號:
#include<iostream>
#include<time.h>
#include <iomanip>
#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盒進行差分分析."<<endl;
BEGIN:
cout<<"請輸入您選擇的操作:\n\t1.加密。\n\t2.解密\n\t3.分析雪崩效應\n\t4.差分分析\n\t5.退出.\n"<<endl;
cin>>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<<"無法打開文件。"<<endl;
goto START;
}
NEXT:
cout<<"處理后文件存為(包括文件名、路徑和擴展名):" ;
cin >> DesFileName;
file2 = fopen(DesFileName,"w+b");
if(file2 == NULL) {
cout<<"您的輸入有誤,請重新輸入。"<<endl;
goto NEXT;
}
//密鑰:
cout<<"請輸入8字節密鑰:";
for( i = 0 ; i < 8 ; i++ )
cin >> buffer[i];
key.ByteToDes(buffer,64);
memset( buffer, (char)0, 8 );
cout <<"密鑰串為:" <<endl;
key.print();
cout << "正在處理,請稍候。。。" << endl;
while ( (numread = fread(buffer, sizeof(char), 8, file ))>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<<"第"<<i<<"輪后不同位的個數"<<count[i]<<endl;
}
void printarray(bool* a, unsigned long l)
{
int i;
for(i = 0 ; i < l ; i ++)
{
//if(i % 8 == 0 )
//cout<<endl;
cout<<a[i];
}
cout<<endl;
}
void genePlain(unsigned long n ,bool* first , bool* second )
{//產生只有一位不同的長度為n兩個明文
int i,j;
// srand(time(NULL));
for(i = 0 ; i < n ; i ++)
{
j = rand() % 2;
first[i] = (bool)j;
second[i] = (bool)j;
}
j = rand() % n;
first[j] = ! first[j];
cout<<"第"<<j+1<<"位不同"<<endl;
}
void geneKey (bool* key)
{
int i,j;
//key = new bool [64];
// _sleep();
// srand(time(NULL));
for( i = 0 ; i < 64 ; i ++)
{
j = rand() % 2;
key[i] = (bool)j ;
}
}
void NDS()
{
int i,j;
int n;
char sel;
Des plain1,plain2;
int ND[64][16]={0};
REPUT:
cout<<"請輸入需要進行差分分析的盒號(0-9)"<<endl;
cout<<"-->若要對DES的S盒進行差分分析請選擇0-7."<<endl;
cout<<"-->若要對隨機的S盒進行差分分析盒號請選擇8."<<endl;
cout<<"-->若要對基于線性變換進行差分分析盒號請選擇9."<<endl;
cin>>n;
if(n <0 || n>9)
{//S8為作為與前8個盒的對比
cout<<"您的輸入有誤。"<<endl;
goto REPUT;
}
for(i = 0; i< 64; i++)
for(j = 0; j < 64; j++)
{
plain1.IntToDes(i,6);
plain2.IntToDes(j,6);
plain2.Difference(plain1,plain2,n,&ND[0][0]);
}
cout<<"S"<<n<<"上的差分分布表為:\n\t";
for(i = 0 ; i < 16 ; i++ )
cout<<setw(4)<<i;
cout<<endl;
for(i = 0; i < 64; i ++)
{
cout<<i<<"\t";
for( j = 0; j < 16; j++)
{
cout<<setw(4)<<ND[i][j];
}
cout<<endl;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -