· 6 years ago · Oct 12, 2019, 08:00 PM
1#include <stdio.h>
2#include <stdlib.h>
3#include <openssl/conf.h>
4#include <openssl/evp.h>
5#include <openssl/err.h>
6#include <string.h>
7
8unsigned char* key = (unsigned char *)"6D656469616E20202020202020202020"; //cuvantul "median" in baza 16
9unsigned char* iv = (unsigned char *)"123456789abcdef"; //vector de initializare
10
11void handleErrors(void)
12{
13 ERR_print_errors_fp(stderr);
14 abort();
15}
16
17int ecnrypt(unsigned int type, unsigned char *plaintext, unsigned char *ciphertext, unsigned char *key, unsigned char *iv) {
18 int ciphertext_length, lengthh;
19 int plaintext_length = strlen((const char*) plaintext);
20 EVP_CIPHER_CTX *context;
21
22 //creez contextul
23 if(!(context = EVP_CIPHER_CTX_new()))
24 handleErrors();
25
26 //initializez operatiunea de criptare in functie de modul preferat
27 if(type == 0){ //ecb type
28 if(1 != EVP_EncryptInit_ex(context, EVP_aes_256_ecb(), NULL, key, NULL)){
29 handleErrors();
30 }
31 } else {
32 if(1 != EVP_EncryptInit_ex(context, EVP_aes_256_ofb(), NULL, key, iv)){
33 handleErrors();
34 }
35 }
36
37 //se cripteaza mesajul(plaintext) si se obtine output-ul (ciphertext)
38 if(1 != EVP_EncryptUpdate(context, ciphertext, &lengthh, plaintext, plaintext_length))
39 handleErrors();
40 ciphertext_length = lengthh;
41
42 //se finalizeaza procesul de criptare
43 if(1 != EVP_EncryptFinal_ex(context, ciphertext + lengthh, &lengthh))
44 handleErrors();
45 ciphertext_length += lengthh;
46
47 // se elibereaza contextul din memorie
48 EVP_CIPHER_CTX_free(context);
49
50 return ciphertext_length ;
51}
52
53int decrypt(unsigned int type, unsigned char *plaintext, unsigned char *ciphertext, unsigned char *key, unsigned char *iv) {
54 int ciphertext_length = strlen((const char*) ciphertext);
55 int lengthh;
56 int plaintext_length;
57 EVP_CIPHER_CTX *context;
58
59 //creez contextul
60 if(!(context = EVP_CIPHER_CTX_new()))
61 handleErrors();
62
63 //initializez operatiunea de decriptare in functie de modul preferat
64 if(type == 0){ //ecb type
65 if(1 != EVP_DecryptInit_ex(context, EVP_aes_256_ecb(), NULL, key, NULL)){
66 handleErrors();
67 }
68 } else {
69 if(1 != EVP_DecryptInit_ex(context, EVP_aes_256_ofb(), NULL, key, iv)){
70 handleErrors();
71 }
72 }
73
74 //se decripteaza mesajul (ciphertext) si se obtine output-ul (plaintext)
75 if(1 != EVP_DecryptUpdate(context, plaintext, &lengthh, ciphertext, ciphertext_length))
76 handleErrors();
77 plaintext_length = lengthh;
78
79 //se finalizeaza procesul de decriptare
80 if(1 != EVP_DecryptFinal_ex(context, plaintext + lengthh, &lengthh))
81 handleErrors();
82 plaintext_length += lengthh;
83
84 // se elibereaza contextul din memorie
85 EVP_CIPHER_CTX_free(context);
86
87 return plaintext_length;
88}
89
90
91int main() {
92 /* Load the human readable error strings for libcrypto */
93 ERR_load_crypto_strings();
94
95 /* Load all digest and cipher algorithms */
96 OpenSSL_add_all_algorithms();
97
98 /* Load config file, and other important initialisation */
99 OPENSSL_config(NULL);
100//stuff to do
101
102 FILE *pt, *ct, *word_dictionary;
103 char line[100000], word[64];
104 int type,i;
105
106 pt = fopen("pt.txt", "r");
107 if(!pt) {
108 printf("Eroare la deschiderea fisierului!");
109 return 1;
110 }
111
112 fseek(pt, 0L, SEEK_END);
113 int pt_size = ftell(pt);
114 rewind(pt);
115
116 unsigned char *plaintext = malloc(100 *pt_size * sizeof(char*));
117 unsigned char *ciphertext = malloc(100 * pt_size * sizeof(char*));
118 unsigned char *new_plaintext = malloc(100 * pt_size * sizeof(char*));
119 fgets(plaintext, pt_size, pt);
120 fclose(pt);
121
122 printf("Scrie 0 pentru metoda ecb de criptare sau 1 pentru ofb: ");
123 scanf("%d",&type);
124
125 printf("%s %d\n",plaintext, type);
126
127 ecnrypt(type,plaintext,ciphertext,key,iv);
128
129 ct = fopen("ct.txt", "w");
130 if(!ct) {
131 printf("Eroare la deschiderea fisierului!");
132 return 1;
133 }
134
135 fwrite(ciphertext, 1, sizeof(ciphertext), ct);
136 fclose(ct);
137
138 word_dictionary = fopen("word_dict.txt", "r");
139 if(!word_dictionary) {
140 printf("Eroare la deschiderea fisierului!");
141 return 1;
142 }
143
144 int nr_iteratii = 0;
145 while(fgets(line, sizeof(line), word_dictionary))
146 {
147 line[strlen(line) - 1] = '\0';
148 //transform cuvintele din dictionar in format hexadecimal pentru a fi folosite la decriptare
149 for(i = 0; i < strlen(line); i++) {
150 sprintf((char*)word + (i*2), "%2X", line[i]);
151 }
152 //adaug spatii pana ajung la 16 biti
153 while(strlen(word) < 32) {
154 strcat(word, "20");
155 }
156
157 decrypt(type,new_plaintext,ciphertext,key,iv);
158
159 nr_iteratii++;
160 //daca sirurile suhnt egale, atunci am gasit cheia
161 if(strcmp((const char*) new_plaintext,(const char*) plaintext) == 0)
162 {
163 printf("Cheia %s a fost gasita dupa %d iteratii!", word, nr_iteratii);
164 break;
165 }
166 }
167
168 fclose(word_dictionary);
169
170 EVP_cleanup();
171
172 /* if you omit the next, a small leak may be left when you make use of the BIO (low level API) for e.g. base64 transformations */
173 CRYPTO_cleanup_all_ex_data();
174
175 /* Remove error strings */
176 ERR_free_strings();
177 return 0;
178}