-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecrypt.cpp
More file actions
104 lines (73 loc) · 2.91 KB
/
decrypt.cpp
File metadata and controls
104 lines (73 loc) · 2.91 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
#include <filesystem>
#include <fstream>
#include <ios>
#include <iostream>
#include <sodium.h>
#include <sodium/core.h>
#include <sodium/crypto_box.h>
#include <sodium/crypto_secretstream_xchacha20poly1305.h>
#include "headers.h"
int decrypt(Config cfg) {
namespace fs = std::filesystem;
if(cfg.pubDir.length() < 1 && cfg.secDir.length() < 1) {
if(!fs::is_directory(cfg.keysDir)) {
std::cout << "Generating keys...\n";
generateKeypair();
}
}
if(sodium_init() != 0) {
std::cerr << "Error sodium\n";
}
std::ifstream file(cfg.file, std::ios::binary);
if(!file) {
std::cerr << "Cannot open input file\n";
return -1;
}
// Reading keys
unsigned char publicKey[crypto_box_PUBLICKEYBYTES];
unsigned char secretKey[crypto_box_SECRETKEYBYTES];
fs::path pubPath, secPath;
findKeys(pubPath, secPath, cfg);
std::ifstream pubFile(pubPath, std::ios::binary);
pubFile.read(reinterpret_cast<char*>(publicKey), crypto_box_PUBLICKEYBYTES);
std::ifstream secFile(secPath, std::ios::binary);
secFile.read(reinterpret_cast<char*>(secretKey), crypto_box_SECRETKEYBYTES);
// Reading official data and decrypting stream key
unsigned char boxNonce[crypto_box_NONCEBYTES];
file.read(reinterpret_cast<char*>(boxNonce), sizeof boxNonce);
unsigned char boxedKey[crypto_box_MACBYTES + crypto_secretstream_xchacha20poly1305_KEYBYTES];
file.read(reinterpret_cast<char*>(boxedKey), sizeof boxedKey);
unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES];
file.read(reinterpret_cast<char*>(header), sizeof header);
unsigned char streamKey[crypto_secretstream_xchacha20poly1305_KEYBYTES];
if(crypto_box_open_easy(streamKey, boxedKey, sizeof boxedKey, boxNonce, publicKey, secretKey) != 0) {
std::cerr << "Failed to decrypt streamKey\n";
return -1;
}
crypto_secretstream_xchacha20poly1305_state state;
crypto_secretstream_xchacha20poly1305_init_pull(&state, header, streamKey);
std::string filename;
if(cfg.filename != "" && cfg.filename.size() > 0) {
filename = cfg.filename;
} else {
filename = cfg.file + ".out";
}
std::ofstream out(filename.c_str(), std::ios::binary);
// Decrypting file by chunks
unsigned char fileBuffer[CHUNK_SIZE + crypto_secretstream_xchacha20poly1305_ABYTES];
unsigned char outBuffer[CHUNK_SIZE];
while(file.good()) {
file.read(reinterpret_cast<char*>(fileBuffer), CHUNK_SIZE + crypto_secretstream_xchacha20poly1305_ABYTES);
std::streamsize readBytes = file.gcount();
if(readBytes <= 0) break;
unsigned long long out_len;
unsigned char tag;
if(crypto_secretstream_xchacha20poly1305_pull(&state, outBuffer, &out_len, &tag, fileBuffer, readBytes, nullptr, 0) != 0) {
std::cerr << "Failed to decrypt file. Maybe it's corrupted or forged.\n";
return 1;
}
out.write(reinterpret_cast<char*>(outBuffer), out_len);
}
std::cout << "Decrypted successfully\n";
return 0;
}