forked from shanet/Crypto-Example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto-file-example.cpp
160 lines (123 loc) · 4.59 KB
/
crypto-file-example.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
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
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "base64.h"
#include "Crypto.h"
// Uncomment to write the encrypted file as base64
//#define CONVERT_TO_BASE64
// Note: This isn't a good way to encrypt large file (anything that can't be read into
// memory in a single buffer). A better approach for this is to read in one block at a type,
// encrypt it, write it to a file and so on.
using namespace std;
void writeFile(char *filename, unsigned char *file, size_t fileLength);
int readFile(char *filename, unsigned char **file);
int main(int argc, char **argv) {
if(argc != 2) {
fprintf(stderr, "No file argument supplied.\n");
return 1;
}
char *filename = argv[1];
// Create our crypto object
Crypto crypto;
// Read the file to encrypt
unsigned char *file;
size_t fileLength = readFile(filename, &file);
printf("%d bytes to be encrypted\n", (int)fileLength);
// Encrypt the file
unsigned char *encryptedFile;
int encryptedFileLength;
if((encryptedFileLength = crypto.aesEncrypt((const unsigned char*)file, fileLength, &encryptedFile)) == -1) {
fprintf(stderr, "Encryption failed\n");
return 1;
}
printf("%d bytes encrypted\n", encryptedFileLength);
// Append .enc to the filename
char *encryptedFilename = (char*)malloc(strlen(filename) + 5);
if(encryptedFilename == NULL) {
fprintf(stderr, "Failed to allocate memory\n");
return 1;
}
sprintf(encryptedFilename, "%s.enc", filename);
#ifdef CONVERT_TO_BASE64
// Encode the encrypted file to base64
char *base64Buffer = base64Encode(encryptedFile, encryptedFileLength);
// Change the encrypted file pointer to the base64 string and update
// the length (we can use strlen() now since the base64 string is ASCII data)
free(encryptedFile);
encryptedFile = (unsigned char*)base64Buffer;
encryptedFileLength = strlen((char*)encryptedFile);
#endif
// Write the encrypted file to its own file
writeFile(encryptedFilename, encryptedFile, encryptedFileLength);
printf("Encrypted message written to \"%s\"\n", encryptedFilename);
free(file);
// Read the encrypted file back
fileLength = readFile(encryptedFilename, &file);
#ifdef CONVERT_TO_BASE64
// Decode the encrypted file from base64
unsigned char *binaryBuffer;
fileLength = base64Decode((char*)file, fileLength, &binaryBuffer);
// Change the pointer of the string containing the file info to the decoded base64 string
free(file);
file = binaryBuffer;
#endif
// Decrypt the encrypted file
unsigned char *decryptedFile;
int decryptedFileLength;
if((decryptedFileLength = crypto.aesDecrypt(file, fileLength, &decryptedFile)) == -1) {
fprintf(stderr, "Decryption failed\n");
return 1;
}
printf("%d bytes decrypted\n", (int)decryptedFileLength);
// Append .dec to the filename
char *decryptedFilename = (char*)malloc(strlen(filename) + 5);
if(decryptedFilename == NULL) {
fprintf(stderr, "Failed to allocate memory\n");
return 1;
}
sprintf(decryptedFilename, "%s.dec", filename);
// Write the decrypted file to its own file
writeFile(decryptedFilename, decryptedFile, decryptedFileLength);
printf("Decrypted file written to \"%s\"\n", decryptedFilename);
free(decryptedFile);
free(file);
return 0;
}
void writeFile(char *filename, unsigned char *file, size_t fileLength) {
FILE *fd = fopen(filename, "wb");
if(fd == NULL) {
fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
exit(1);
}
size_t bytesWritten = fwrite(file, 1, fileLength, fd);
if(bytesWritten != fileLength) {
fprintf(stderr, "Failed to write file\n");
exit(1);
}
fclose(fd);
}
int readFile(char *filename, unsigned char **file) {
FILE *fd = fopen(filename, "rb");
if(fd == NULL) {
fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
exit(1);
}
// Determine size of the file
fseek(fd, 0, SEEK_END);
size_t fileLength = ftell(fd);
fseek(fd, 0, SEEK_SET);
// Allocate space for the file
*file = (unsigned char*)malloc(fileLength);
if(*file == NULL) {
fprintf(stderr, "Failed to allocate memory\n");
exit(1);
}
// Read the file into the buffer
size_t bytesRead = fread(*file, 1, fileLength, fd);
if(bytesRead != fileLength) {
fprintf(stderr, "Error reading file\n");
exit(1);
}
fclose(fd);
return fileLength;
}