-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileio.cpp
72 lines (50 loc) · 1.57 KB
/
fileio.cpp
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
#include <fstream>
#include <vector>
#include <cstdint>
#include <iostream>
#include <iomanip>
#include <filesystem>
#include "helpers.h"
#include "fileio.h"
using namespace std;
void fileio::GetPlainTextFromFile(vector<char>* plainTextBuffer, string fileName) {
ifstream plaintextFile(fileName, ios::in | ios::binary | std::ios::ate);
streamsize psize = plaintextFile.tellg();
while (psize % 8 != 0) {
psize++;
}
plainTextBuffer->resize(psize);
plaintextFile.seekg(0, std::ios::beg);
plaintextFile.read(plainTextBuffer->data(), psize);
}
void fileio::GetCipherTextFromFile(vector<uint64_t>* cipherTextBuffer, string fileName) {
ifstream ciphertextFile;
ciphertextFile.open(fileName, ios::in | ios::binary);
uint64_t result = 0;
while (!ciphertextFile.eof()) {
ciphertextFile >> std::hex;
ciphertextFile >> result;
cipherTextBuffer->push_back(result);
}
cipherTextBuffer->pop_back();
}
void fileio::WriteToFile(uint64_t ciphertext, const char* fileName) {
ofstream outputFile;
helpers help;
outputFile.open(fileName, ios::out | ios::binary | ios::app);
if (fileName == "ciphertext.txt") {
outputFile << std::hex << ciphertext << std::dec << " ";
}
else {
string ptext = help.Convert64ToString(ciphertext);
outputFile << ptext;
}
}
uint64_t fileio::GetKeyFromFile(string fileName) {
ifstream keyFile;
keyFile.open(fileName, ios::in | ios::binary);
uint64_t result = 0;
keyFile >> std::hex;
keyFile >> result;
return result;
}