📄 desdenew.cpp
字号:
// des.cpp : Defines the entry point for the console application.
//
#include "StdAfx.h"
/* DES Program Decryption*/
/*---------------------------------------------------------------------------------------------------------------*/
#include<stdio.h>
#include<stdlib.h>
/*---------------------------------------------------------------------------------------------------------------*/
char IP[] /* initial permutation P */
= { 58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7 };
char InvIP[] /* Inverse permutation F */
= { 40, 8, 48, 16, 56, 24, 64, 32,
39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30,
37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28,
35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25 };
char ex[] /* expansion operation matrix */
= { 32, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13,
12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21,
20, 21, 22, 23, 24, 25,
24, 25, 26, 27, 28, 29,
28, 29, 30, 31, 32, 1 };
char pc1[] /* permuted choice table (1) */
= { 57, 49, 41, 33, 25, 17, 9,
1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27,
19, 11, 3, 60, 52, 44, 36,
63, 55, 47, 39, 31, 23, 15,
7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29,
21, 13, 5, 28, 20, 12, 4 };
char pc2[] /* permuted choice table (2) */
= { 14, 17, 11, 24, 1, 5,
3, 28, 15, 6, 21, 10,
23, 19, 12, 4, 26, 8,
16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55,
30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53,
46, 42, 50, 36, 29, 32 };
char sbox[8][4][16] /* 48->32 bit compression tables (S Box)*/
= { { /* S[1] */
14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 },
{ /* S[2] */
15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 },
{ /* S[3] */
10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 },
{ /* S[4] */
7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 },
{ /* S[5] */
2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 },
{ /* S[6] */
12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 },
{ /* S[7] */
4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 },
{ /* S[8] */
13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 },
};
char p32[] /* 32-bit permutation function */
= { 16, 7, 20, 21,
29, 12, 28, 17,
1, 15, 23, 26,
5, 18, 31, 10,
2, 8, 24, 14,
32, 27, 3, 9,
19, 13, 30, 6,
22, 11, 4, 25 };
char recycle[] /* the number of left rotations */
= { 1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1 };
char KEY[64]={'0','0','0','1','0','0','1','1', '0','0','1','1','0','1','0','0', '0','1','0','1','0','1','1','1', '0','1','1','1','1','0','0','1', '1','0','0','1','1','0','1','1', '1','0','1','1','1','1','0','0', '1','1','0','1','1','1','1','1', '1','1','1','1','0','0','0','1'};
/* initial key 64bit */
/*---------------------------------------------------------------------------------------------------------------*/
void main()
{
char * fn_in, * fn_out;
fn_in=(char *)malloc(36);
fn_out=(char *)malloc(36);
printf("enter the filename that you want to decode.\n");
scanf("%s",fn_in);
printf("enter the filename of which you want to save the file decoded.\n");
scanf("%s",fn_out);
FILE * fp_in;
FILE * fp_out;
fp_in=fopen(fn_in,"rb"); //二进制格式打开文件
if(fp_in==NULL)
{
printf("This file %s can not open!\n",fn_in);
exit(1);
}
fp_out=fopen(fn_out,"wb");//二进制格式打开文件
if(fp_out==NULL)
{
printf("This file %s can not open!\n",fn_out);
exit(1);
}
char c[1]; // 来自文本的一个字符
char ch[8]; // 一个含8个字符(64 bit)的分组
char dech[8]; // 加密后的一个分组
char Ptxt[64]; // 明文
char Ctxt[64]; // 密文
char temptxt[64]; // 临时文本
void DES(char [], char []); // DES加密函数
void CharToBin(char [], char [],int); // 单个字符转换成8位2进制字符串
void BinToChar(char [], char []); // 8位2进制字符串转换成单个字符
void output(char [],FILE *,int); // 向文件输出加密结果
int t=0;
int d=0;
while(fread(c,1,1,fp_in)==1) // 读取8bit
{
t=t+1;
ch[t-1]=c[0];
if(t==8) // 成功读取64bit(一个分组)
{
if(d) // 判断上一次是否成功读取64bit(一个分组)
{
output(dech,fp_out,8); // 若成功则输出上一次分组的加密结果
}
CharToBin(ch,Ctxt,8); // 对当前分组进行加密开始
DES(Ctxt,Ptxt); // 使用DES加密函数
for(int i=0;i<64;i++)
{
temptxt[i]=Ptxt[i]; // 记录当前加密结果一备密文挪用
}
BinToChar(Ptxt,dech); // 完成对当前分组的加密
t=0; // 重新记录变量t
d=1; // 确定成功读取64bit(一个分组)并完成加密
}
}
if(t) // 判断是否进行密文挪用
{
char lastch[8];
for(int j=0;j<t;j++)
{
lastch[j]=dech[j];
}// 选择上一个分组的加密结果的头 8*t bit作为最后一组加密结果
char tailtxt[64];
for(j=t*8;j<64;j++)
{
tailtxt[j]=temptxt[j];
}
// 选择上一个分组的加密结果的后64-8*t bit
CharToBin(ch,Ctxt,t);
// 选择当前不完整的分组作为下一个待加密分组的前8*t bit
for(j=t*8;j<64;j++)
{
Ctxt[j]=tailtxt[j];
}
// tailtxt作为下一个待加密分组的后64-8*t bit
// 至此新的一个64bit分组产生
DES(Ctxt,Ptxt); // 使用DES加密函数
BinToChar(Ptxt,dech); // 完成加密
output(dech,fp_out,8);
// 输出当前分组的加密结果
output(lastch,fp_out,t);
// 输出上一个分组的加密结果的头 8*t bit
}
else // 确定不进行密文挪用
{
if(d)
{
output(dech,fp_out,8); // 输出当前分组的加密结果
}
fclose(fp_in);
fclose(fp_out); // 关闭文件
}
}
/*--------------------------------------------------------------------------------------------------------------*/
// 8个字符转换成长度为64bit的2进制字符串(按二进制补码)
void CharToBin(char in[], char out[],int n)
{
int j=0;
int s=0;
char * str;
str=(char *)malloc(32);
for(int i=0;i<n*8;i=i+8)
{
if(in[i/8]>=0)
{
out[i]='0';
if(in[i/8]>=64)
{
s=0;
}
if(in[i/8]<64&&in[i/8]>=32)
{
s=1;
}
if(in[i/8]<32&&in[i/8]>=16)
{
s=2;
}
if(in[i/8]<16&&in[i/8]>=8)
{
s=3;
}
if(in[i/8]<8&&in[i/8]>=4)
{
s=4;
}
if(in[i/8]<4&&in[i/8]>=2)
{
s=5;
}
if(in[i/8]<2&&in[i/8]>=0)
{
s=6;
}
str=itoa(in[i/8],str,2);
for(j=1;j<=s;j++)
{
out[i+j]='0';
}
for(j=s+1;j<8;j++)
{
out[i+j]=str[j-(s+1)];
}
}
if(in[i/8]<0)
{
str=itoa(in[i/8],str,2);
for(j=0;j<8;j++)
{
out[i+j]=str[24+j];
}
}
}
}
/*---------------------------------------------------------------------------------------------------------------*/
// 长度为64bit的2进制字符串转换成8个字符(按二进制补码)
void BinToChar(char in[], char out[])
{
char s=0;
for(int i=0;i<64;i=i+8)
{
if(in[i+0]=='1')
{
if(in[i+7]=='0')
{
s=s-1;
}
if(in[i+6]=='0')
{
s=s-2;
}
if(in[i+5]=='0')
{
s=s-4;
}
if(in[i+4]=='0')
{
s=s-8;
}
if(in[i+3]=='0')
{
s=s-16;
}
if(in[i+2]=='0')
{
s=s-32;
}
if(in[i+1]=='0')
{
s=s-64;
}
s=s-1;
out[i/8]=s;
}
if(in[i+0]=='0')
{
if(in[i+7]=='1')
{
s=s+1;
}
if(in[i+6]=='1')
{
s=s+2;
}
if(in[i+5]=='1')
{
s=s+4;
}
if(in[i+4]=='1')
{
s=s+8;
}
if(in[i+3]=='1')
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -