#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. java reflect反射获取方法变量参数

    类的成员包含变量(Field),方法(Method),构造器(Constructor) 类定义 package Reflect; public class MyTest { public int a; ...

  2. 关于visual studio code在win10系统上安装后会报扩展宿主意外终止的解决方法

    我的电脑的地址 C:\Users\Administrator.SC-201810160958\AppData\Local\Programs\Microsoft VS Code\resources\ap ...

  3. nginx 虚拟主机、反向代理服务器及负载均衡,多台主机分离php-fpm实验

    一.简介 本章介绍一些架构原理基础知识, 1.1.LNMP及php-fpm 请参考https://www.cnblogs.com/zhangxingeng/p/10242902.html 1.2.透明 ...

  4. Django models模型(1)

    1)使用模型需要在INSTALLED_APPS中注册 2)模型字段(字段类型和字段选项) 1.字段类型 常用: CharField,TextField: 对应字符串对象 DateTimeField和D ...

  5. delphi RTTI 三 属性读取与赋值

    delphi RTTI 读取属性 运行期 通过RTTI给属性赋值 http://docwiki.embarcadero.com/CodeExamples/Berlin/en/Event_RTTI_In ...

  6. HTML学习-2标记标签-1

    大致可以分为以下6类学习: 1.通用标签. 2.常用标签. 3.表格标签. 4.表单元素. 5.框架. 6.其他. 一.通用标签.及属性 1.<body></body>标签,主 ...

  7. 机器学习进阶-图像基本操作-数值计算 1.cv2.add(将图片进行加和) 2.cv2.resize(图片的维度变换) 3.cv2.addWeighted(将图片按照公式进行重叠操作)

    1.cv2.add(dog_img, cat_img)  # 进行图片的加和 参数说明: cv2.add将两个图片进行加和,大于255的使用255计数 2.cv2.resize(img, (500, ...

  8. mui集成百度ECharts的统计图表以及清空释放图表

    echarts官网地址: http://echarts.baidu.com/index.html 更换主题颜色: // 图表清空------------------- mychart.clear(); ...

  9. android 环境变量

    1.新建一个环境变量和系统变量名称为ANDROID_HOME,变量值为当前安装SDK的目录 2.把%ANDROID_HOME%\platform-tools;%ANDROID_HOME%\tools添 ...

  10. Netty - 2

    参考:Scalable IO in Java - http://gee.cs.oswego.edu/dl/cpjslides/nio.pdf mainReactor负责处理客户端的连接请求,并将acc ...