GUI: Small improvement in executable version check.
[slunkcrypt.git] / examples / example_decrypt.cpp
blob712489e57755506ba90279f2c61e300b18608411
1 /******************************************************************************/
2 /* SlunkCrypt, by LoRd_MuldeR <MuldeR2@GMX.de> */
3 /* This work has been released under the CC0 1.0 Universal license! */
4 /******************************************************************************/
6 //SlunkCrypt API
7 #include <slunkcrypt.hpp>
9 //CRT
10 #include <cstdlib>
11 #include <iostream>
12 #include <fstream>
14 //Example password
15 static std::string EXAMPLE_PASSWORD("cMRe5E5D)'!2?5]QDlCQ4tBb");
17 //Const
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;
25 if (argc < 4)
27 std::cerr << "Usage:\n decrypt.exe <nonce> <ciphertext.enc> <plaintext.out>\n" << std::endl;
28 return -1;
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;
41 return -1;
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;
48 return -1;
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 // -----------------------------------------------------------
63 // Decryption loop
64 // -----------------------------------------------------------
66 file_src.exceptions(std::ifstream::badbit);
67 file_dst.exceptions(std::ifstream::failbit | std::ifstream::badbit);;
69 try
71 while (file_src.good())
73 file_src.read(reinterpret_cast<char*>(buffer), BUFF_SIZE);
74 const std::streamsize count = file_src.gcount();
75 if (count > 0)
77 if (!slunk_decrypt.inplace(buffer, (size_t)count))
79 std::cerr << "failed!\n\nError: SlunkCrypt decryption has failed!" << std::endl;
80 return -1;
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;
89 return -1;
92 // -----------------------------------------------------------
93 // Clean up
94 // -----------------------------------------------------------
96 std::cerr << "done.\n\nCompleted.\n" << std::endl;
98 file_src.close();
99 file_dst.close();
101 return 0;
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;
113 return -1;