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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use std::env;
use std::fs::File;
use std::io::{Read, Write};
use h_encrypt::{encrypt_bytes, encrypt_stream, decrypt_bytes, decrypt_stream};
const MAJOR_VERSION: usize = 0;
const MINOR_VERSION: usize = 2;
const PATCH_VERSION: usize = 3;
fn main() {
let args = env::args().collect::<Vec<String>>();
if args.len() > 1 {
if args[1] == "--help".to_string() || args[1] == "-h".to_string() {
print_help();
}
else if args[1] == "--version".to_string() || args[1] == "-v".to_string() {
println!("{}.{}.{}", MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION);
}
else if args[1] == "--encrypt".to_string() || args[1] == "-e".to_string() {
if args.len() < 5 {
println!("Invalid number of arguments!\n\n");
print_help();
return;
}
match File::open(&args[2]) {
Ok(mut f) => {
match f.metadata() {
Ok(m) => if m.len() > 2_000_000 { // 32 MB
match encrypt_stream(&args[2], &args[3], &args[4].as_bytes(), 1_000_000) {
Err(e) => {
println!("Encryption Error: {:?}", e);
},
_ => {
println!("File Encryption Successful");
}
}
} else {
let mut buf = vec![0; m.len() as usize];
match f.read(&mut buf) {
Err(e) => {
println!("File IO Error with {}: {:?}", args[2], e);
}
_ => {}
}
let enc = encrypt_bytes(&buf, &args[4].as_bytes());
match File::create(&args[3]) {
Ok(mut ff) => {
match ff.write_all(&enc) {
Err(e) => {
println!("File IO Error with {}: {:?}", args[3], e);
}
Ok(_) => {
println!("File Encryption Successful");
}
}
}
Err(e) => {
println!("File IO Error with {}: {:?}", args[3], e);
}
}
}
Err(e) => {
println!("File IO Error with {}: {:?}", args[2], e);
}
}
}
Err(e) => {
println!("File IO Error with {}: {:?}", args[2], e);
}
}
}
else if args[1] == "--decrypt".to_string() || args[1] == "-d".to_string() {
if args.len() < 5 {
println!("Invalid number of arguments!\n\n");
print_help();
return;
}
match File::open(&args[2]) {
Ok(mut f) => {
match f.metadata() {
Ok(m) => if m.len() > 2_000_000 { // 32 MB
match decrypt_stream(&args[2], &args[3], &args[4].as_bytes(), 1_000_000) {
Err(e) => {
println!("Decryption Error: {:?}", e);
},
_ => {
println!("File Decryption Successful");
}
}
} else {
let mut buf = vec![0; m.len() as usize];
match f.read(&mut buf) {
Err(e) => {
println!("File IO Error with {}: {:?}", args[2], e);
}
_ => {}
}
let enc = match decrypt_bytes(&buf, &args[4].as_bytes()) {
Ok(v) => v,
Err(e) => {
println!("Decryption Error: {:?}", e);
return;
}
};
match File::create(&args[3]) {
Ok(mut ff) => {
match ff.write_all(&enc) {
Err(e) => {
println!("File IO Error with {}: {:?}", args[3], e);
}
Ok(_) => {
println!("File Decryption Successful");
}
}
}
Err(e) => {
println!("File IO Error with {}: {:?}", args[3], e);
}
}
}
Err(e) => {
println!("File IO Error with {}: {:?}", args[2], e);
}
}
}
Err(e) => {
println!("File IO Error with {}: {:?}", args[2], e);
}
}
}
else {
println!("Invalid Arguments!\n\n");
print_help();
}
}
else {
print_help();
}
}
fn print_help() {
println!(
"h_encrypt {}.{}.{}
encryption/decryption utility written from scratch (including the algorithm).
-h / --help
show this message
-v / --version
show version
-e / --encrypt <path 1> <path 2> <password>
read <path 1>, encrypt the read data with <password>, and write the encrypted data to <path 2>
-d / --decrypt <path 1> <path 2> <password>
read <path 1>, decrypt the read data with <password>, and write the decrypted data to <path 2>
",
MAJOR_VERSION,
MINOR_VERSION,
PATCH_VERSION
);
}