#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. 1.深入分析_NIO性能分析

    首先还是看一个用3中方式copy文件的测试Demo 分别是:普通Stream文件copy,BuffferedStream进行Copy 和Channel(nio)进行文件Copy的代码和性能测试报告: ...

  2. <基础> PHP 进阶之 函数(Function)

    引用参数 $name = "eko"; function chang_name(&$name){ $name .= '_after_change'; } chang_nam ...

  3. node.js 爬虫案例

    本案例是爬的一部小说,爬取并存在本地 使用了动态浏览器头部信息,和 动态ip代理, 这2个方式可以有效的伪装自己,不至于被反爬,然后拉黑,之前已有记录,浏览器头部信息,也记录的有, app.js im ...

  4. SRM-相关资料路径

    SRM采购管理平台功能介绍 https://wenku.baidu.com/view/b05cff5930b765ce0508763231126edb6f1a763c.html https://wen ...

  5. sendfile函数--零拷贝(转)

    零拷贝:零拷贝技术可以减少数据拷贝和共享总线操作的次数,消除通信数据在存储器之间不必要的中间拷贝过程,有效地提高通信效率,是设计高速接口通道.实现高速服务器和路由器的关键技术之一. sendfile ...

  6. Jquery select chosen 插件注意点

    <select style="width:200px;" name="carId" data-placeholder="选择车辆牌照" ...

  7. 关于openwrt使用web升级提示固件版本不对的处理方法

    参考资料:https://blog.csdn.net/caoshunxin01/article/details/79355602 当openwrt使用web升级提示固件版本不对: The upload ...

  8. Hibernate学习笔记3.2(Hibernate组建映射)

    1.组建映射 可以存在一个表里面 Husband.java package com.bjsxt.hibernate; import javax.persistence.Embedded; import ...

  9. (转)关闭win10的Skype

    https://blog.csdn.net/qq_38285661/article/details/86663849 使用win10的小伙伴们,有没有发现一个不用的功能Skype,假如你想卸载又怕卸不 ...

  10. quartz 实例

    第一步:添加jar包 第二步:在spring配置文件中添加 <context:annotation-config/> 第三步:编写定时代码 我们通常做Java后台接口,是让前端访问的,让前 ...