# contains "memory.h"
# contains "time.h"
# contains "stdlib.h"
# include & ltiostream & gt
Use namespace std
# define PLAIN _ FILE _ OPEN _ ERROR- 1
#define KEY_FILE_OPEN_ERROR -2
# Error in opening the definition password file -3
# Define OK 1
typedef long ElemType
/* Initial substitution table IP*/
Long IP _ table [64] = {57,49,4 1, 33,25,17,9,1,
59,5 1,43,35,27, 19, 1 1,3,
6 1,53,45,37,29,2 1, 13,5,
63,55,47,39,3 1,23, 15,7,
56,48,40,32,24, 16,8,0,
58,50,42,34,26, 18, 10,2,
60,52,44,36,28,20, 12,4,
62,54,46,38,30,22, 14,6};
/* Inverse initial substitution table IP- 1 */
Long IP_ 1_Table[64] = {39, 7, 47, 15, 55, 23, 63, 3 1,
38,6,46, 14,54,22,62,30,
37,5,45, 13,53,2 1,6 1,29,
36,4,44, 12,52,20,60,28,
35,3,43, 1 1,5 1, 19,59,27,
34,2,42, 10,50, 18,58,26,
33, 1,4 1,9,49, 17,57,25,
32,0,40,8,48, 16,56,24};
/* Extended substitution table E*/
E Long _ table [48] = {3 1, 0, 1, 2, 3, 4,
3, 4, 5, 6, 7, 8,
7, 8,9, 10, 1 1, 12,
1 1, 12, 13, 14, 15, 16,
15, 16, 17, 18, 19,20,
19,20,2 1,22,23,24,
23,24,25,26,27,28,
27,28,29,30,3 1, 0};
/* permutation function P*/
long P_Table[32] = { 15,6, 19,20,28, 1 1,27, 16,
0, 14,22,25,4, 17,30,9,
1,7,23, 13,3 1,26,2,8,
18, 12,29,5,2 1, 10,3,24};
/*S box */
Dragon S[8][4][ 16] =
/*S 1*/
{{{ 14,4, 13, 1,2, 15, 1 1,8,3, 10,6, 12,5,9,0,7},
{0, 15,7,4, 14,2, 13, 1, 10,6, 12, 1 1,9,5,3,8},
{4, 1, 14,8, 13,6,2, 1 1, 15, 12,9,7,3, 10,5,0},
{ 15, 12,8,2,4,9, 1,7,5, 1 1,3, 14, 10,0,6, 13}},
/*S2*/
{{ 15, 1,8, 14,6, 1 1,3,4,9,7,2, 13, 12,0,5, 10},
{3, 13,4,7, 15,2,8, 14, 12,0, 1, 10,6,9, 1 1,5},
{0, 14,7, 1 1, 10,4, 13, 1,5,8, 12,6,9,3,2, 15},
{ 13,8, 10, 1,3, 15,4,2, 1 1,6,7, 12,0,5, 14,9}},
/*S3*/
{{ 10,0,9, 14,6,3, 15,5, 1, 13, 12,7, 1 1,4,2,8},
{ 13,7,0,9,3,4,6, 10,2,8,5, 14, 12, 1 1, 15, 1},
{ 13,6,4,9,8, 15,3,0, 1 1, 1,2, 12,5, 10, 14,7},
{ 1, 10, 13,0,6,9,8,7,4, 15, 14,3, 1 1,5,2, 12}},
/*S4*/
{{7, 13, 14,3,0,6,9, 10, 1,2,8,5, 1 1, 12,4, 15},
{ 13,8, 1 1,5,6, 15,0,3,4,7,2, 12, 1, 10, 14,9},
{ 10,6,9,0, 12, 1 1,7, 13, 15, 1,3, 14,5,2,8,4},
{3, 15,0,6, 10, 1, 13,8,9,4,5, 1 1, 12,7,2, 14}},
/*S5*/
{{2, 12,4, 1,7, 10, 1 1,6,8,5,3, 15, 13,0, 14,9},
{ 14, 1 1,2, 12,4,7, 13, 1,5,0, 15, 10,3,9,8,6},
{4,2, 1, 1 1, 10, 13,7,8, 15,9, 12,5,6,3,0, 14},
{ 1 1,8, 12,7, 1, 14,2, 13,6, 15,0,9, 10,4,5,3}},
/*S6*/
{{ 12, 1, 10, 15,9,2,6,8,0, 13,3,4, 14,7,5, 1 1},
{ 10, 15,4,2,7, 12,9,5,6, 1, 13, 14,0, 1 1,3,8},
{9, 14, 15,5,2,8, 12,3,7,0,4, 10, 1, 13, 1 1,6},
{4,3,2, 12,9,5, 15, 10, 1 1, 14, 1,7,6,0,8, 13}},
/*S7*/
{{4, 1 1,2, 14, 15,0,8, 13,3, 12,9,7,5, 10,6, 1},
{ 13,0, 1 1,7,4,9, 1, 10, 14,3,5, 12,2, 15,8,6},
{ 1,4, 1 1, 13, 12,3,7, 14, 10, 15,6,8,0,5,9,2},
{6, 1 1, 13,8, 1,4, 10,7,9,5,0, 15, 14,2,3, 12}},
/*S8*/
{{ 13,2,8,4,6, 15, 1 1, 1, 10,9,3, 14,5,0, 12,7},
{ 1, 15, 13,8, 10,3,7,4, 12,5,6, 1 1,0, 14,9,2},
{7, 1 1,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, 1 1}}};
/* alternative selection 1*/
long PC_ 1[56] = {56,48,40,32,24, 16,8,
0,57,49,4 1,33,25, 17,
9, 1,58,50,42,34,26,
18, 10,2,59,5 1,43,35,
62,54,46,38,30,22, 14,
6,6 1,53,45,37,29,2 1,
13,5,60,52,44,36,28,
20, 12,4,27, 19, 1 1,3};
/* Replace option 2*/
long PC_2[48] = { 13, 16, 10,23,0,4,2,27,
14,5,20,9,22, 18, 1 1,3,
25,7, 15,6,26, 19, 12, 1,
40,5 1,30,36,46,54,29,39,
50,44,32,46,43,48,38,55,
33,52,45,4 1,49,35,28,3 1};
/* Provisions on the number of left shifts */
long MOVE _ TIMES[ 16]= { 1, 1,2,2,2,2,2,2, 1,2,2,2,2,2, 1 };
/* key arrangement 1*/
long DES _ PC 1 _ Transform(elem type key[64],ElemType tempbts[56]){
Long cnt
for(CNT = 0; cnt & lt56; cnt++){
tempbts[CNT]= key[PC _ 1[CNT]];
}
Returns 0;
}
Long des _ rol (elementtype data [56], long time) {
elem type temp[56];
/* Save the bits to be cyclically moved to the right */
Memcpy (temperature, data, time);
memcpy(temp+time,data+28,time);
/* The first 28 shifts */
Memcpy (data, data+time, 28 times);
memcpy(data+28-time,temp,time);
/* Move the last 28 bits */
Memcpy (data +28, data +28+ time, 28- time);
memcpy(data+56-time,temp+time,time);
Returns 0;
}
long DES _ PC2 _ Transform(elem type key[56],ElemType tempbts[48]){
Long cnt
for(CNT = 0; cnt & lt48; cnt++){
tempbts[CNT]= key[PC _ 2[CNT]];
}
Returns 0;
}
/* Generate subitems */
long DES _ make subKeys(elem type key[64],elem type subKeys[ 16][48]){
elem type temp[56];
elem type temp 1[48];
Long cnt
DES_PC 1_Transform(key,temp); /*PC 1 replace */
for(CNT = 0; cnt & lt 16; Cnt++){/* 16 rounds of substitution, resulting in 16 subkeys */
Des _ROL(temp, move _ times [0]);
DES_PC2_Transform(temp,temp 1);
for(long I = 0; I & lt48; i++)
Subkey [CNT] [I] = temp1[I];
}
Returns 0;
}
/* Extended permutation */
long DES _ E _ Transform(elem type data[48]){
Long cnt
elem type temp[48];
for(CNT = 0; cnt & lt48; cnt++){
temp[CNT]= data[E _ Table[CNT]];
}
Memcpy (data, temperature, 48);
Returns 0;
}
/*IP replacement */
long DES _ IP _ Transform(elem type data[64]){
Long cnt
elem type temp[64];
for(CNT = 0; cnt & lt64; cnt++){
temp[CNT]= data[IP _ Table[CNT]];
}
Memcpy (data, temperature, 64);
Returns 0;
}
/*IP inverse permutation */
long DES _ IP _ 1 _ Transform(elem type data[64]){
Long cnt
elem type temp[64];
for(CNT = 0; cnt & lt64; cnt++){
temp[CNT]= data[IP _ 1 _ Table[CNT]];
}
Memcpy (data, temperature, 64);
Returns 0;
}
long DES _ P _ Transform(elem type data[32]){
Long cnt
elem type temp[32];
for(CNT = 0; cnt & lt32; cnt++){
temp[CNT]= data[P _ Table[CNT]];
}
Memcpy (data, temperature, 32);
Returns 0;
}
/* XOR */
long DES_XOR(ElemType R[48],ElemType L[48],long count){
Long cnt
for(CNT = 0; Cnt< count; cnt++){
^=·l[CNT];
}
Returns 0;
}
/*S box replacement */
long DES _ SBOX(elem type data[48]){
Long cnt
Long line, line, output;
long cur 1,cur2
for(CNT = 0; cnt & lt8; cnt++){
cur 1 = CNT * 6;
cur2 = cnt & lt& lt2;
/* Calculate rows and columns in the S box */
line =(data[cur 1]& lt; & lt 1)+data[cur 1+5];
row =(data[cur 1+ 1]& lt; & lt3)+(data[cur 1+2]& lt; & lt2)
+(data[cur 1+3]& lt; & lt 1)+data[cur 1+4];
output = S[CNT][line][row];
/* to binary */
Data[cur2] = (output & amp0X08)>& gt3;
data[cur 2+ 1]=(output & amp; 0X04)>& gt2;
Data[cur2+2] = (output & amp0X02)>& gt 1;
Data [cur2+3] = output sum. 0x 0 1;
}
Returns 0;
}
/* Exchange */
long DES_Swap(ElemType left[32],ElemType right[32]){
elem type temp[32];
Memcpy (temperature, left, 32);
Memcpy (left, right, 32);
Memcpy (right, temperature, 32);
Returns 0;
}
/* Encrypt a single packet */
long DES _ encrypt block(elem type plain bits[64],ElemType subKeys[ 16][48],ElemType cipherBlock[64]){
ElemType copyright [48];
Long cnt
Long temperature [48];
/* Initial replacement (IP replacement) */
DES _ IP _ Transform(plain bits);
/* 16 iterations */
for(CNT = 0; cnt & lt 16; cnt++){
Memcpy (all rights reserved, plain bits+32,32);
/* Extend the right half from 32 bits to 48 bits */
DES_E_Transform (all rights reserved);
/* XOR the right half with the subkey */
DES_XOR (all rights reserved, temp, 48);
for(long I = 0; I & lt48; i++)
Child [CNT] [I] = temp [I];
/* XOR result enters S box and outputs 32-bit result */
DES_SBOX (all rights reserved);
/*P permutation */
DES_P_Transform (all rights reserved);
/* XOR the left and right parts of plaintext */
DES_XOR(plainBits, copyright, 32);
If (cnt! = 15){
/* Finally complete the exchange of left and right parts */
DES_Swap(plainBits,plain bits+32);
}
}
/* Inverse initial permutation (IP 1 permutation) */
DES _ IP _ 1 _ Transform(plain bits);
//Bit64ToChar8(plainBits,cipher block);
Memcpy (cipher block, plainBits, 64);
Returns 0;
}
Dragon DES_jiami (dragon a[64], dragon bkey[64])
{
Long copyright [48];
Long subkey [16] [48];
Long temperature [48];
DES_MakeSubKeys(bkey,subkey);
DES _ IP _ Transform(a);
for(long CNT = 0; cnt & lt 16; cnt++){
For (length k = 0;; k & lt32; k++)
Copyright [k] = a [32+k];
Copyright [32] = a [32];
Copyright [33] = a [35];
Copyright [34] = a [36];
Copyright [35] = a [39];
Copyright [36] = a [40];
Copyright [37] = a [43];
Copyright [38] = a [44];
Copyright [39] = a [47];
Copyright [40] = a [48];
Copyright [41] = a [52];
Copyright [42] = a [53];
Copyright [43] = a [56];
Copyright [44] = a [57];
Copyright [45] = a [61];
Copyright [46] = a [62];
Copyright [47] = a [63];
DES_E_Transform (all rights reserved);
DES_XOR (all rights reserved, subkey [cnt], 48);
DES_SBOX (all rights reserved);
DES_P_Transform (all rights reserved);
DES_XOR(a, copyright, 32);
If (cnt! = 15){
DES_Swap(a,a+32);
}
}
DES _ IP _ 1 _ Transform(a);
Returns 0;
}
Long trunk line ()
{
Long bkey[64]={
1,0, 1, 1,0, 1, 1,0,
1,0,0, 1,0,0, 1,0,
0, 1, 1,0, 1,0, 1,0,
0, 1,0, 1,0,0, 1, 1,
0, 1,0, 1,0, 1, 1,0,
1,0, 1,0, 1,0,0, 1,
0, 1, 1,0, 1, 1,0, 1,
1,0, 1, 1, 1, 1,0, 1
};
Longyi [64]={
1,0, 1, 1,0, 1, 1,0,
0,0, 1,0, 1,0, 1,0,
0, 1, 1,0, 1,0, 1,0,
0, 1,0, 1,0,0, 1, 1,
1,0, 1, 1, 1,0, 1, 1,
1,0, 1,0, 1,0,0, 1,
0,0, 1,0, 1,0,0, 1,
1,0, 1, 1,0,0, 1,0
};
long b[64]={
0, 1,0,0, 1,0, 1,0,
1,0, 1,0,0, 1,0, 1,
0, 1,0, 1,0, 1, 1,0,
1,0, 1,0, 1, 1,0, 1,
0, 1,0, 1,0,0, 1, 1,
1, 1, 1, 1,0,0, 1,0,
0, 1,0, 1,0, 1, 1,0,
0, 1,0, 1,0, 1,0, 1
};
Dragon c[8], d [8];
DES_jiami(a,bkey);
DES_jiami(b,bkey);
c[0]= 1;
c[7]= 1;
d[0]= 1;
d[7]= 1;
For (length x = 0;; x & lt6; x++)
c[ 1+x]= a[58+x];
//for(long j = 0; j & lt8; j++){
//cout & lt; & ltc[j];
// }
for(x = 0; x & lt6; x++)
d[ 1+x]= b[58+x];
//cout & lt; & ltendl
Long t, p;
//t = c[7]+c[6]* 10+c[5]* 1000+c[4]* 10000+c[3]* 10000+c[2]* 100000+c[ 1]* 100000+c[0]* 10438+000
t = c[7]+c[6]* 2+c[5]* 4+c[4]* 8+c[3]* 16+c[2]* 32+c[ 1]* 64+c[0]* 128;
p = d[7]+d[6]* 2+d[5]* 4+d[4]* 8+d[3]* 16+d[2]* 32+d[ 1]* 64+d[0]* 128;
cout & lt& ltt & lt& ltendl
cout & lt& ltp & lt& ltendl
Returns 0;
}
I'm sure it will work ~ ~ ~