程序分三部分,des头文件,des类实现,main函数调用。

 //panda
//2013-4-13
//des //des.h class DES
{
private:
//public:
//明文
char msg[];
bool bmsg[];
//密钥
char key[];
bool bkey[];
//16个子密钥
bool subkey[][];
//l0 r0中间变量
bool rmsgi[],lmsgi[];//第i个
bool rmsgi1[],lmsgi1[];//第i+1个
//密文
bool bcryptedmsg[];
char cryptedmsg[];
//解密的结果
bool bdecipher[];
char decipher[];
private:
//静态常量 //不允许在类内初始化
//初始值换ip
const static int ip[];
//子密钥
//置换选择1
const static int c0[];
const static int d0[];
//循环左移表
const static int keyoff[];
//置换选择2
const static int di[];
//加密函数
//e运算
const static int e_operate[];
//sbox
const static int sbox[][];
//置换运算p
const static int p_operate[];
//逆初始置换ip
const static int back_ip[];
//位掩码
const static char bitmask[];
public:
//设置明文和密钥
//_length要小于或等于8
void SetMsg(char* _msg,int _length);
void SetKey(char* _msg,int _length);
//生产子密钥
void ProduceSubKey();
//总的的加密流程
void Crypte();
//解密
void Decipher();
//输出密文
void OutPutCryptedMsg();
//二进制转成字符
void Bit2Char(bool* _barray,char* _carray);//length=64
//输出解密后的明文
void OutPutDecipher();
private:
//字符转成二进制,并保存到64位bool数组中
void Char2Bit(char* _carray,bool* _barray,int length);
////二进制转成字符
//void Bit2Char(bool* _barray,char* _carray);//length=64
//初始置换
void InitSwap(bool in[]);
//初始逆置换
void InitReSwap(bool out[]);
//循环左移
void SubKeyOff(bool* _subkey,int _off);
//e运算操作函数
void EOperation(bool a[],bool b[]);
//模2相加
//相同为0 不同为1
void Mode2Add(bool a[],bool b[],bool c[],int length);
//sbox
void DealSBox(bool in[],bool out[]);
void _DealSBox(bool in[],bool out[],int box);
//p opraration
void POperation(bool temp[],bool result[]);
//加密函数
void CrypteFunction(bool in[],int isubkey,bool out[]); //数组之间赋值
void CopyArray(bool array1[],bool array2[],int size);
}; //2013-4-13
//panda
//des
#include<string>
#include<iostream>
#include"des.h"
using namespace std;
//静态常量
const int DES::ip[]={
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,
};
const int DES::c0[]={
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,
};
const int DES::d0[]={
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,
};
const int DES::keyoff[]={
,,,,,,,,,,,,,,,
};
const int DES::di[]={
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,
};
const int DES::e_operate[]={
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,
};
const int DES::sbox[][]={
{
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , ,
}, {
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , ,
}, {
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , ,
}, {
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , ,
}, {
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , ,
}, {
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , ,
}, {
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , ,
}, {
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , ,
}
};
const int DES::p_operate[]={
,,,,
,,,,
,,,,
,,,,
,,,,
,,,,
,,,,
,,,
};
const int DES::back_ip[]={
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,
};
const char DES::bitmask[]={ ,,,,,,,}; //实现函数 //
//设置明文
//
void DES::SetMsg(char* _msg,int _length)
{
if (_length>)
{
return;
}
for (int i = ; i < _length; i++)
{
msg[i]=_msg[i];
}
//转换成二进制
Char2Bit(msg,bmsg,); };
//
//设置密钥
//
void DES::SetKey(char* _key,int _length)
{
if (_length>)
{
return;
}
for (int i = ; i < _length; i++)
{
key[i]=_key[i];
}
//转成二进制
Char2Bit(key,bkey,);
};
//
//字符转成二进制
//ok length字符数组的长度
void DES::Char2Bit(char* _carray,bool* _barray,int length)
{
//int index=0;
for (int i = ; i <length; i++)
{
for (int j = ; j < ; j++)
{
_barray[i*+-j]=(_carray[i]>>j)&;
}
}
};
//
//二进制转成字符
//
void DES::Bit2Char(bool* _barray,char* _carray)
{
char temp;
for (int i = ; i < ; i++)
{
//数学方法转成字符
temp=;
for (int j = ; j < ; j++)
{
if (_barray[i*+j]==)
{
temp|=bitmask[j];
}
}
//cout<<temp;
_carray[i]=temp;
}
};
//
//初始置换函数
//ok
void DES::InitSwap(bool in[])
{
//打乱
for (int i = ; i < ; i++)
{
lmsgi[i]=in[ip[i]-];
rmsgi[i]=in[ip[i+]-];
}
};
//
//初始逆置换函数
//ok
void DES::InitReSwap(bool out[])
{
//组合成64数组
bool temp[];
for (int i = ; i < ; i++)
{
temp[i]=rmsgi[i];
temp[+i]=lmsgi[i];
}
//按照逆ip矩阵
for (int i = ; i < ; i++)
{
out[i]=temp[back_ip[i]-];
}
};
//
//循环左移
//ok
void DES::SubKeyOff(bool* _subkey,int _off)
{
//有没有更好的办法???
bool temp;
for (int i = ; i < _off; i++)
{
temp=_subkey[];
for (int i = ; i < ; i++)
{
_subkey[i]=_subkey[i+];
}
_subkey[]=temp;
}
};
//
//生产子密钥
//ok
void DES::ProduceSubKey()
{
//置换选择1
bool ctemp[],dtemp[];
for (int i = ; i < ; i++)
{
ctemp[i]=bkey[c0[i]-];
dtemp[i]=bkey[d0[i]-];
}
bool keytemp[];
for (int i = ; i < ; i++)
{
//循环左移
SubKeyOff(ctemp,keyoff[i]);
SubKeyOff(dtemp,keyoff[i]);
//合并成一个56数组
for (int j = ; j <; j++)
{
keytemp[j]=ctemp[j];
keytemp[+j]=dtemp[j];
}
//置换选择2
for (int j = ; j < ; j++)
{
subkey[i][j]=keytemp[di[j]-];
}
}
};
//
//e运算
//ok
void DES::EOperation(bool a[],bool b[])
{
for (int i = ; i < ; i++)
{
b[i]=a[e_operate[i]-];
}
};
//
//模2想加
//ok
void DES::Mode2Add(bool a[],bool b[],bool c[],int length)
{
for (int i = ; i < length; i++)
{
if (a[i]==b[i])
{
c[i]=;
}else
{
c[i]=;
}
}
};
//
//sbox处理
//ok
void DES::DealSBox(bool in[],bool out[])
{
bool _in[],_out[];
//8个盒子
for (int i = ; i < ; i++)
{
//提取盒子
for (int j = ; j < ; j++)
{
_in[j]=in[i*+j];
}
//压缩
_DealSBox(_in,_out,i);
//放进out数组
for (int jj = ; jj < ; jj++)
{
out[i*+jj]=_out[jj];
}
}
};
//
//_dealsbox
//ok
void DES::_DealSBox(bool in[],bool out[],int box)
{
int raw,col;
raw=in[]*+in[];//转换成十进制 行
col=in[]***+in[]**+in[]*+in[];//列
int result=sbox[box][raw*+col];
//转成二进制
for (int i = ; i >=; i--)
{
out[i]=(result>>(-i))&;
}
};
//
//p操作
//ok
void DES::POperation(bool temp[],bool result[])
{
for (int i = ; i < ; i++)
{
result[i]=temp[p_operate[i]-];
}
};
//
//加密函数
//isubkey表明用那个子密钥加密 ok
void DES::CrypteFunction(bool in[],int isubkey,bool out[])
{
//e 操作
bool temp1[];
EOperation(in,temp1);
bool temp2[];
Mode2Add(temp1,(bool *)subkey[isubkey],temp2,);//ok
//盒子压缩
bool temp3[];
DealSBox(temp2,temp3);
//置换运算p
POperation(temp3,out); };
//
// des加密流程
//ok
void DES::Crypte()
{
//直接用bmsg明文
//直接用cryptedmsg存放密文
bool temp1[],temp2[];
//初始置换ip
InitSwap(bmsg);
//16轮迭代
for (int i = ; i < ; i++)
{
if (i%==)
{
//L1=R0
CopyArray(rmsgi,lmsgi1,);
//f(R0,k0)
CrypteFunction(rmsgi,i,temp1);
//L0+f(R0,k0)
Mode2Add(lmsgi,temp1,temp2,);
//R1=L0+f(R0,k0)
CopyArray(temp2,rmsgi1,);
}else
{
//L2=R1
CopyArray(rmsgi1,lmsgi,);
//f(R1,k1)
CrypteFunction(rmsgi1,i,temp1);
//L1+f(R1,k1)
Mode2Add(lmsgi1,temp1,temp2,);
//R2=L1+f(R1,k1)
CopyArray(temp2,rmsgi,);
}
} //逆初始置换ip
InitReSwap(bcryptedmsg);
//转成字符
Bit2Char(bcryptedmsg,cryptedmsg);
};
//
//数组赋值
//ok
void DES::CopyArray(bool content[],bool empty[],int size)
{
for (int i = ; i < size; i++)
{
empty[i]=content[i];
}
};
//
//解密
//ok
void DES::Decipher()
{
bool temp1[],temp2[];
//初始置换ip
InitSwap(bcryptedmsg);
//16轮迭代加密 for (int i = ; i < ; i++)
{
if (i%==)
{
//L1=R0
CopyArray(rmsgi,lmsgi1,);
//f(R0,k0)
CrypteFunction(rmsgi,-i,temp1);
//L0+f(R0,k0)
Mode2Add(lmsgi,temp1,temp2,);
//R1=L0+f(R0,k0)
CopyArray(temp2,rmsgi1,);
}else
{
//L2=R1
CopyArray(rmsgi1,lmsgi,);
//f(R1,k1)
CrypteFunction(rmsgi1,-i,temp1);
//L1+f(R1,k1)
Mode2Add(lmsgi1,temp1,temp2,);
//R2=L1+f(R1,k1)
CopyArray(temp2,rmsgi,);
}
}
//逆初始置换ip
InitReSwap(bdecipher);
//转成字符
Bit2Char(bdecipher,decipher); };
//
//输出密文
//
void DES::OutPutCryptedMsg()
{
//Bit2Char(bcryptedmsg,cryptedmsg);
cout<<endl<<"密文:";
for (int i = ; i < ; i++)
{
cout<<cryptedmsg[i]<<' ';
}
};
//
//输出解密明文
//
void DES::OutPutDecipher()
{
//Bit2Char(bdecipher,decipher);
cout<<endl<<"解密:";
for (int i = ; i < ; i++)
{
cout<<decipher[i]<<' ';
}
cout<<endl;
}; #include<iostream>
#include"des.h"
#include<string.h>
using namespace std; int main()
{
//教材的测试数据
char msg[]={'','','','','','','',''};
char key[]={'','','','','','','',''};
cout<<"明文:";
for (int i = ; i < ; i++)
{
cout<<msg[i]<<' ';
}
DES des;
//设置明文
des.SetMsg(msg,);
//设置密钥
des.SetKey(key,);
//生产子密钥
des.ProduceSubKey();
//加密
des.Crypte();
//输出密文
des.OutPutCryptedMsg();
//解密
des.Decipher();
//输出解密后的明文
des.OutPutDecipher(); system("pause");
return ;
}

