?? des.cpp
字號:
#include "Cipher.h"
void main()
{
cout<<"***************DES加解密**************"<<endl<<endl;
text3_index = 0;
char ch;
do{
int choice;
do{
cout<<"Choose Please:"<<endl;
cout<<"1.DES加密"<<endl;
cout<<"2.DES解密"<<endl;
cin>>choice;
}while(choice!=1&&choice!=2);
int keylen;
int textblk[64];
char textblock[8];
int index;
switch(choice){
case 1:
cout<<"請輸入明文:";
cin>>text1;
keylen = 0;
do{
cout<<"請輸入密鑰(長度為7個字節):";
cin>>key;
keylen=strlen(key);
}while(keylen<7||keylen>7);
SKeyProduce(key); //生成16輪的密鑰,存于全局變量SubKey中
char *tp;
tp=text1;
while(true){
if(strlen(tp)>8){
strncpy(textblock,tp,8); //注意textblock沒有結束符
/*處理明文塊*/
index=63;
for(int i=7;i>=0;i--){
for(int j=0;j<8;j++){
textblk[index--]=(int)textblock[i]%2;
textblock[i]=textblock[i]/2;
}
}
cipher(textblk,choice);
tp=tp+8;
}
else if(strlen(tp)==8){
strncpy(textblock,tp,8); //注意textblock沒有結束符
/*處理明文塊*/
index=63;
for(int i=7;i>=0;i--){
for(int j=0;j<8;j++){
textblk[index--]=(int)textblock[i]%2;
textblock[i]=textblock[i]/2;
}
}
cipher(textblk,choice);
break;
}
else{
strcpy(textblock,tp);
int k=strlen(textblock)+1; //若不足64位,則用0000 0000補足,存在結束符
while(k<8)textblock[k++]=NULL;
/*處理明文塊*/
index=63;
for(int i=7;i>=0;i--){
for(int j=0;j<8;j++){
textblk[index--]=(int)textblock[i]%2;
textblock[i]=textblock[i]/2;
}
}
cipher(textblk,choice);
break;
}
}
break;
case 2:
index = 0;
while(index<text3_index){
for(int k=0;k<64;k++)
textblk[k] = text3[index+k];
cipher(textblk,choice);
index+=64;
}
break;
}
cout<<"經加(解)密后:";
cout<<text2<<endl;
int text2_len = strlen(text2);
for(int y=0;y<text2_len;y++)
text2[y]=NULL;
/* file.open("file.txt",ios::out|ios::trunc,0);
if(!file){
cout<<"Can not open cleartext.txt file to write!"<<endl;
cin.get();
return;
}
file<<text2;
file.close();
*/
cout<<"Want to quit now?(Y/y or N/n)";
cin>>ch;
}while(ch!='Y'&&ch!='y');
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -