基于c++的DES加密解密算法实现.docx
- 文档编号:6413417
- 上传时间:2023-01-06
- 格式:DOCX
- 页数:18
- 大小:19.27KB
基于c++的DES加密解密算法实现.docx
《基于c++的DES加密解密算法实现.docx》由会员分享,可在线阅读,更多相关《基于c++的DES加密解密算法实现.docx(18页珍藏版)》请在冰豆网上搜索。
基于c++的DES加密解密算法实现
1.头文件:
DES.h
设计方法:
类的共有接口只设计一个,即加密解密函数,用一个bool参数区分是加密还是解密。
其他像读文件,写文件,加密变换函数等等设置为私有涉及内容:
文件操作,STL向量,数制变换,位操作及函数设计等等。
代码:
#include
#include
#include
#include
#include
#include
usingnamespacestd;
/************************************************************************/
/************************************************************************/
/*DES类声明*/
classDES
{
public:
DES();/*构造函数*/
voidencrypt(boolflag=true);/*加解密函数*/
private:
stringplaintextFilePath_;/*明文文件路径*/
stringciphertextFilePath_;/*密文文件路径*/
stringkeyFilename_;/*密钥文件路径*/
stringplaintext_;/*明文存储*/
stringciphertext_;/*密文存储*/
stringkey_;/*密钥存储*/
vector
voidgetsubkey_();/*计算子密钥*/
voidsetPlaintextFilePath_();/*设置明文路径*/
voidsetCiphertextFilePath_();/*设置密文路径*/
voidsetKeyFilePath_();/*设置密钥路径*/
stringreadData_(stringfilename);/*读取文件*/
voidsaveData_(stringfilename,stringdata);/*保存文件*/
vector
stringbit2string_(vector
vector
vector
vector
vector
vector
vector
vector
/************************************************************************/
/*变换矩阵*/
staticconstunsignedintPC_1_[56];
staticconstunsignedintPC_2_[48];
staticconstunsignedintLOOP_[16];
staticconstunsignedintIP_[64];
staticconstunsignedintIPR_[64];
staticconstunsignedintSBOX_[8][4][16];
staticconstunsignedintE_[48];
staticconstunsignedintP_[32];
/************************************************************************/
};
2.DES加密代码
创建基于DES加解密的函数,以备后面的代码调用。
代码:
#include"DES.h"
/************************************************************************/
/************************************************************************/
/*静态常量放在这里*/
constunsignedintDES:
:
IP_[64]=
{
58,50,42,34,26,18,10,2,60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6,64,56,48,40,32,24,16,8,
57,49,41,33,25,17,9,1,59,51,43,35,27,19,11,3,
61,53,45,37,29,21,13,5,63,55,47,39,31,23,15,7
};
constunsignedintDES:
:
IPR_[64]=
{
40,8,48,16,56,24,64,32,39,7,47,15,55,23,63,31,
38,6,46,14,54,22,62,30,37,5,45,13,53,21,61,29,
36,4,44,12,52,20,60,28,35,3,43,11,51,19,59,27,
34,2,42,10,50,18,58,26,33,1,41,9,49,17,57,25
};
constunsignedintDES:
:
PC_1_[56]=
{
/*注释的部分是对应64位带奇偶校验的*/
/*57,49,41,33,25,17,9,1,58,50,42,34,26,18,
10,2,59,51,43,35,27,19,11,3,60,52,44,36,
63,55,47,39,31,23,15,7,62,54,46,38,30,22,
14,6,61,53,45,37,29,21,13,5,28,20,12,4*/
50,43,36,29,22,15,8,1,51,44,37,30,23,16,
9,2,52,45,38,31,24,17,10,3,53,46,39,32,
56,49,42,35,28,21,14,7,55,48,41,34,27,20,
13,6,54,47,40,33,26,19,12,5,25,18,11,4
};
constunsignedintDES:
:
PC_2_[48]=
{
/*注释的部分是对应64位带奇偶校验的*/
/*14,17,11,24,1,5,3,28,15,6,21,10,
23,19,12,4,26,8,16,7,27,20,13,2,
41,52,31,37,47,55,30,40,51,45,33,48,
44,49,39,56,34,53,46,42,50,36,29,32*/
14,17,11,24,1,5,3,28,15,6,21,10,
23,19,12,4,26,8,16,7,27,20,13,2,
13,24,3,9,19,27,2,12,23,17,5,20,
16,21,11,28,6,15,18,14,12,8,1,4
};
constunsignedintDES:
:
LOOP_[16]=
{
1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1
};
constunsignedintDES:
:
SBOX_[8][4][16]=
{
//S1
14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,
0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8,
4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0,
15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13,
//S2
15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10,
3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5,
0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15,
13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9,
//S3
10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8,
13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1,
13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7,
1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12,
//S4
7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15,
13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9,
10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4,
3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14,
//S5
2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9,
14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6,
4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14,
11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3,
//S6
12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11,
10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8,
9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6,
4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13,
//S7
4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1,
13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6,
1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2,
6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12,
//S8
13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7,
1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2,
7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8,
2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11
};
constunsignedintDES:
:
E_[48]=
{
32,1,2,3,4,5,4,5,6,7,8,9,
8,9,10,11,12,13,12,13,14,15,16,17,
16,17,18,19,20,21,20,21,22,23,24,25,
24,25,26,27,28,29,28,29,30,31,32,1
};
constunsignedintDES:
:
P_[32]=
{
16,7,20,21,29,12,28,17,1,15,23,26,5,18,31,10,
2,8,24,14,32,27,3,9,19,13,30,6,22,11,4,25
};
/************************************************************************/
/************************************************************************/
/*DES类实现*/
DES:
:
DES():
subkey_(16,vector
{
/*构造函数*/
}
voidDES:
:
encrypt(boolflag/*=true*/)
{
/*加密函数*/
/*读取文件*/
vector
if(flag)
{
/*加密*/
setPlaintextFilePath_();
plaintext_=readData_(plaintextFilePath_);
textBit=string2bit_(plaintext_);/*不够64bit的整数,补0,如下*/
unsignedintzeroFill=(textBit.size()%64)?
(64-textBit.size()%64):
0;
for(unsignedinti=0;i { textBit.push_back(0); } } else { /*解密*/ setCiphertextFilePath_(); ciphertext_=readData_(ciphertextFilePath_); textBit=string2bit_(ciphertext_); } /*读取密钥文件*/ getsubkey_(); /*循环读取64bit加密*/ unsignedintlen=textBit.size(); unsignedintcount=0; vector while(count<(len/64)) { vector for(unsignedinti=64*count;i<64*count+64;i++) { textBitTmp.push_back(textBit[i]); } /*IP置换*/ vector textBitTmp_IP=vecReplace_(textBitTmp,IP_); /*划分L0和R0*/ vector vector for(inti=0;i<32;i++) { L[0][i]=textBitTmp_IP[i]; } for(inti=0;i<32;i++) { R[0][i]=textBitTmp_IP[i+32]; } /*迭代生成L1-L16,R1-R16*/ for(inti=1;i<17;i++) { L[i]=R[i-1]; if(flag) { R[i]=vecXor_(L[i-1],f_(R[i-1],subkey_[i-1])); } else { R[i]=vecXor_(L[i-1],f_(R[i-1],subkey_[16-i])); } } vector for(unsignedinti=0;i<64;i++) { secret.push_back(RL[IPR_[i]-1]); } count++; } if(flag) { ciphertext_=bit2string_(secret); setCiphertextFilePath_(); saveData_(ciphertextFilePath_,ciphertext_); } else { plaintext_=bit2string_(secret); setPlaintextFilePath_(); saveData_(plaintextFilePath_,plaintext_); } } voidDES: : setPlaintextFilePath_() { /*设置明文路径*/ stringplaintextFilePath; cout<<"请输入明文文件路径(例如: c: //plaintext.txt): "; cin>>plaintextFilePath; plaintextFilePath_=plaintextFilePath; } voidDES: : setCiphertextFilePath_() { /*设置密文路径*/ stringciphertextFilePath; cout<<"请输入密文文件路径(例如: c: //ciphertext.txt): "; cin>>ciphertextFilePath; ciphertextFilePath_=ciphertextFilePath; } voidDES: : setKeyFilePath_() { /*设置密钥路径*/ stringkeyFilename; cout<<"请输入密钥文件路径(例如: c: //key.txt): "; cin>>keyFilename; keyFilename_=keyFilename; } stringDES: : readData_(stringfilename) { /*通用函数: 读取文件内容到string中,包含空格和回车符等*/ ifstreamin; ostrstreamos; in.open(filename.c_str(),ios: : in); os< in.close(); returnos.str(); } voidDES: : saveData_(stringfilename,stringdata) { /*通用函数: 保存数据到文件中,包含空格和回车*/ ofstreamout(filename.c_str()); out.write(data.c_str(),data.size()); out.close(); } vector : string2bit_(stringstr) { /*string转为二进制*/ vector for(unsignedinti=0;i { intmask=128; while(mask>0) { v.push_back(((mask&str[i])>0)); mask=mask>>1; } } returnv; } stringDES: : bit2string_(vector { /*二进制转为string*/ assert(v.size()%8==0); unsignedintcount=0,len=v.size(); stringstr; while(count<(len/8)) { intmask=128; intch=0; for(unsignedinti=8*count;i<8*count+8;i++) { ch+=(mask&(v[i]*mask)); mask=mask>>1; } str+=(char)ch; count++; } returnstr; } vector : int2bit_(vector { /*整形数0-15化为二进制*/ vector for(unsignedinti=0;i { intmask=8; while(mask>0) { v.push_back(((mask&vecInt[i])>0)); mask=mask>>1; } } returnv; } vector : vecXor_(vect
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 基于 c+ DES 加密 解密 算法 实现
![提示](https://static.bdocx.com/images/bang_tan.gif)