-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
112 lines (100 loc) · 3.18 KB
/
main.cpp
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
#include "CBC_enc.hpp"
#define MESSAGELENGTH 100
int main(int argc, char **argv)
{
unsigned char *iv=(unsigned char*)malloc(sizeof(unsigned char)
* AES_BLOCK_SIZE);
srand(time(NULL));
init(iv);
unsigned char *message1=(unsigned char*)malloc(sizeof(unsigned char)
* MESSAGELENGTH);
unsigned char *message2=(unsigned char*)malloc(sizeof(unsigned char)
* MESSAGELENGTH);
memset(message1, 0, MESSAGELENGTH);
memset(message2, 255, MESSAGELENGTH);
/*
printf("Message 1: ");
hex_print(message1,MESSAGELENGTH);
printf("Message 2: ");
hex_print(message2,MESSAGELENGTH);
*/
const size_t encslength =
((MESSAGELENGTH + AES_BLOCK_SIZE)
/ AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
unsigned char *encryptedMessage_first =
(unsigned char*)malloc(sizeof(unsigned char)
* encslength);
unsigned char *encryptedMessage_second =
(unsigned char*)malloc(sizeof(unsigned char)
* encslength);
int oracle=rand()%2;
if(oracle==0)
{
encryptedMessage_first=encMessage(message1,MESSAGELENGTH,iv);
}
else
{
encryptedMessage_first=encMessage(message2,MESSAGELENGTH,iv);
}
for(int j=0;j<AES_BLOCK_SIZE;j++)
{
message1[j]=message1[j]^iv[j];
}
if(oracle==0)
{
encryptedMessage_second=encMessage(message1,MESSAGELENGTH,iv);
}
else
{
encryptedMessage_second=encMessage(message2,MESSAGELENGTH,iv);
}
int result=strcmp((char*)encryptedMessage_first,(char*)encryptedMessage_second);
if(result==0)
printf("First message was encrypted\n");
else
printf("Second message was encrypted\n");
printf("Oracle choise: %d\n", oracle);
//hex_print(encryptedMessage, MESSAGELENGTH);
/*
unsigned char *aes_key =
(unsigned char*)malloc(sizeof(unsigned char)
* (KEYLENGTH/8));
memset(aes_key, 0, KEYLENGTH/8);
if (!RAND_bytes(aes_key, KEYLENGTH/8))
exit(-1);
size_t inputslength = 92;
unsigned char *aes_input =
(unsigned char*)malloc(sizeof(unsigned char)
* inputslength);
memset(aes_input, 'X', inputslength);
// buffers for encryption and decryption
const size_t encslength =
((inputslength + AES_BLOCK_SIZE)
/ AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
unsigned char *enc_out =
(unsigned char*)malloc(sizeof(unsigned char)
* encslength);
unsigned char *dec_out =
(unsigned char*)malloc(sizeof(unsigned char)
* inputslength);
memset(enc_out, 0, sizeof(enc_out));
memset(dec_out, 0, sizeof(dec_out));
encMessage(aes_input,inputslength);
// so i can do with this aes-cbc-128 aes-cbc-192 aes-cbc-256
AES_KEY enc_key, dec_key;
AES_set_encrypt_key(aes_key, KEYLENGTH, &enc_key);
AES_cbc_encrypt(aes_input, enc_out, inputslength,
&enc_key, iv, AES_ENCRYPT);
AES_set_decrypt_key(aes_key, KEYLENGTH, &dec_key);
AES_cbc_encrypt(enc_out, dec_out, encslength,
&dec_key, iv_dec, AES_DECRYPT);
printf("original:\t");
hex_print(aes_input, inputslength);
printf("encrypt:\t");
hex_print(enc_out, encslength);
printf("decrypt:\t");
hex_print(dec_out, inputslength);
// free memory here
*/
return 0;
}