-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFcrypt.cc
More file actions
137 lines (130 loc) · 4.38 KB
/
Fcrypt.cc
File metadata and controls
137 lines (130 loc) · 4.38 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
#include "iostream"
#include "File.h"
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <string.h>
#include "Exception.h"
#define BUFFER_SIZE 128
using namespace std;
int main(const int argc, const char **argv)
{
string inFileName = "";
string outFileName = "";
string ckey = "01234567890123456789012345678901";
string ivec = "0123456789012345";
string mode = "e";
if (argc != 6)
{
cout << "Interactive mode..." << endl;
cout << "Enter source file name..." << endl;
cin >> inFileName;
cout << "Enter destination file name..." << endl;
cin >> outFileName;
cout << "Enter a secret key consisting of 32 charecters..." << endl;
cin >> ckey;
cout << "Enter a secret initial vector consisting of 16 charecters..." << endl;
cin >> ivec;
cout << "Choose e for encryption / d for decryption..." << endl;
cin >> mode;
}
else
{
inFileName = argv[1];
outFileName = argv[2];
ckey = argv[3];
ivec = argv[4];
mode = argv[5];
}
if (inFileName.length() == 0 || outFileName.length() == 0)
{
cout << "Enter valid file names..." << endl;
return 1;
}
if (ckey.length() != 32)
{
cout << "secret key is not consisting of 32 charecters, using defaults..." << endl;
ckey = "01234567890123456789012345678901";
}
if (ivec.length() != 16)
{
cout << "secret initial vector is not consisting of 16 charecters, using defaults..." << endl;
ivec = "0123456789012345";
}
File sourceFile(inFileName.c_str(), "r");
File destinationFile(outFileName.c_str(), "w");
EVP_CIPHER_CTX *ctx;
int s = 0, read = 0;
/* Create and initialise the context */
if (!(ctx = EVP_CIPHER_CTX_new()))
{
ERR_print_errors_fp(stderr);
abort();
}
int cipherBlockSize = EVP_CIPHER_block_size(EVP_aes_256_cbc());
try
{
/* code */
if (strcmp(mode.c_str(), "e") == 0)
{
cout << "Starting Encryption..." << endl;
if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, (const unsigned char *)ckey.c_str(), (const unsigned char *)ivec.c_str()))
{
throw Exception(OPENSSL_INIT_ERROR);
}
while (true)
{
Buffer inBuffer(BUFFER_SIZE);
Buffer outBuffer(BUFFER_SIZE + cipherBlockSize);
read = sourceFile.read(BUFFER_SIZE, inBuffer);
if (read == BUFFER_SIZE)
{
cout << "Reading..." << endl;
s = sourceFile.encryptBlock((void *)ctx, BUFFER_SIZE, inBuffer, outBuffer, false);
destinationFile.write(s, outBuffer);
}
else
{
cout << "Reading last block..." << endl;
s = sourceFile.encryptBlock((void *)ctx, read, inBuffer, outBuffer, true);
destinationFile.write(s, outBuffer);
break;
}
}
}
if (strcmp(mode.c_str(), "d") == 0)
{
cout << "Starting Decryption..." << endl;
if (1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, (const unsigned char *)ckey.c_str(), (const unsigned char *)ivec.c_str()))
{
throw Exception(OPENSSL_INIT_ERROR);
}
while (true)
{
Buffer inBuffer(BUFFER_SIZE);
Buffer outBuffer(BUFFER_SIZE + cipherBlockSize);
read = sourceFile.read(BUFFER_SIZE, inBuffer);
if (read == BUFFER_SIZE)
{
cout << "Reading..." << endl;
s = sourceFile.decryptBlock((void *)ctx, BUFFER_SIZE, inBuffer, outBuffer, false);
destinationFile.write(s, outBuffer);
}
else
{
cout << "Reading last block..." << endl;
s = sourceFile.decryptBlock((void *)ctx, read, inBuffer, outBuffer, true);
destinationFile.write(s, outBuffer);
break;
}
}
}
}
catch (Exception &e)
{
cout << e.getMessage();
}
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return 0;
}