-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecrypt_example.cc
54 lines (44 loc) · 1.38 KB
/
decrypt_example.cc
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
#include "crypto.h"
#include "status.h"
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <optional>
#include <string>
#include <vector>
using ::ette::CryptoAlgorithm;
using ::ette::CryptoState;
using ::ette::Decrypt;
using ::ette::StatusCode;
std::optional<std::string> ReadFileToString(const std::string& path) {
std::ifstream file(path);
if (!file.is_open()) {
return std::nullopt;
}
std::string content((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
return content;
}
int main(int argc, char* argv[]) {
// Use first argument from argv as file path. Use second argument as password
if (argc != 3) {
std::cerr << "Usage: decrypt_example <filename> <password>"
<< std::endl;
exit(1);
}
// Read file contents into string.
std::optional<std::string> file_contents = ReadFileToString(argv[1]);
if (!file_contents) {
std::cerr << "Could not read file: " << argv[1] << std::endl;
exit(1);
}
std::string password = argv[2];
// Decrypt file contents.
const CryptoState state =
Decrypt(*file_contents, password, CryptoAlgorithm::kAES256CBC);
if (!state.status.ok()) {
std::cerr << "Could not decrypt file: " << argv[1] << std::endl;
exit(1);
}
std::cout << state.plaintext << std::endl;
}