-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
175 lines (156 loc) · 4.23 KB
/
main.c
File metadata and controls
175 lines (156 loc) · 4.23 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "blowfish.h"
#include "secure.c"
void start()
{
printf(" ___ ___ ___ ___ _____ ___ \n");
printf(" | || | ( ) ( ) | | ( ) | | || ||\n");
printf(" |___ | | | | __ |___ | | |___ | |___ | | | |\n");
printf(" | | | | | | || | | | | | | |\n");
printf(" |___ | || (___) (___) || (___) | |___ | || |\n");
}
int Blowfish_main() {
int type;
printf("Enter [1] for Encryption..\n");
printf("Enter [2] for Decryption..\n");
scanf("%d",&type);
int keylen, flag = 0;
char k, tmp;
char filename[50], dirname[50], filenametmp[50], sdir[50];
int fileindex = 0, filecount, dirindex = 0, dircount, delfile;
unsigned int retstrlen, retstrlen1;
unsigned char key[56];
unsigned long l, r, filelen;
unsigned char *cr, *cl;
short i = -1;
unsigned long percent;
unsigned long count;
if(type < 1 || type > 2) {
errno = EINVAL;
perror("Select [1] or [2] :");
return errno;
}
printf("\nEnter key:\t");
scanf("%s",key);
keylen = strlen(key);
/************************Strong key*********************/
while(++i < keylen) {
if(key[i] < 97 || key[i] > 122) {
flag = 1;
break;
}
keylen = strlen(key);
if(keylen < 4 || keylen > 56) { /*Argv[1] is my key length 32-448 bits */
perror("Key should be greater than 4 bytes and less than 56 bytes : ");
return -1;
}
/**********************Strong key***************************************/
FILE *fp1;
FILE *fp2;
char input_file_name[20],output_file_name[20];
printf("\n\tEnter Input File path:\t");
scanf("%s",input_file_name);
printf("\n\tEnter Output File path:\t");
scanf("%s",output_file_name);
fp1 = fopen(input_file_name, "r");
if(fp1 == NULL) {
perror("\tOpened failed : ");
return errno;
}
BLOWFISH_CTX ctx;
Blowfish_Init(&ctx, key, keylen);
percent = 0;
if(type==1) {
strcat(output_file_name, ".enc");
fp2=fopen(output_file_name,"wb");
if(fp2 == NULL) {
perror("\tOpened failed : ");
return errno;
}
fseek(fp1 , 0L, SEEK_END);
filelen = ftell(fp1);
rewind(fp1);
filelen = filelen + keylen; //Don't want to store bytes directly
fprintf(fp2, "%lu", filelen);
while(!feof(fp1)) {
l = r = 0X00000000;
retstrlen1 = fread(&l, sizeof(unsigned long), 1, fp1);
retstrlen = fread(&r, sizeof(unsigned long), 1, fp1);
i = 0;
Blowfish_Encrypt(&ctx, &l, &r);
cl = &l;
cr = &r;
for(i = 0; i < 8; i++) {
fprintf(fp2, "%c", cl[i]);
}
for(i = 0; i < 8; i++) {
fprintf(fp2, "%c", cr[i]);
}
printf("\t\rIn progress %d%%", (int)((16 * percent++ * 100) / (filelen - 8)));
fflush(stdout);
}
}
else if(type==2) {
strcat(output_file_name, ".dec");
fp2 = fopen(output_file_name, "w");
if(fp2 == NULL) {
perror("\tOpened failed : ");
return errno;
}
fscanf(fp1, "%lu", &filelen);
filelen = filelen - keylen;
while(!feof(fp1)) {
l = r = 0X00000000;
retstrlen1 = fread(&l, sizeof(unsigned long), 1, fp1);
retstrlen = fread(&r, sizeof(unsigned long), 1, fp1);
i = 0;
Blowfish_Decrypt(&ctx, &l, &r);
cl = &l;
cr = &r;
for(i = 0; i < 8; i++) {
if(count > filelen - 1)
break;
fprintf(fp2, "%c", cl[i]);
count++;
}
for(i = 0; i < 8; i++) {
if(count > filelen - 1)
break;
fprintf(fp2, "%c", cr[i]);
count++;
}
printf("\t\rIn progress %d%%", (int)((16 * percent++ * 100) / (filelen - 8)));
fflush(stdout);
}
}
fclose(fp1);
fclose(fp2);
printf("\n");
if(type==1)
printf("\tYour data is successfully encrypted in %s\n", output_file_name);
else{
printf("\tYour data is successfully decrypted in %s\n",output_file_name);
break;}
}
return 0;
}
int main()
{
int op;
start();
printf("\n\n");
printf("\tEnter [1] to select AES \n");
printf("\tEnter [2] to select Blowfish\n");
scanf("%d",&op);
if(op == 1)
AES_main();
else Blowfish_main();
return 0;
}