程序分三部分,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 ;
}

c++实现des算法的更多相关文章

  1. DES算法详解

    本文主要介绍了DES算法的步骤,包括IP置换.密钥置换.E扩展置换.S盒代替.P盒置换和末置换. 1.DES算法简介 DES算法为密码体制中的对称密码体制,又被称为美国数据加密标准. DES是一个分组 ...

  2. 基于DES算法加密的防撞库密码系统项目总结

    项目内容:基于DES算法加密的防撞库密码系统 小组名:zqhzkzkj 目标:1.对用户输入的8位字符进行DES加密,要求用户输入8位密钥 2.对于不同的网站,不同的用户名生成不同的密码 小组成员:周 ...

  3. DES 算法的 C++ 与 JAVA 互相加解密

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  4. MFC 简单实现 DES 算法

    前言 徐旭东老师说过学者就应该对知识抱有敬畏之心,所以我的博客的标题总喜欢加上"简单"二字,就是为了提醒自己,自己所学知识只是皮毛,离真理还远矣. DES 算法 DES算法是密码体 ...

  5. 在IOS中使用DES算法对Sqlite数据库进行内容加密存储并读取解密

    在IOS中使用DES算法对Sqlite 数据库进行内容加密存储并读取解密 涉及知识点: 1.DES加密算法: 2.OC对Sqlite数据库的读写: 3.IOS APP文件存储的两种方式及读取方式. 以 ...

  6. Asp.Net 常用工具类之加密——对称加密DES算法(2)

    又到周末,下午博客园看了两篇文章,关于老跳和老赵的程序员生涯,不禁感叹漫漫程序路,何去何从兮! 转眼毕业的第三个年头,去过苏州,跑过上海,从一开始的凌云壮志,去年背起行囊默默回到了长沙准备买房,也想有 ...

  7. AES算法,DES算法,RSA算法JAVA实现

    1     AES算法 1.1    算法描述 1.1.1      设计思想 Rijndael密码的设计力求满足以下3条标准: ① 抵抗所有已知的攻击. ② 在多个平台上速度快,编码紧凑. ③ 设计 ...

  8. 安全体系(一)—— DES算法详解

    本文主要介绍了DES算法的步骤,包括IP置换.密钥置换.E扩展置换.S盒代替.P盒置换和末置换. 安全体系(零)—— 加解密算法.消息摘要.消息认证技术.数字签名与公钥证书 安全体系(二)——RSA算 ...

  9. DES算法原理完整版

    1.所需参数 key:8个字节共64位的工作密钥 data:8个字节共64位的需要被加密或被解密的数据 mode:DES工作方式,加密或者解密 2.初始置换 DES算法使用64位的密钥key将64位的 ...

  10. C#与Java同步加密解密DES算法

    在实际项目中,往往前端和后端使用不同的语言.比如使用C#开发客户端,使用Java开发服务器端.有时出于安全性考虑需要将字符加密传输后,由服务器解密获取.本文介绍一种采用DES算法的C#与Java同步加 ...

随机推荐

  1. ArchLinux 安装笔记 --zz

    为何安装 ArchLinux 为了更深层次的理解 Linux (其实只是闲的蛋疼 准备安装介质 U盘首选,没有之一.自己的本子是 MBR 的,UEFI 神马的我才不知道呢哼! 制作 U 盘启动: Li ...

  2. 突破python缺陷,实现几种自定义线程池 以及进程、线程、协程的介绍

    Python线程 Threading用于提供线程相关的操作,线程是应用程序中工作的最小单元. #!/usr/bin/env python # -*- coding:utf-8 -*- import t ...

  3. SQL高级查询之一

    一,子查询 SELECT e.emp_id, e.fname, e.lname FROM (SELECT emp_id, fname, lname, start_date, title FROM em ...

  4. AOP常用术语

    1.连接点(Joinpoint) 程序执行的某个特定位置:如类开始初始化前,类初始化后,类某个方法调用前,调用后,方法跑出异常后.一个类或一段程序代码拥有一些具有边界性质的特定点.这些代码中的特定点就 ...

  5. C#动态方法调用

    此篇将介绍C#如何在运行时动态调用方法.当某些类型是运行时动态确定时,编译时的静态编码是无法解决这些动态对象或类的方法调用的.此篇则给你一把利剑,让动态对象的方法调用成为可能. 1.动态调用dll里的 ...

  6. php正则获取网页标题、关键字、网页描述代码

    php正则获取网页关键字,代码如下: function get_keywords($html) { $html=strtolower($html); preg_match("@<hea ...

  7. hihoCoder 1391 Countries【预处理+排序+优先队列】2016北京网络赛

    题目:http://hihocoder.com/problemset/problem/1391 题目大意: A和B两个国家互射导弹,每个国家都有一个防御系统,在防御系统开启的时间内可以将到达本国的导弹 ...

  8. vc++ 加载,卸载自己的驱动程序

    用vc++加载自己的驱动程序主要分为以下几个步骤: 1.加载驱动服务 主要要用到以下几个函数 SC_HANDLE WINAPI OpenSCManagerA( __in_opt        LPCS ...

  9. h5嵌入视频遇到的bug及总结---转载

    最近做的一个h5活动因为嵌入视频而发现了好多以前从未发现的问题,在测试的时候不同系统不同版本不同环境等多多少少都出现了些问题,搞得我也是焦头烂额的,不过好在最终问题都解决了,自己也学到了好多东西,为了 ...

  10. MySQL数据库监控

    MySQL MTOP由PHP和Python开发,所以监控机需要安装PHP运行环境和Python环境.需要的核心包如下: 1.MySQL 5.0及以上(用来存储监控系统采集的数据) 2.Apache 2 ...