最新文章

  1. java并发容器类
  2. Centos5.8 安装openvpn
  3. 【转】 linux之sed用法
  4. iOS - OC NSLocale 本地化信息
  5. Extjs随笔
  6. 一.CSS工作原理
  7. andriod
  8. python bisect模块
  9. Ubuntu Linux---控制用户权限:root特权/sudo
  10. php中curl、fsockopen的应用
  11. java生成Excel文件,下载
  12. python ----字符串基础练习题30道
  13. linux系统下如何批量更改文件夹里面所有相同字符【转】
  14. Python在VSCode中进入交互界面调试
  15. vue实现点击区域外部的区域,关闭该区域
  16. JavaWeb(HTML +css+js+Servlet....)
  17. 【HDU】1693:Eat the Trees【插头DP】
  18. DMA(Direct Memory Access)简介
  19. Gradle2.0用户指南翻译——第三章. 教程
  20. 4-2:实现cp命令

热门文章

  1. 比较各大挪动门户网站淘宝、京东、网易、新浪、腾讯meta标签的异同
  2. SQL数据字符串的拆分
  3. JQuery EasyUI validatebox(验证框)
  4. BRIEF 特征描述子
  5. 在苹果手机上input有内阴影怎么去除
  6. sublime text 2中Emmet8个常用的技巧
  7. javascript概述
  8. this和super
  9. Markdown语言.md文件
  10. LoadRunner关联函数的脚本实例--如何操作关联参数