#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. Spring MVC 的springMVC.xml疑问解析

    <mvc:annotation-driven /> <mvc:annotation-driven /> 会自动注册DefaultAnnotationHandlerMapping ...

  2. AspxGridView服务器事件列表

    2.AutoFilterCellEditorCreate (1)语法:publicdelegate void ASPxGridViewEditorCreateEventHandler(object s ...

  3. 利用STM32CubeMX生成HID双向通讯工程

    使用开发板为正点原子ministm32 现在我们先使用HID descriptor Tool来生成我们需要的hid的 保存使用选择.H // D:\usb资料\HID\MSDEV\Projects\t ...

  4. CSS样式学习-1

    一.分类 ①内联,写在标签中,写法是style="样式属性".优先级最高. 优点:控制精确.缺点:代码重用性差,范围小. 例如: <div style="font- ...

  5. beyondCompare工具使用

    1.下载beyondcompare  (从官网下载) 2.载入.class文件比对 参见: beyond compare 对class文件反编译及比较  (https://blog.csdn.net/ ...

  6. Servlet基本_初期化パラメータ、外部ファイル

    1.サーブレットの初期化パラメータサーブレットの初期化パラメータを利用するには.必ずweb.xmlにおいてサーブレットマッピングを指定する必要がある.(Tomactのinvokerサーブレットは利用で ...

  7. YCSB性能测试工具使用(转)

    在网上查In-Memory NoSQL性能测试的资料时,偶然间发现了这个性能测试工具YCSB,全称为“Yahoo! Cloud Serving Benchmark”.它内置了对常见NoSQL数据库和数 ...

  8. 转:HTML5页面如何在手机端浏览器调用相机、相册功能

    HTML5页面如何在手机端浏览器调用相机.相册功能 开发微信端浏览器访问的HTML5的页面,页面中有一个<input id="input" type="file&q ...

  9. vue启动时报错,node-modules下xxx缺失

    从qq上拷贝了一个项目,解压后打开进vscode,安装依赖与scss后启动,显示node-modules下xxx指向缺失(想不起来是哪个缺失了),在网上找了很多解决办法,包括重新安装node 与 np ...

  10. EF 1

    安装框架: 在NuGet中安装ef框架,命令:Install-package EntityFramework 数据迁移: 在程序包管理器控制台,执行语句. 初始化: 1.Enable-Migratio ...