Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 616
1
#include "des56.h"
2
#include <stdlib.h>
3
#include <stdio.h>
4
#include <map>
5
#include <vector>
6
#include <string.h>
7
8
//Use these constants to specify ENCRYPT or DECRYPT in the fencrypt function call. (The middle parameter)
9
#define ENCRYPT 0
10
#define DECRYPT 1
11
12
using namespace std;
13
14
void DES_21_bit_key(const char pt[8],char ct[8],int decrypt, char key[3])
15
{
16
keysched ks;
17
char big_key[8] = {0};
18
memcpy(big_key,key,3);
19
fsetkey(big_key,&ks);
20
if (ct != pt)
21
memcpy(ct,pt,8);
22
fencrypt(ct,decrypt,&ks);
23
}
24
25
void double_DES_21(char block[8],int decrypt, char key1[3], char key2[3])
26
{
27
if(decrypt) {
28
DES_21_bit_key(block,block,decrypt,key2);
29
DES_21_bit_key(block,block,decrypt,key1);
30
} else {
31
DES_21_bit_key(block,block,decrypt,key1);
32
DES_21_bit_key(block,block,decrypt,key2);
33
}
34
}
35
36
void next_key(char key1[])
37
{
38
key1[2] += 2;
39
if(key1[2] == 0) // rollover
40
{
41
key1[1] += 2;
42
if(key1[1]==0) // rollover
43
{
44
key1[0] += 2;
45
}
46
}
47
}
48
49
int main()
50
{
51
char x[8] = {'a','b','c','d','e','f','g','h'}; ///x value of known plaintext pair
52
char y[8] = {0x03,0x8d ,0x8a,0xdf ,0x90, 0xe7 ,0x0b,0xbe}; //y value of known plaintext pair
53
char z[8] = {0, 1, 2, 3, 4, 5, 6, 7};
54
55
char key1[3] = {0};
56
char key2[3] = {0};
57
58
int i = 0;
59
60
map<vector<char>,vector<char> > M;
61
62
for(; i < (1 << 21); i++)
63
{
64
DES_21_bit_key(x, z, 0, key1);
65
vector<char> K(key1,key1+3);
66
vector<char> Z(z,z+8);
67
M[Z] = K;
68
next_key(key1);
69
// printf("%hhx %hhx %hhx \n", key1[0], key1[1], key1[2]);
70
}
71
bzero(key1, 3);
72
73
i = 0;
74
for(; i < (1 << 21); i++)
75
{
76
DES_21_bit_key(y, z, 1, key1);
77
vector<char> K(key1,key1+3);
78
vector<char> Z(z,z+8);
79
80
map<vector<char>,vector<char> >::iterator L = M.find(Z);
81
if (L != M.end())
82
{
83
for(int i = 0; i < 3; i++)
84
{
85
key2[i] = M[Z][i];
86
// printf("%hhx", key1[i]);
87
}
88
break;
89
}
90
else
91
next_key(key1);
92
}
93
94
// printf("%hhx %hhx %hhx \n", key1[0], key1[1], key1[2]);
95
// printf("%hhx %hhx %hhx \n", key2[0], key2[1], key2[2]);
96
97
char block1[8] = {0x1a, 0xe8, 0xba, 0x6f, 0xb4, 0x0f, 0xf2, 0xea};
98
char block2[8] = {0xc9, 0xfc, 0xdf, 0x8c, 0xc4, 0x79, 0x0b, 0xcc};
99
char block3[8] = {0xc4, 0x8e, 0x20, 0xb6, 0xc0, 0xb5, 0x15, 0x8e};
100
101
double_DES_21(block1, 1, key2, key1);
102
double_DES_21(block2, 1, key2, key1);
103
double_DES_21(block3, 1, key2, key1);
104
105
for(int i = 0; i < 8; i++)
106
printf("%c", block1[i]);
107
108
for(int i = 0; i < 8; i++)
109
printf("%c", block2[i]);
110
111
for(int i = 0; i < 8; i++)
112
printf("%c", block3[i]);
113
114
double_DES_21(block1, 0, key2, key1);
115
double_DES_21(block2, 0, key2, key1);
116
double_DES_21(block3, 0, key2, key1);
117
118
for(int i = 0; i < 8; i++)
119
printf("%hhx", block1[i]);
120
121
for(int i = 0; i < 8; i++)
122
printf("%hhx", block2[i]);
123
124
for(int i = 0; i < 8; i++)
125
printf("%hhx", block3[i]);
126
127
return 0;
128
}
129
130
131