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 decrypt_main(int argc
, char *argv
[])
22 std::cerr
<< "SlunkCrypt decrypt 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 decrypt.exe <nonce> <ciphertext.enc> <plaintext.out>\n" << std::endl
;
31 const uint64_t nonce
= std::strtoull(argv
[1], NULL
, 16);
33 // -----------------------------------------------------------
34 // Open input/output files
35 // -----------------------------------------------------------
37 std::ifstream
file_src(argv
[2], std::ios::binary
);
38 if (!file_src
.is_open())
40 std::cerr
<< "Error: Failed to open input file for reading!" << std::endl
;
44 std::ofstream
file_dst(argv
[3], std::ios::binary
);
45 if (!file_dst
.is_open())
47 std::cerr
<< "Error: Failed to open output file for writing!" << std::endl
;
51 // -----------------------------------------------------------
52 // Initialize the SlunkCryptDecr instance
53 // -----------------------------------------------------------
55 std::cerr
<< "Initializing key, please wait... " << std::flush
;
57 uint8_t buffer
[BUFF_SIZE
];
58 slunkcrypt::Decryptor
slunk_decrypt(nonce
, EXAMPLE_PASSWORD
);
60 std::cerr
<< "done.\nSlunk-decrypting the file contents, please wait... " << std::flush
;
62 // -----------------------------------------------------------
64 // -----------------------------------------------------------
66 file_src
.exceptions(std::ifstream::badbit
);
67 file_dst
.exceptions(std::ifstream::failbit
| std::ifstream::badbit
);;
71 while (file_src
.good())
73 file_src
.read(reinterpret_cast<char*>(buffer
), BUFF_SIZE
);
74 const std::streamsize count
= file_src
.gcount();
77 if (!slunk_decrypt
.inplace(buffer
, (size_t)count
))
79 std::cerr
<< "failed!\n\nError: SlunkCrypt decryption has failed!" << std::endl
;
82 file_dst
.write(reinterpret_cast<char*>(buffer
), count
);
86 catch (std::ios_base::failure e
)
88 std::cerr
<< "failed!\n\nI/O Error: \"" << e
.code().message() << "\" [Code: " << e
.code().value() << "]\n" << std::endl
;
92 // -----------------------------------------------------------
94 // -----------------------------------------------------------
96 std::cerr
<< "done.\n\nCompleted.\n" << std::endl
;
104 int main(int argc
, char *argv
[])
108 return decrypt_main(argc
, argv
);
110 catch (std::exception e
)
112 std::cerr
<< "\n\nException: \"" << e
.what() << "\"\n" << std::endl
;