-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCryptoLib93.cpp
More file actions
96 lines (75 loc) · 1.72 KB
/
Copy pathCryptoLib93.cpp
File metadata and controls
96 lines (75 loc) · 1.72 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
#include <iostream>
#include <string>
#include <string_view>
#include "sha.h"
#include "hex.h"
#include "files.h"
#include "default.h"
#ifdef USE_BOOST_FILESYSTEM
# include<boost/filesystem/path.hpp>
# include<boost/filesystem/operations.hpp>
#else
# include<filesystem>
# ifdef FILESYSTEM_EXPERIMENTAL
namespace fs = std::experimental::filesystem;
# else
namespace fs = std::filesystem;
# endif
#endif
void encrypt_file(
fs::path const& sourcefile,
fs::path const& destfile,
std::string_view password)
{
CryptoPP::FileSource source(
sourcefile.c_str(),
true,
new CryptoPP::DefaultEncryptorWithMAC((CryptoPP::byte*)password.data(), password.size(),
new CryptoPP::FileSink(
destfile.c_str())
)
);
}
void encrypt_file(
fs::path const& filepath,
std::string_view password
)
{
auto temp = fs::temp_directory_path() / filepath.filename();
encrypt_file(filepath, temp, password);
fs::remove(filepath);
fs::rename(temp, filepath);
}
void decrypt_file(
fs::path const& sourcefile,
fs::path const& destfile,
std::string_view password
)
{
CryptoPP::FileSource source(
sourcefile.c_str(),
true,
new CryptoPP::DefaultDecryptorWithMAC(
(CryptoPP::byte*)password.data(), password.size(),
new CryptoPP::FileSink(
destfile.c_str())
)
);
}
void decrypt_file(
fs::path const& filepath,
std::string_view password
)
{
auto temp = fs::temp_directory_path() / filepath.filename();
decrypt_file(filepath, temp, password);
fs::remove(filepath);
fs::rename(temp, filepath);
}
int main()
{
encrypt_file("sample.txt", "sample.txt.enc", "cppchallenger");
decrypt_file("sample.txt.enc", "sample.txt.dec", "cppchallenger");
encrypt_file("sample.txt", "cppchallenger");
decrypt_file("sample.txt", "cppchallenger");
}