· 6 years ago · Oct 23, 2019, 02:20 AM
1crypt.c
2********
3
4#include <openssl/conf.h>
5#include <openssl/evp.h>
6#include <openssl/err.h>
7#include <stdlib.h>
8#include <string.h>
9#include <stdio.h>
10
11int hex_to_int(char c){ // hex to int will come with parameter value c which is passed from main function, line 53
12int first = c / 16 - 3; // if c is say 70 then 5-3 = 2 will be in the first variable value
13int second = c % 16; // 70 % 16 = 6 will be in the second variable value
14int result = first*10 + second; // result will contain 2*10=20+6 = 26
15if(result > 9) // if result is greater then 9 then result -- so result will be 25
16result--;
17return result; // return the result
18}
19
20int hex_to_ascii(char c, char d){ // hex to ascci will bring with two parameteres , assume c = 70 , d=5
21int high = hex_to_int(c) * 16; // 70 * 16 will be passed to above function and store the high variable
22int low = hex_to_int(d); // 5 will be pass to above function and return value will be store in low
23return high+low; // return the total of both
24}
25
26int main(int arc, char *argv[])
27{
28unsigned char outbuf[1024]; // output buffer for storing output string
29unsigned char cipher[1024]; // cipher buffer array will store encrypted strings
30unsigned char temp, key[16]; // key array will store key for this encryption , temp variable for temp storage
31int outlen, tmplen, l, i, length, count, found =0, k = 0;
32size_t nread, len;
33FILE *in; // file operation for input
34unsigned char iv[17]; // initialization vector
35
36for(i = 0; i < 17; i++) // this loop will fill null byte on all 17 IV values
37iv[i] = 0;
38iv[16] = '\0';
39
40char intext[] = "This is a top secret."; // plain text
41char st[] = "8d20e5056a8d24d0462ce74e4904c1b513e10d1df4a2ef2ad4540fae1ca0aaf9"; // SHA2-256 hexadecimal key of 64bit
42i = 0;
43while(i < 64) // this loop will convert small alphabatic to capital character
44{
45if(st[i] >= 'a' && st[i] <= 'z')
46st[i] = st[i] - 32;
47i++;
48}
49
50length = strlen(st); // this will find the length of st array and store in length variable
51char buf = 0; // character type variable buf
52
53for(i = 0; i < length; i++) // this loop will run until the length over which we measure above
54{
55if(i % 2 != 0) // if value of i modulo 2 is not equal to zero then pass that value of st[i] array to hex_to_ascii function which we created above
56{
57cipher[k] = hex_to_ascii(buf, st[i]); // value will be pass with buf=0 and possition value of st[i]
58k++; // k variable is initialized with 0 above that will be increment by 1 now
59}
60else
61{
62buf = st[i]; // if value of i is equal to 0 then insert null in the cipher[k] array position
63}
64}
65cipher[k] = '\0';
66in = fopen("/home/seed/Desktop/english_wrd_list.txt", "r"); // open the file in read mode
67if(in == NULL) // if path is wrong then in value will be null that means an error occured or file not found or path not found or may not have read rights
68{
69printf("\n cannot open file"); // error will be thrown
70exit(1); // and exit from the program
71}
72
73EVP_CIPHER_CTX ctx; // declared the libraray data type variable ctx
74EVP_CIPHER_CTX_init(&ctx); // storing address of ctx for reference with memory allocation initialization
75
76while(fgets(key, sizeof(key), in) != NULL) // key[16] is not NULL this loop will continue
77{
78l = 0;
79if(strlen(key) < 16) // if the key length is lessthan 16
80{
81l = strlen(key)-1; // substract 1 from key length and store in variable l ie if key size is 13 then l will contain 12
82while(l < 16) // if l is less than 16
83{
84key[l] = ' '; // put space in the key array with possition of l
85l++; // increment l with 1 , declared above
86}
87key[l] = '\0'; // else insert null byte in the key array
88}
89else
90key[16] = '\0'; // else insert null byte at the end of key array at possition 16 last position of array
91
92
93EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, key, iv); // this is openssl library you can see the reference here https://www.openssl.org/docs/man1.1.0/man3/EVP_CipherInit_ex.html
94
95if(!EVP_EncryptUpdate(&ctx, outbuf, &outlen, intext, strlen(intext))) // if this parameteres are not passed then return with 0
96{
97return 0;
98}
99
100if(!EVP_EncryptFinal_ex(&ctx, outbuf + outlen, &tmplen)) // if this parameteres are nt passed then return with 0
101{
102return 0;
103}
104outlen += tmplen; // outlen = outlen + tmplen
105EVP_CIPHER_CTX_cleanup(&ctx); // openssl cleanup api to clear the ctx address values
106
107count = 0;
108for(i = 0; i < 32; i++) // this loop will copy the encrypted outbuf to cipher buffer array
109{
110if(cipher[i] == outbuf[i])
111count++;
112}
113
114if(count == 32)
115{
116printf("\nplain text...................%s\n",intext); // plaint text display
117printf("\ncipher text to be compared...\n%s", cipher); // cipher text display
118printf("\niv...........................%s",iv); // initialization vector
119printf("\n....key found....\n");
120printf("\n key.........................%s",key); // key display
121printf("\n actual cipher text..........\n%s", cipher); // encrypted chipher
122printf("\n formed cipher text..........\n%s\n", outbuf); // output buffer stored text
123found = 1; // if it found properly values then set found to 1
124break;
125}
126
127}
128fclose(in); // close the file in variable
129if(found == 0) // if variable found equal to zero
130{
131printf("\niv...........................%s",iv); // print initialization vector value
132printf("\nplain text...................%s\n",intext); // plaint text
133printf("cipher text..........%s\n", cipher); // encrypted cipher text
134printf("cipher text in hex...%s\n",st); // st array value
135printf("\n\n key cannot be found for the above cipher text\n"); // key not found message as found status is set to 0
136}
137return 0;
138}
139
140Makefile
141***********
142
143INC=/usr/local/ssl/include/
144LIB=/usr/local/ssl/lib/
145
146all:
147 gcc -I$(INC) -L$(LIB) -o lab crypt.c -lcrypto -ldl
148
149english_wrd_list.txt
150**********************
151you can take word list directory from internet or from linux wordlist and change the path from code as well.
152
153if you have any doubt then please ask me without any hesitation in the comment section below , if you like my answer then please thumbs up for the answer , before giving thumbs down please discuss the question it may possible that we may understand the question different way and we can edit and change the answers if you argue, thanks :)