Thank you for your reply.
I do know about the manual and have used it a lot.
What I want to confirm with the code is that I am using the manual and the
library correctly.

Best,
Trung

God loved the birds and invented trees.  Man loved the birds and invented
cages.
  ~Jacques Deval

Probability does not exist.
  ~Bruno de Finetti

Brian: "Look, you've got it all wrong! You don't NEED to follow ME, You
don't NEED to follow ANYBODY! You've got to think for your selves! You're
ALL individuals!"
  ~Monty Python, "The Life of Brian"

On Sun, Apr 15, 2018 at 2:27 AM, <hanluluge...@gmail.com> wrote:

> You can download the Crypto++ 's manual from this link:
> https://www.cryptopp.com/docs/ref/CryptoPPRef.zip.
>
> 在 2018年4月15日星期日 UTC+8上午11:13:31,Trung B. Pham写道:
>
>> Hi,
>>
>> I am very new to this library and trying to use it for a personal project.
>> Can someone please review the below code for correctness?
>>
>> Let's call this program xyz.
>> So,
>> g++ main.cpp -O3 -o xyz -lcryptopp
>>
>> Encrypt file using Threefish1024/CTR and PBKDF2
>> ./xyz -p password -s salt -i filename > filename.enc
>> Decrypt file using Threefish1024/CTR and PBKDF2
>> ./xyz -p password -s salt -i filename.enc > filename.enc.dec
>> Derive key using PBKDF2:
>> ./xyz -h password -s salt
>>
>>
>>
>> #include <assert.h>
>> #include <string.h>
>>
>>
>> #include <fstream>
>> #include <iostream>
>> #include <string>
>> #include <iterator>
>>
>>
>> #include <cryptopp/modes.h>
>> #include <cryptopp/pwdbased.h>
>> #include <cryptopp/sha3.h>
>> #include <cryptopp/threefish.h>
>> #include <cryptopp/hex.h>
>>
>>
>> #define KEY_LENGTH 128U
>> #define CTR_LENGTH 128U
>> #define TWEAK_LENGTH 16U
>>
>> // Read all file into std::vector<char>
>> static std::vector<char> read_file(char const *filename) {
>>     std::ifstream ifs(filename, std::ios::binary | std::ios::ate);
>>     std::ifstream::pos_type pos = ifs.tellg();
>>
>>     std::vector<char> result(pos);
>>
>>
>>     ifs.seekg(0, std::ios::beg);
>>     ifs.read(result.data(), pos);
>>
>>
>>     return result;
>> }
>>
>>
>> int main(int argc, char *argv[]) {
>>
>>     // Encrypt and decrypt file using Threefish1024/CTR and PBKDF2
>>     // Require password, salt, and filename
>>     // Output: stdout
>>     if (argc == 7 && strcmp(argv[1], "-p") == 0 && strcmp(argv[3], "-s")
>> == 0 && strcmp(argv[5], "-i") == 0) {
>>
>>         size_t pwd_len = strlen(argv[2]);
>>         size_t salt_len = strlen(argv[4]);
>>         assert(pwd_len >= 10);
>>         assert(salt_len >= 10);
>>
>>
>>         try {
>>             size_t buf_len = KEY_LENGTH + CTR_LENGTH + TWEAK_LENGTH;
>>             CryptoPP::byte buf[buf_len];
>>             CryptoPP::PKCS5_PBKDF2_HMAC<CryptoPP::SHA3_512> pbkdf2;
>>             pbkdf2.DeriveKey(buf, buf_len, 0, (CryptoPP::byte *)argv[2],
>> pwd_len, (CryptoPP::byte *)argv[4], salt_len, 42);
>>
>>             std::vector<char> plain_text = read_file(argv[6]);
>>
>>             // I am very unsure about ConstByteArrayParameter and
>> SetTweak
>>             // Please advise
>>             CryptoPP::ConstByteArrayParameter tweak(&buf[KEY_LENGTH +
>> CTR_LENGTH], TWEAK_LENGTH, false);
>>             CryptoPP::AlgorithmParameters params = CryptoPP::
>> MakeParameters(CryptoPP::Name::Tweak(), tweak);
>>             CryptoPP::Threefish1024::Encryption t3f(buf, KEY_LENGTH);
>>             t3f.SetTweak(params);
>>             CryptoPP::CTR_Mode_ExternalCipher::Encryption encryptor(t3f,
>> &buf[KEY_LENGTH]);
>>             encryptor.ProcessData((CryptoPP::byte *)plain_text.data(), (
>> CryptoPP::byte *)plain_text.data(), plain_text.size());
>>             std::copy(plain_text.begin(), plain_text.end(), std::
>> ostream_iterator<char>(std::cout, ""));
>>         } catch (CryptoPP::Exception const& ex) {
>>             std::cout << "CryptoPP::Exception caught: " << ex.what() <<
>> std::endl;
>>             exit(-1);
>>         } catch (std::exception const& ex) {
>>             std::cout << "std::exception caught: " << ex.what() << std::
>> endl;
>>             exit(-1);
>>         }
>>
>>     // Derive key using PBKDF2
>>     // Require password and salt
>>     // Output: stdout
>>     } else if (argc == 5 && strcmp(argv[1], "-h") == 0 && strcmp(argv[3],
>> "-s") == 0) {
>>
>>         size_t pwd_len = strlen(argv[2]);
>>         size_t salt_len = strlen(argv[4]);
>>         assert(pwd_len >= 10);
>>         assert(salt_len >= 10);
>>
>>         try {
>>             size_t buf_len = 64;
>>             CryptoPP::byte buf[buf_len];
>>             CryptoPP::PKCS5_PBKDF2_HMAC<CryptoPP::SHA3_512> pbkdf2;
>>             pbkdf2.DeriveKey(buf, buf_len, 0, (CryptoPP::byte *)argv[2],
>> pwd_len, (CryptoPP::byte *)argv[4], salt_len, 42);
>>
>>             std::string encoded;
>>             CryptoPP::StringSource(buf, buf_len, true, new CryptoPP::
>> HexEncoder(new CryptoPP::StringSink(encoded), false));
>>             std::cout << encoded;
>>         } catch (CryptoPP::Exception const& ex) {
>>             std::cout << "CryptoPP::Exception caught: " << ex.what() <<
>> std::endl;
>>             exit(-1);
>>         } catch (std::exception const& ex) {
>>             std::cout << "std::exception caught: " << ex.what() << std::
>> endl;
>>             exit(-1);
>>         }
>>
>>     } else {
>>         printf("Wrong argv\n");
>>         exit(-1);
>>     }
>>
>>
>>     return EXIT_SUCCESS;
>> }
>>
>>
>>
>> --
> You received this message because you are subscribed to "Crypto++ Users".
> More information about Crypto++ and this group is available at
> http://www.cryptopp.com and http://groups.google.com/
> forum/#!forum/cryptopp-users.
> ---
> You received this message because you are subscribed to the Google Groups
> "Crypto++ Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to cryptopp-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to "Crypto++ Users". More 
information about Crypto++ and this group is available at 
http://www.cryptopp.com and 
http://groups.google.com/forum/#!forum/cryptopp-users.
--- 
You received this message because you are subscribed to the Google Groups 
"Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to cryptopp-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to