· 6 years ago · Oct 23, 2019, 02:18 AM
1cipher[k] = hex_to_ascii(buf, st[i]); // value will be pass with buf=0 and possition value of st[i]
2k++; // k variable is initialized with 0 above that will be increment by 1 now
3}
4else
5{
6buf = st[i]; // if value of i is equal to 0 then insert null in the cipher[k] array position
7}
8}
9cipher[k] = '\0';
10in = fopen("/home/seed/Desktop/english_wrd_list.txt", "r"); // open the file in read mode
11if(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
12{
13printf("\n cannot open file"); // error will be thrown
14exit(1); // and exit from the program
15}
16
17EVP_CIPHER_CTX ctx; // declared the libraray data type variable ctx
18EVP_CIPHER_CTX_init(&ctx); // storing address of ctx for reference with memory allocation initialization
19
20while(fgets(key, sizeof(key), in) != NULL) // key[16] is not NULL this loop will continue
21{
22l = 0;
23if(strlen(key) < 16) // if the key length is lessthan 16
24{
25l = strlen(key)-1; // substract 1 from key length and store in variable l ie if key size is 13 then l will contain 12
26while(l < 16) // if l is less than 16
27{
28key[l] = ' '; // put space in the key array with possition of l
29l++; // increment l with 1 , declared above
30}
31key[l] = '\0'; // else insert null byte in the key array
32}
33else
34key[16] = '\0'; // else insert null byte at the end of key array at possition 16 last position of array
35
36
37EVP_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
38
39if(!EVP_EncryptUpdate(&ctx, outbuf, &outlen, intext, strlen(intext))) // if this parameteres are not passed then return with 0
40{
41return 0;
42}
43
44if(!EVP_EncryptFinal_ex(&ctx, outbuf + outlen, &tmplen)) // if this parameteres are nt passed then return with 0
45{
46return 0;
47}
48outlen += tmplen; // outlen = outlen + tmplen
49EVP_CIPHER_CTX_cleanup(&ctx); // openssl cleanup api to clear the ctx address values
50
51count = 0;
52for(i = 0; i < 32; i++) // this loop will copy the encrypted outbuf to cipher buffer array
53{
54if(cipher[i] == outbuf[i])
55count++;
56}
57
58if(count == 32)
59{
60printf("\nplain text...................%s\n",intext); // plaint text display
61printf("\ncipher text to be compared...\n%s", cipher); // cipher text display
62printf("\niv...........................%s",iv); // initialization vector
63printf("\n....key found....\n");
64printf("\n key.........................%s",key); // key display
65printf("\n actual cipher text..........\n%s", cipher); // encrypted chipher
66printf("\n formed cipher text..........\n%s\n", outbuf); // output buffer stored text
67found = 1; // if it found properly values then set found to 1
68break;
69}
70
71}
72fclose(in); // close the file in variable
73if(found == 0) // if variable found equal to zero
74{
75printf("\niv...........................%s",iv); // print initialization vector value
76printf("\nplain text...................%s\n",intext); // plaint text
77printf("cipher text..........%s\n", cipher); // encrypted cipher text
78printf("cipher text in hex...%s\n",st); // st array value
79printf("\n\n key cannot be found for the above cipher text\n"); // key not found message as found status is set to 0
80}
81return 0;
82}
83
84Makefile
85***********
86
87INC=/usr/local/ssl/include/
88LIB=/usr/local/ssl/lib/
89
90all:
91 gcc -I$(INC) -L$(LIB) -o lab crypt.c -lcrypto -ldl
92
93english_wrd_list.txt
94**********************
95you can take word list directory from internet or from linux wordlist and change the path from code as well.
96
97if 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 :)