#include "memory.h"
#include "stdio.h"
enum {encrypt,decrypt};//ENCRYPT:加密,DECRYPT:解密
void des_run(char out[],char in[],bool type=encrypt);
//设置密钥
void des_setkey(const char key[]);
static void f_func(bool in[],const bool ki[]);//f函数
static void s_func(bool out[],const bool in[]);//s盒代替
//变换
static void transform(bool *out, bool *in, const char *table, int len);
static void xor(bool *ina, const bool *inb, int len);//异或
static void rotatel(bool *in, int len, int loop);//循环左移
//字节组转换成位组
static void bytetobit(bool *out,const char *in, int bits);
//位组转换成字节组
static void bittobyte(char *out, const bool *in, int bits);
//置换IP表
const static char ip_table[]={,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,};
//逆置换IP-1表
const static char ipr_table[]={,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,};
//E 位选择表
static const char e_table[]={,, , , , ,, , , , , ,, , ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,};
//P换位表
const static char p_table[]={,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,};
//pc1选位表
const static char pc1_table[]={
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,
};
//pc2选位表
const static char pc2_table[]={
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,
};
//左移位数表
const static char loop_table[]={,,,,,,,,,,,,,,,};
//S盒
const static char s_box[][][]={
//s1
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
//s2
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
//s3
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
//s4
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
//s5
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
//s6
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
//s7
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
//s8
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , ,
};
static bool subkey[][];//16圈子密钥
void des_run(char out[],char in[], bool type)
{
static bool m[],tmp[],*li=&m[], *ri=&m[];
bytetobit(m,in,);
transform(m,m,ip_table,);
if(type==encrypt){
for(int i=;i<;i++){
memcpy(tmp,ri,);
f_func(ri,subkey[i]);
xor(ri,li,);
memcpy(li,tmp,);
}
}else{
for(int i=;i>=;i--){
memcpy(tmp,li,);
f_func(li,subkey[i]);
xor(li,ri,);
memcpy(ri,tmp,);
}
}
transform(m,m,ipr_table,);
bittobyte(out,m,);
}
void des_setkey(const char key[])
{
static bool k[], *kl=&k[], *kr=&k[];
bytetobit(k,key,);
transform(k,k,pc1_table,);
for(int i=;i<;i++)
{
rotatel(kl,,loop_table[i]);
rotatel(kr,,loop_table[i]);
transform(subkey[i],k,pc2_table,);
}
}
void f_func(bool in[],const bool ki[])
{
static bool mr[];
transform(mr,in,e_table,);
xor(mr,ki,);
s_func(in,mr);
transform(in,in,p_table,);
}
void s_func(bool out[],const bool in[])
{
for(char i=,j,k;i<;i++,in+=,out+=)
{
j=(in[]<<)+in[];
k=(in[]<<)+(in[]<<)+(in[]<<)+in[];
bytetobit(out,&s_box[i][j][k],);
}
}
void transform(bool *out,bool *in,const char *table,int len)
{
static bool tmp[];
for(int i=;i<len;i++)
tmp[i]=in[table[i]-];
memcpy(out,tmp,len);
}
void xor(bool *ina,const bool *inb,int len)
{
for(int i=;i<len;i++)
ina[i]^=inb[i];
}
void rotatel(bool *in,int len,int loop)
{
static bool tmp[];
memcpy(tmp,in,loop);
memcpy(in,in+loop,len-loop);
memcpy(in+len-loop,tmp,loop);
}
void bytetobit(bool *out,const char *in,int bits)
{
for(int i=;i<bits;i++)
out[i]=(in[i/]>>(i%)) &;
}
void bittobyte(char *out,const bool *in,int bits)
{
memset(out,,(bits+)/);
for(int i=;i<bits;i++)
out[i/]|=in[i]<<(i%);
}
void main()
{
char key[]={'p','r','o','g','r','a','m'},str[];
puts("*****************DES***********************");
printf("\n");
printf("\n");
puts("please input your words");
gets(str);
printf("\n");
puts("****************************************");
des_setkey(key);
des_run(str,str,encrypt);
puts("after encrypting:");
puts(str);
printf("\n");
puts("****************************************");
puts("after decrypting:");
des_run(str,str,decrypt);
puts(str);
printf("\n");
puts("****************************************");
printf("\n");
}

DES算法实现(C++版)的更多相关文章

  1. PHP版DES算法加密数据(3DES)另附openssl_encrypt版本

    PHP版DES算法加密数据(3DES) 可与java的DES(DESede/CBC/PKCS5Padding)加密方式兼容 <?php /** * Created by PhpStorm. * ...

  2. 分组密码(三)DES 算法— 密码学复习(六)

    在介绍完Feistel结构之后,接下来进入到著名的DES算法. 6.1 DES算法的意义 在正式介绍DES之前,首先介绍几个重要的历史时间节点. ① 1973年,美国国家标准局(NBS)向社会公开征集 ...

  3. DES算法详解

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

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

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

  5. 算法第四版 在Eclipse中调用Algs4库

    首先下载Eclipse,我选择的是Eclipse IDE for Java Developers64位版本,下载下来之后解压缩到喜欢的位置然后双击Eclipse.exe启动 然后开始新建项目,File ...

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

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

  7. 常见排序算法(JS版)

    常见排序算法(JS版)包括: 内置排序,冒泡排序,选择排序,插入排序,希尔排序,快速排序(递归 & 堆栈),归并排序,堆排序,以及分析每种排序算法的执行时间. index.html <! ...

  8. MFC 简单实现 DES 算法

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

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

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

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

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

随机推荐

  1. vue-router 动态导航 router-link :to属性

    经常碰到这类需求,从后台获取数据后再前程连接,参数id动态获取 <el-row v-for="item in Travels"> <el-col :span=&q ...

  2. 完整验证码类(validityHelper)(代码+使用)

    using System; using System.Web; using System.Drawing; using System.Security.Cryptography; namespace ...

  3. java split函数结尾空字符串被丢弃的问题

    参考: http://yinny.iteye.com/blog/1750210 http://www.xuebuyuan.com/1692988.html java中的split函数用于将字符串分割为 ...

  4. 多线程(Java)

    Thread 类 和 Runnable 接口 在Java中实现多线程有两种方法 继承 Thread 类 优点:通过覆盖Thread 类的方法,可以改变线程的行为. 实现 Runnable 接口 优点: ...

  5. 2.Servlet基础.md

    目录 1.定义 2.特点 3.手动编写一个Servlet 3.1创建一个Servlet类 3.2找到编写类的class文件,将这个类的整个包拷贝到一个web应用中的WEB-INF/classes目录下 ...

  6. Oracle ORA-00911: 无效字符

    SQL语句后多了个分号 “ ; ”.

  7. Jumpserver 文档

    http://docs.jumpserver.org/zh/docs/admin_guide.html

  8. 学JS的心路历程-函式(六)其余参数及预设参数

    今天我们要来介绍ES6新增的其余参数及预设参数! 其余参数rest parameter …numbers可以让我们表示不确定数量的参数,并将其视为一个数组: function getVal(…numb ...

  9. linux下卸载自带的JDK和安装想要的JDK

    卸载 1.卸载用 bin文件安装的JDK方法:      删除/usr/java目录下的所有东西 2.卸载系统自带的jdk版本方法: 查看自带的jdk: #rpm -qa | grep gcj 看到如 ...

  10. workerman channel组件集群推送

    <?phpuse Workerman\Worker; require_once '../../web/Workerman/Autoloader.php';require_once '../../ ...