1 /******************************************************************************/
2 /* SlunkCrypt, by LoRd_MuldeR <MuldeR2@GMX.de> */
3 /* This work has been released under the CC0 1.0 Universal license! */
4 /******************************************************************************/
7 #include <slunkcrypt.hpp>
15 static std::string
EXAMPLE_PASSWORD("cMRe5E5D)'!2?5]QDlCQ4tBb");
18 #define BUFF_SIZE 4096U
20 static int encrypt_main(int argc
, char *argv
[])
22 std::cerr
<< "SlunkCrypt encrypt sample [" << __DATE__
<< "]" << std::endl
;
23 std::cerr
<< "using libSlunkCrypt v" << SLUNKCRYPT_VERSION_MAJOR
<< '.' << SLUNKCRYPT_VERSION_MINOR
<< '.' << SLUNKCRYPT_VERSION_PATCH
<< '\n' << std::endl
;
27 std::cerr
<< "Usage:\n encrypt.exe <plaintext.txt> <ciphertext.enc>\n" << std::endl
;
31 // -----------------------------------------------------------
32 // Open input/output files
33 // -----------------------------------------------------------
35 std::ifstream
file_src(argv
[1], std::ios::binary
);
36 if (!file_src
.is_open())
38 std::cerr
<< "Error: Failed to open input file for reading!" << std::endl
;
42 std::ofstream
file_dst(argv
[2], std::ios::binary
);
43 if (!file_dst
.is_open())
45 std::cerr
<< "Error: Failed to open output file for writing!" << std::endl
;
49 // -----------------------------------------------------------
50 // Initialize the SlunkCryptEncr instance
51 // -----------------------------------------------------------
53 std::cerr
<< "Initializing key, please wait... " << std::flush
;
55 uint8_t buffer
[BUFF_SIZE
];
56 slunkcrypt::Encryptor
slunk_encrypt(EXAMPLE_PASSWORD
);
58 std::cerr
<< "done.\nSlunk-encrypting the file contents, please wait... " << std::flush
;
60 // -----------------------------------------------------------
62 // -----------------------------------------------------------
64 file_src
.exceptions(std::ifstream::badbit
);
65 file_dst
.exceptions(std::ifstream::failbit
| std::ifstream::badbit
);
69 while (file_src
.good())
71 file_src
.read(reinterpret_cast<char*>(buffer
), BUFF_SIZE
);
72 const std::streamsize count
= file_src
.gcount();
75 if (!slunk_encrypt
.inplace(buffer
, (size_t)count
))
77 std::cerr
<< "failed!\n\nError: SlunkCrypt encryption has failed!" << std::endl
;
80 file_dst
.write(reinterpret_cast<char*>(buffer
), count
);
84 catch (std::ios_base::failure e
)
86 std::cerr
<< "failed!\n\nI/O Error: \"" << e
.code().message() << "\" [Code: " << e
.code().value() << "]\n" << std::endl
;
90 // -----------------------------------------------------------
92 // -----------------------------------------------------------
94 std::cerr
<< "done.\n\nNonce: " << std::hex
<< std::uppercase
<< slunk_encrypt
.get_nonce() << '\n' << std::endl
;
102 int main(int argc
, char *argv
[])
106 return encrypt_main(argc
, argv
);
108 catch (std::exception e
)
110 std::cerr
<< "\n\nException: \"" << e
.what() << "\"\n" << std::endl
;