-
Notifications
You must be signed in to change notification settings - Fork 1
/
cryptoCPP.h
190 lines (144 loc) · 4.77 KB
/
cryptoCPP.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#ifndef _INCLUDE_CRYPTOCPP_HEADER_
#define _INCLUDE_CRYPTOCPP_HEADER_
#include <sstream>
#include <vector>
#define ll unsigned long long int
enum PAD_TYPE { NORM_0 =0, PKCS5=1 , PAD_TYPE_MAX };
enum STRING_TYPE { HEX_0 = 0, ASCII_1 , STRING_TYPE_MAX};
enum ENCRYPTION_MODE { ECB_0 = 0, CBC_1 , ENCRYPTION_MODE_MAX};
class BYTE
{
public:
uint8_t byte;
BYTE();
BYTE(int);
BYTE(unsigned int t);
BYTE(uint8_t);
BYTE operator^(const BYTE &);
friend BYTE operator*(const int ,const BYTE &);
friend BYTE operator*(const BYTE &,const int );
BYTE operator *(const BYTE &);
BYTE operator << (std::size_t);
BYTE operator << (int );
BYTE operator >> (int );
BYTE operator >> (std::size_t);
BYTE operator & (const BYTE &);
BYTE operator = (const BYTE &);
operator int();
};
class crypto_AES
{
private:
std::string input;
STRING_TYPE m_type;
std::string output;
std::vector<std::vector<BYTE> > secretKey;
//a utility functions that performs substituition on
//one word
void __subBytes_transform_word__(std::vector<BYTE>&, bool = false);
//Sub-Bytes transformation on the state
void __subBytes_transform__(std::vector< std::vector<BYTE> >&, bool = false);
//a utility function for XORing two words
std::vector<BYTE> __xor_word__(std::vector<BYTE>&,
std::vector<BYTE>&);
//a utility function to shift a row
void __shift_row_left__(std::vector<BYTE> &);
//a utility function to shift a row right
void __shift_row_right__(std::vector<BYTE> &);
//Inverse Shift Rows transformation on the state
void __shiftRows_inv_transform__(std::vector<std::vector<BYTE> > &);
//Shift Rows transormation on the state
void __shiftRows_transform__(std::vector<std::vector<BYTE> >&);
//Mix Columns transformation on the state
void __mixColumns_transform__(std::vector<std::vector<BYTE> >&);
//Inverse Mix Columns transformation on the state
void __mixColumns_inv_transform__(std::vector<std::vector<BYTE> >&);
//Generates keys for all rounds and returns them
std::vector<std::vector<BYTE> > __roundKeyGen__(std::vector<BYTE>&);
//Add Round Key transformation on the state
void __addRoundKey_transform__(std::vector< std::vector<BYTE> >&,
std::vector< std::vector<BYTE> >&,
int);
//Encrypts 4Bytes of data with the key stored in words
std::vector<BYTE> __enc_block__(std::vector<BYTE>&,
std::vector<std::vector<BYTE> >&);
//Decrypts 4Bytes of data with the key stored in words
std::vector<BYTE> __dec_block__(std::vector<BYTE>&,
std::vector<std::vector<BYTE> >&);
//utility function to convert a string to a vector of bytes
std::vector<BYTE> __BYTE_transform__(std::string,
STRING_TYPE);
//utility function to convert vector of bytes to hex string
std::string __hex_transform__(std::vector<BYTE> &);
//utility function to convert vector of bytes to ascii string
std::string __ascii_transform__(std::vector<BYTE> &);
//function used for padding
void __pad_message__(PAD_TYPE pad_type);
//function to get the next block of message
std::vector<BYTE> __getNextBlock__();
public:
crypto_AES();
//Encrypt function
std::string encrypt(std::string,
STRING_TYPE,
std::string,
STRING_TYPE,
ENCRYPTION_MODE,
std::string,
STRING_TYPE,
PAD_TYPE);
//Decrypt Function
std::string decrypt(std::string,
STRING_TYPE,
std::string,
STRING_TYPE,
ENCRYPTION_MODE,
PAD_TYPE);
};
class crypto_DES
{
private:
std::string input;
STRING_TYPE m_type;
std::string output;
ll secretKey;
//round Function;
ll __roundFunction__(ll,ll);
//initial and final permutation
ll __permut__(ll,bool);
//utility function for circular left shift
ll __cirLeftShift__(ll);
//utility function for circular right shift
ll __cirRightShift__(ll);
//function for generating key for next round
ll __roundKeyGen_next__(ll &,int &);
//function for generating key for previous round
ll __roundKeyGen_prev__(ll &, int &);
//utitility function for parity drop
ll __parity_drop__(ll);
//function for encrypting one block of data
ll __enc_block__(ll,ll);
//function for decrypting one block of data
ll __dec_block__(ll,ll);
//get next message block
ll __getNextBlock__();
//pad the input message
void __pad_message__();
//utitlity function for converting string to decimal
ll __convert2Dec__(std::string, STRING_TYPE);
public:
crypto_DES();
std::string decrypt(std::string,
STRING_TYPE,
std::string,
STRING_TYPE,
ENCRYPTION_MODE);
std::string encrypt(std::string,
STRING_TYPE,
std::string,
STRING_TYPE,
ENCRYPTION_MODE,
std::string iv="",
STRING_TYPE iv_type = STRING_TYPE_MAX);
};
#endif