tasos-py / AES-Encryption-Classes

AES encryption in Python, PHP, C#, Java, C++, F#, Ruby, Scala, Node.js
MIT License
145 stars 58 forks source link

What version of CryptoPP are you using? #2

Closed truong2301 closed 4 years ago

truong2301 commented 4 years ago

I tried including the library but it didn't work?

tasos-py commented 4 years ago

I've written and tested the C++ code with CryptoPP v7.0 but it should work with newer versions. Maybe I can help you solve the problem if you share more details.

TR-SLimey commented 4 years ago

Hello. I am also having issues with CryptoPP. I've installed it from the Ubuntu repositories, version libcrypto++-dev (5.6.4-8).

I get the following:

aes_encryption.h:158:58: error: ‘byte’ in namespace ‘CryptoPP’ does not name a type
         CryptoPP::SecByteBlock password, const CryptoPP::byte* salt,
                                                          ^~~~
aes_encryption.h:221:58: error: ‘byte’ in namespace ‘CryptoPP’ does not name a type
     static CryptoPP::SecByteBlock encode(const CryptoPP::byte*, size_t);
                                                          ^~~~
aes_encryption.h:223:58: error: ‘byte’ in namespace ‘CryptoPP’ does not name a type
     static CryptoPP::SecByteBlock decode(const CryptoPP::byte*, size_t, bool);
                                                          ^~~~
aes_encryption.h:226:46: error: ‘byte’ in namespace ‘CryptoPP’ does not name a type
     static void checkEncoded(const CryptoPP::byte*, size_t);
                                              ^~~~
aes_encryption.h:241:27: error: ‘CryptoPP::byte’ has not been declared
     size_t read(CryptoPP::byte* data);
                           ^~~~
aes_encryption.cpp: In member function ‘CryptoPP::SecByteBlock AesEncryption::encrypt(CryptoPP::SecByteBlock, CryptoPP::SecByteBlock)’:
aes_encryption.cpp:35:50: error: invalid user-defined conversion from ‘CryptoPP::SecByteBlock {aka CryptoPP::SecBlock<unsigned char>}’ to ‘const int*’ [-fpermissive]
         this->keys(password, salt, aesKey, macKey);
                                                  ^
In file included from /usr/include/cryptopp/rijndael.h:12:0,
                 from /usr/include/cryptopp/aes.h:9,
                 from aes_encryption.h:10,
                 from aes_encryption.cpp:1:
/usr/include/cryptopp/secblock.h:488:2: note: candidate is: CryptoPP::SecBlock<T, A>::operator void*() [with T = unsigned char; A = CryptoPP::AllocatorWithCleanup<unsigned char>] <near match>
  operator void *()
  ^~~~~~~~
/usr/include/cryptopp/secblock.h:488:2: note:   no known conversion from ‘void*’ to ‘const int*’
In file included from aes_encryption.cpp:1:0:
aes_encryption.h:157:10: note:   initializing argument 2 of ‘void AesEncryption::keys(CryptoPP::SecByteBlock, const int*, CryptoPP::SecByteBlock&, CryptoPP::SecByteBlock&)’
     void keys(
          ^~~~
aes_encryption.cpp:48:74: error: no matching function for call to ‘Base64::encode(CryptoPP::AllocatorWithCleanup<unsigned char>::pointer, CryptoPP::SecBlock<unsigned char>::size_type)’
             encrypted = Base64::encode(encrypted.data(), encrypted.size());
                                                                          ^
In file included from aes_encryption.cpp:1:0:
aes_encryption.h:221:35: note: candidate: static CryptoPP::SecByteBlock Base64::encode(const int*, size_t)
     static CryptoPP::SecByteBlock encode(const CryptoPP::byte*, size_t);
                                   ^~~~~~
aes_encryption.h:221:35: note:   no known conversion for argument 1 from ‘CryptoPP::AllocatorWithCleanup<unsigned char>::pointer {aka unsigned char*}’ to ‘const int*’
aes_encryption.cpp: In member function ‘CryptoPP::SecByteBlock AesEncryption::decrypt(CryptoPP::SecByteBlock, CryptoPP::SecByteBlock)’:
aes_encryption.cpp:96:65: error: no matching function for call to ‘Base64::decode(CryptoPP::AllocatorWithCleanup<unsigned char>::pointer, CryptoPP::SecBlock<unsigned char>::size_type, bool)’
             data = Base64::decode(data.data(), data.size(), true);
                                                                 ^
In file included from aes_encryption.cpp:1:0:
aes_encryption.h:223:35: note: candidate: static CryptoPP::SecByteBlock Base64::decode(const int*, size_t, bool)
     static CryptoPP::SecByteBlock decode(const CryptoPP::byte*, size_t, bool);
                                   ^~~~~~
aes_encryption.h:223:35: note:   no known conversion for argument 1 from ‘CryptoPP::AllocatorWithCleanup<unsigned char>::pointer {aka unsigned char*}’ to ‘const int*’
aes_encryption.cpp:110:50: error: invalid user-defined conversion from ‘CryptoPP::SecByteBlock {aka CryptoPP::SecBlock<unsigned char>}’ to ‘const int*’ [-fpermissive]
         this->keys(password, salt, aesKey, macKey);
                                                  ^
In file included from /usr/include/cryptopp/rijndael.h:12:0,
                 from /usr/include/cryptopp/aes.h:9,
                 from aes_encryption.h:10,
                 from aes_encryption.cpp:1:
/usr/include/cryptopp/secblock.h:488:2: note: candidate is: CryptoPP::SecBlock<T, A>::operator void*() [with T = unsigned char; A = CryptoPP::AllocatorWithCleanup<unsigned char>] <near match>
  operator void *()
  ^~~~~~~~
/usr/include/cryptopp/secblock.h:488:2: note:   no known conversion from ‘void*’ to ‘const int*’
In file included from aes_encryption.cpp:1:0:
aes_encryption.h:157:10: note:   initializing argument 2 of ‘void AesEncryption::keys(CryptoPP::SecByteBlock, const int*, CryptoPP::SecByteBlock&, CryptoPP::SecByteBlock&)’
     void keys(
          ^~~~
aes_encryption.cpp: In member function ‘std::__cxx11::string AesEncryption::encryptFile(std::__cxx11::string, CryptoPP::SecByteBlock)’:
aes_encryption.cpp:168:50: error: invalid user-defined conversion from ‘CryptoPP::SecByteBlock {aka CryptoPP::SecBlock<unsigned char>}’ to ‘const int*’ [-fpermissive]
         this->keys(password, salt, aesKey, macKey);
                                                  ^
In file included from /usr/include/cryptopp/rijndael.h:12:0,
                 from /usr/include/cryptopp/aes.h:9,
                 from aes_encryption.h:10,
                 from aes_encryption.cpp:1:
/usr/include/cryptopp/secblock.h:488:2: note: candidate is: CryptoPP::SecBlock<T, A>::operator void*() [with T = unsigned char; A = CryptoPP::AllocatorWithCleanup<unsigned char>] <near match>
  operator void *()
  ^~~~~~~~
/usr/include/cryptopp/secblock.h:488:2: note:   no known conversion from ‘void*’ to ‘const int*’
In file included from aes_encryption.cpp:1:0:
aes_encryption.h:157:10: note:   initializing argument 2 of ‘void AesEncryption::keys(CryptoPP::SecByteBlock, const int*, CryptoPP::SecByteBlock&, CryptoPP::SecByteBlock&)’
     void keys(
          ^~~~
aes_encryption.cpp:180:19: error: ‘byte’ is not a member of ‘CryptoPP’
         CryptoPP::byte chunk[FileChunks::chunkSize];
                   ^~~~
aes_encryption.cpp:180:19: note: suggested alternative:
In file included from /usr/include/cryptopp/seckey.h:9:0,
                 from /usr/include/cryptopp/rijndael.h:11,
                 from /usr/include/cryptopp/aes.h:9,
                 from aes_encryption.h:10,
                 from aes_encryption.cpp:1:
/usr/include/cryptopp/config.h:203:23: note:   ‘byte’
 typedef unsigned char byte;  // put in global namespace to avoid ambiguity with other byte typedefs
                       ^~~~
aes_encryption.cpp:187:41: error: ‘chunk’ was not declared in this scope
             size_t chunkSize = ifs.read(chunk);
                                         ^~~~~
aes_encryption.cpp:204:19: error: ‘byte’ is not a member of ‘CryptoPP’
         CryptoPP::byte mac[macLen];
                   ^~~~
aes_encryption.cpp:204:19: note: suggested alternative:
In file included from /usr/include/cryptopp/seckey.h:9:0,
                 from /usr/include/cryptopp/rijndael.h:11,
                 from /usr/include/cryptopp/aes.h:9,
                 from aes_encryption.h:10,
                 from aes_encryption.cpp:1:
/usr/include/cryptopp/config.h:203:23: note:   ‘byte’
 typedef unsigned char byte;  // put in global namespace to avoid ambiguity with other byte typedefs
                       ^~~~
aes_encryption.cpp:205:20: error: ‘mac’ was not declared in this scope
         hmac.Final(mac);
                    ^~~
aes_encryption.cpp:205:20: note: suggested alternative: ‘hmac’
         hmac.Final(mac);
                    ^~~
                    hmac
aes_encryption.cpp: In member function ‘std::__cxx11::string AesEncryption::decryptFile(std::__cxx11::string, CryptoPP::SecByteBlock)’:
aes_encryption.cpp:252:50: error: invalid user-defined conversion from ‘CryptoPP::SecByteBlock {aka CryptoPP::SecBlock<unsigned char>}’ to ‘const int*’ [-fpermissive]
         this->keys(password, salt, aesKey, macKey);
                                                  ^
In file included from /usr/include/cryptopp/rijndael.h:12:0,
                 from /usr/include/cryptopp/aes.h:9,
                 from aes_encryption.h:10,
                 from aes_encryption.cpp:1:
/usr/include/cryptopp/secblock.h:488:2: note: candidate is: CryptoPP::SecBlock<T, A>::operator void*() [with T = unsigned char; A = CryptoPP::AllocatorWithCleanup<unsigned char>] <near match>
  operator void *()
  ^~~~~~~~
/usr/include/cryptopp/secblock.h:488:2: note:   no known conversion from ‘void*’ to ‘const int*’
In file included from aes_encryption.cpp:1:0:
aes_encryption.h:157:10: note:   initializing argument 2 of ‘void AesEncryption::keys(CryptoPP::SecByteBlock, const int*, CryptoPP::SecByteBlock&, CryptoPP::SecByteBlock&)’
     void keys(
          ^~~~
aes_encryption.cpp:259:19: error: ‘byte’ is not a member of ‘CryptoPP’
         CryptoPP::byte chunk[FileChunks::chunkSize];
                   ^~~~
aes_encryption.cpp:259:19: note: suggested alternative:
In file included from /usr/include/cryptopp/seckey.h:9:0,
                 from /usr/include/cryptopp/rijndael.h:11,
                 from /usr/include/cryptopp/aes.h:9,
                 from aes_encryption.h:10,
                 from aes_encryption.cpp:1:
/usr/include/cryptopp/config.h:203:23: note:   ‘byte’
 typedef unsigned char byte;  // put in global namespace to avoid ambiguity with other byte typedefs
                       ^~~~
aes_encryption.cpp:267:40: error: ‘chunk’ was not declared in this scope
             size_t chunkSize = fc.read(chunk);
                                        ^~~~~
aes_encryption.cpp: In member function ‘void AesEncryption::setMasterKey(CryptoPP::SecByteBlock, bool)’:
aes_encryption.cpp:306:62: error: no matching function for call to ‘Base64::decode(CryptoPP::AllocatorWithCleanup<unsigned char>::pointer, CryptoPP::SecBlock<unsigned char>::size_type, bool)’
             key = Base64::decode(key.data(), key.size(), true);
                                                              ^
In file included from aes_encryption.cpp:1:0:
aes_encryption.h:223:35: note: candidate: static CryptoPP::SecByteBlock Base64::decode(const int*, size_t, bool)
     static CryptoPP::SecByteBlock decode(const CryptoPP::byte*, size_t, bool);
                                   ^~~~~~
aes_encryption.h:223:35: note:   no known conversion for argument 1 from ‘CryptoPP::AllocatorWithCleanup<unsigned char>::pointer {aka unsigned char*}’ to ‘const int*’
aes_encryption.cpp: In member function ‘CryptoPP::SecByteBlock AesEncryption::getMasterKey(bool)’:
aes_encryption.cpp:327:77: error: no matching function for call to ‘Base64::encode(CryptoPP::AllocatorWithCleanup<unsigned char>::pointer, CryptoPP::SecBlock<unsigned char>::size_type)’
         return Base64::encode(this->masterKey.data(), this->masterKey.size());
                                                                             ^
In file included from aes_encryption.cpp:1:0:
aes_encryption.h:221:35: note: candidate: static CryptoPP::SecByteBlock Base64::encode(const int*, size_t)
     static CryptoPP::SecByteBlock encode(const CryptoPP::byte*, size_t);
                                   ^~~~~~
aes_encryption.h:221:35: note:   no known conversion for argument 1 from ‘CryptoPP::AllocatorWithCleanup<unsigned char>::pointer {aka unsigned char*}’ to ‘const int*’
aes_encryption.cpp: In member function ‘CryptoPP::SecByteBlock AesEncryption::randomKeyGen(size_t, bool)’:
aes_encryption.cpp:336:77: error: no matching function for call to ‘Base64::encode(CryptoPP::AllocatorWithCleanup<unsigned char>::pointer, CryptoPP::SecBlock<unsigned char>::size_type)’
         return Base64::encode(this->masterKey.data(), this->masterKey.size());
                                                                             ^
In file included from aes_encryption.cpp:1:0:
aes_encryption.h:221:35: note: candidate: static CryptoPP::SecByteBlock Base64::encode(const int*, size_t)
     static CryptoPP::SecByteBlock encode(const CryptoPP::byte*, size_t);
                                   ^~~~~~
aes_encryption.h:221:35: note:   no known conversion for argument 1 from ‘CryptoPP::AllocatorWithCleanup<unsigned char>::pointer {aka unsigned char*}’ to ‘const int*’
aes_encryption.cpp: At global scope:
aes_encryption.cpp:341:75: error: ‘byte’ in namespace ‘CryptoPP’ does not name a type
 esEncryption::keys(CryptoPP::SecByteBlock password, const CryptoPP::byte* salt,
                                                                     ^~~~
aes_encryption.cpp: In member function ‘void AesEncryption::keys(CryptoPP::SecByteBlock, const int*, CryptoPP::SecByteBlock&, CryptoPP::SecByteBlock&)’:
aes_encryption.cpp:350:9: error: no matching function for call to ‘CryptoPP::PKCS5_PBKDF2_HMAC<CryptoPP::SHA512>::DeriveKey(CryptoPP::AllocatorWithCleanup<unsigned char>::pointer, CryptoPP::SecBlock<unsigned char>::size_type, int, CryptoPP::SecByteBlock&, CryptoPP::SecBlock<unsigned char>::size_type, const int*&, const int&, unsigned int&)’
         );
         ^
In file included from aes_encryption.cpp:7:0:
/usr/include/cryptopp/pwdbased.h:113:14: note: candidate: unsigned int CryptoPP::PKCS5_PBKDF2_HMAC<T>::DeriveKey(byte*, size_t, byte, const byte*, size_t, const byte*, size_t, unsigned int, double) const [with T = CryptoPP::SHA512; byte = unsigned char; size_t = long unsigned int]
 unsigned int PKCS5_PBKDF2_HMAC<T>::DeriveKey(byte *derived, size_t derivedLen, byte purpose, const byte *password, size_t passwordLen, const byte *salt, size_t saltLen, unsigned int iterations, double timeInSeconds) const
              ^~~~~~~~~~~~~~~~~~~~
/usr/include/cryptopp/pwdbased.h:113:14: note:   no known conversion for argument 6 from ‘const int*’ to ‘const byte* {aka const unsigned char*}’
aes_encryption.cpp:357:9: error: no matching function for call to ‘CryptoPP::HKDF<CryptoPP::SHA256>::DeriveKey(CryptoPP::AllocatorWithCleanup<unsigned char>::pointer, CryptoPP::SecBlock<unsigned char>::size_type, CryptoPP::SecByteBlock&, CryptoPP::SecBlock<unsigned char>::size_type, const int*&, const int&, NULL, int)’
         );
         ^
In file included from aes_encryption.cpp:9:0:
/usr/include/cryptopp/hkdf.h:60:14: note: candidate: unsigned int CryptoPP::HKDF<T>::DeriveKey(byte*, size_t, const byte*, size_t, const byte*, size_t, const byte*, size_t) const [with T = CryptoPP::SHA256; byte = unsigned char; size_t = long unsigned int]
 unsigned int HKDF<T>::DeriveKey(byte *derived, size_t derivedLen, const byte *secret, size_t secretLen, const byte *salt, size_t saltLen, const byte* info, size_t infoLen) const
              ^~~~~~~
/usr/include/cryptopp/hkdf.h:60:14: note:   no known conversion for argument 5 from ‘const int*’ to ‘const byte* {aka const unsigned char*}’
aes_encryption.cpp: In member function ‘CryptoPP::SecByteBlock AesEncryption::signFile(std::__cxx11::string, CryptoPP::SecByteBlock)’:
aes_encryption.cpp:410:15: error: ‘byte’ is not a member of ‘CryptoPP’
     CryptoPP::byte chunk[FileChunks::chunkSize];
               ^~~~
aes_encryption.cpp:410:15: note: suggested alternative:
In file included from /usr/include/cryptopp/seckey.h:9:0,
                 from /usr/include/cryptopp/rijndael.h:11,
                 from /usr/include/cryptopp/aes.h:9,
                 from aes_encryption.h:10,
                 from aes_encryption.cpp:1:
/usr/include/cryptopp/config.h:203:23: note:   ‘byte’
 typedef unsigned char byte;  // put in global namespace to avoid ambiguity with other byte typedefs
                       ^~~~
aes_encryption.cpp:413:36: error: ‘chunk’ was not declared in this scope
         size_t chunkSize = fc.read(chunk);
                                    ^~~~~
aes_encryption.cpp:416:15: error: ‘byte’ is not a member of ‘CryptoPP’
     CryptoPP::byte mac[macLen];
               ^~~~
aes_encryption.cpp:416:15: note: suggested alternative:
In file included from /usr/include/cryptopp/seckey.h:9:0,
                 from /usr/include/cryptopp/rijndael.h:11,
                 from /usr/include/cryptopp/aes.h:9,
                 from aes_encryption.h:10,
                 from aes_encryption.cpp:1:
/usr/include/cryptopp/config.h:203:23: note:   ‘byte’
 typedef unsigned char byte;  // put in global namespace to avoid ambiguity with other byte typedefs
                       ^~~~
aes_encryption.cpp:417:16: error: ‘mac’ was not declared in this scope
     hmac.Final(mac);
                ^~~
aes_encryption.cpp:417:16: note: suggested alternative: ‘hmac’
     hmac.Final(mac);
                ^~~
                hmac
aes_encryption.cpp: In member function ‘void AesEncryption::verifyFile(std::__cxx11::string, CryptoPP::SecByteBlock, CryptoPP::SecByteBlock)’:
aes_encryption.cpp:426:15: error: ‘byte’ is not a member of ‘CryptoPP’
     CryptoPP::byte chunk[FileChunks::chunkSize];
               ^~~~
aes_encryption.cpp:426:15: note: suggested alternative:
In file included from /usr/include/cryptopp/seckey.h:9:0,
                 from /usr/include/cryptopp/rijndael.h:11,
                 from /usr/include/cryptopp/aes.h:9,
                 from aes_encryption.h:10,
                 from aes_encryption.cpp:1:
/usr/include/cryptopp/config.h:203:23: note:   ‘byte’
 typedef unsigned char byte;  // put in global namespace to avoid ambiguity with other byte typedefs
                       ^~~~
aes_encryption.cpp:429:36: error: ‘chunk’ was not declared in this scope
         size_t chunkSize = fc.read(chunk);
                                    ^~~~~
aes_encryption.cpp: At global scope:
aes_encryption.cpp:499:55: error: ‘byte’ in namespace ‘CryptoPP’ does not name a type
 CryptoPP::SecByteBlock Base64::encode(const CryptoPP::byte* data, size_t size)
                                                       ^~~~
aes_encryption.cpp: In static member function ‘static CryptoPP::SecByteBlock Base64::encode(const int*, size_t)’:
aes_encryption.cpp:502:27: error: invalid conversion from ‘const int*’ to ‘byte {aka unsigned char}’ [-fpermissive]
     encoder.Put(data, size);
                           ^
In file included from /usr/include/cryptopp/seckey.h:16:0,
                 from /usr/include/cryptopp/rijndael.h:11,
                 from /usr/include/cryptopp/aes.h:9,
                 from aes_encryption.h:10,
                 from aes_encryption.cpp:1:
/usr/include/cryptopp/cryptlib.h:1378:10: note:   initializing argument 1 of ‘size_t CryptoPP::BufferedTransformation::Put(byte, bool)’
   size_t Put(byte inByte, bool blocking=true)
          ^~~
aes_encryption.cpp: At global scope:
aes_encryption.cpp:510:55: error: ‘byte’ in namespace ‘CryptoPP’ does not name a type
 CryptoPP::SecByteBlock Base64::decode(const CryptoPP::byte* data, size_t size, bool check)
                                                       ^~~~
aes_encryption.cpp: In static member function ‘static CryptoPP::SecByteBlock Base64::decode(const int*, size_t, bool)’:
aes_encryption.cpp:516:27: error: invalid conversion from ‘const int*’ to ‘byte {aka unsigned char}’ [-fpermissive]
     decoder.Put(data, size);
                           ^
In file included from /usr/include/cryptopp/seckey.h:16:0,
                 from /usr/include/cryptopp/rijndael.h:11,
                 from /usr/include/cryptopp/aes.h:9,
                 from aes_encryption.h:10,
                 from aes_encryption.cpp:1:
/usr/include/cryptopp/cryptlib.h:1378:10: note:   initializing argument 1 of ‘size_t CryptoPP::BufferedTransformation::Put(byte, bool)’
   size_t Put(byte inByte, bool blocking=true)
          ^~~
aes_encryption.cpp: At global scope:
aes_encryption.cpp:524:43: error: ‘byte’ in namespace ‘CryptoPP’ does not name a type
 void Base64::checkEncoded(const CryptoPP::byte* data, size_t size)
                                           ^~~~
aes_encryption.cpp:551:35: error: ‘size_t FileChunks::read’ is not a static data member of ‘class FileChunks’
 size_t FileChunks::read(CryptoPP::byte* data)
                                   ^~~~
aes_encryption.cpp:551:35: error: ‘byte’ is not a member of ‘CryptoPP’
aes_encryption.cpp:551:35: note: suggested alternative:
In file included from /usr/include/cryptopp/seckey.h:9:0,
                 from /usr/include/cryptopp/rijndael.h:11,
                 from /usr/include/cryptopp/aes.h:9,
                 from aes_encryption.h:10,
                 from aes_encryption.cpp:1:
/usr/include/cryptopp/config.h:203:23: note:   ‘byte’
 typedef unsigned char byte;  // put in global namespace to avoid ambiguity with other byte typedefs
                       ^~~~
aes_encryption.cpp:551:41: error: ‘data’ was not declared in this scope
 size_t FileChunks::read(CryptoPP::byte* data)
                                         ^~~~

Is this simply due to the old version of the library?

Also, I am quite new to C++ so would you mind giving me an example of how to correctly compile a .cpp file which uses your AES class? If it's relevant, I'm using Ubuntu 18.04.

tasos-py commented 4 years ago

I'm getting the same error with CryptoPP v5.6.4. I don't know what the problem is, but you could solve it if you update CryptoPP to version 7 or higher (I have tested v7.0.0 and v8.2.0, so I assume this problem doesn't occur in any version >=v7.0.0).

How to link aes_encryption_cpp to your cpp file:
a) download and build CryptoPP - see https://www.cryptopp.com/wiki/Linux_(Command_Line)
b) download aes_encryption_cpp and build a simple static library (let's call it libaec.a) -

g++ -Wall -I/path/to/cryptopp/headers -L/path/to/aes_encryption_cpp/lib -c /path/to/aes_encryption_cpp/aes_encryption.cpp -l:libcryptopp.a
ar rvs libaec.a aes_encryption.o

c) link the required headers and libraries. For example:

g++ -Wall -I/path/to/aes_encryption_cpp/headers -I/path/to/cryptopp/headers -L/path/to/aes_encryption_cpp/lib -L/path/to/cryptopp/lib myprogram.cpp -o myprogram -l:libaec.a -l:libcryptopp.a

Or you could just use the aes_encryption.cpp file, without building a library:

g++ -Wall -I/path/to/aes_encryption_cpp/headers -I/path/to/cryptopp/headers -L/path/to/cryptopp/lib /path/to/aes_encryption_cpp/aes_encryption.cpp myprogram.cpp -o myprogram -l:libcryptopp.a
TR-SLimey commented 4 years ago

Still a little confused.

So I downloaded CryptoPP from here (v8.2) and extracted its contents to ./lib/cryptopp where . is my project directory.

The files from aes_encryption_cpp are in ./lib/aes.

Currently, I am using the following Makefile to compile my project:

all: main.cpp

    # -Wall       | Show all warnings
    # -static     | Compile a static binary to use exactly the libraries we used for compilation
    # -s          | Remove debugging symbols (smaller final file size)
    # -Os         | Compile optimizing for file size
    # -std=c++11  | Compile for the C++11 standard
    g++ -Wall -static -s -Os -std=c++11 main.cpp -o main

clean:
    rm main

I know I'm literally asking to be walked through this like a baby but would you mind showing what edits I need to make to my Makefile?

tasos-py commented 4 years ago

I don't have any experience in creating Makefiles, but I think all you have to do is change your g++ command to match the last one in my previous post.

The -I argument sets the path of headers. We need that for the aes_encryption.hpp file and for any CryptoPP headers that are included in my code. Because in my includes I use the cryptopp folder, you should use the path of cryptppp, not the path of cryptopp headers (for example -I ./lib and not -I ./lib/cryptopp).

The -L argument sets the path of libraries. This tells the compiler to look in that path for libraries.

The -l argument links a library - libcryptopp.a in this case.

# Full path of project
project_path = /home/my-username/my-project

all: main.cpp

    # -Wall         | Show all warnings
    # -static       | Compile a static binary to use exactly the libraries we used for compilation
    # -s            | Remove debugging symbols (smaller final file size)
    # -Os           | Compile optimizing for file size
    # -std=c++11    | Compile for the C++11 standard
    # -I PATH       | Path of headers
    # -L PATH       | Path of libraries
    # -l:FILENAME   | File name of ibrary

    g++ -Wall -static -s -Os -std=c++11 \
    -I $(project_path)/lib/aes -I $(project_path)/lib \
    -L $(project_path)/lib/cryptopp \
    $(project_path)/lib/aes/aes_encryption.cpp \
    main.cpp -o main \
    -l:libcryptopp.a

clean:
    rm main

This should produce an executable if you just change project_path to the path of your project. The -I and -L arguments accept relative paths, but I think it's best to use absolute paths.

TR-SLimey commented 4 years ago

Sooo, I've spent about an hour figuring out why my project was still not compiling only to realize the compiler was using my apt installed cryptopp. After that, I could compile an empty project successfully. However, when compiling your example code I got some has not been declared and was not declared in this scope errors. I'm assuming that's just due to a missing #include? Any suggestions? I have tried <aes> but that does not work.

tasos-py commented 4 years ago

Yes, you should include aes_encryption.hpp in main.cpp. If you have set the path of headers as described in my previous comment (the -I $(project_path)/lib/aes parameter), you can include it with this statement: #include "aes_encryption.hpp". If not you should use the absolute or relative path in your include statement (eg #include "lib/aes/aes_encryption.hpp").

A simple example,

#include <iostream>
#include <string>
#include "aes_encryption.hpp"

int main(int, char**) {
    AesEncryption aes;
    CryptoPP::SecByteBlock ct = aes.encrypt("my data", "my password");
    std::cout << std::string(ct.begin(), ct.end()) << "\n";
}
TR-SLimey commented 4 years ago

That's weird. Whenever I add the #include suddenly I get:

/usr/bin/ld: cannot find -l:libcryptopp.a
/usr/bin/ld: cannot find -lPocoNet
/usr/bin/ld: cannot find -lPocoUtil
/usr/bin/ld: cannot find -lPocoFoundation

Note: I'm also using POCO

Edit: actually, the only reason why the error does not show up without the include is because the compilation never gets to it. So the linker simply cannot find these.

Edit2: So I've created a brand new project folder with exactly the Makefile and contents as suggested. It fails to compile with the error: -l:libcryptopp.a. I attach the project folder so the issue can be reproduced: test.zip

tasos-py commented 4 years ago

It seems that you haven't built libcryptopp.a. If so, simply cd to the lib/cryptopp directory and run make. You will find more info on this topic in the link I shared in a previous comment.

TR-SLimey commented 4 years ago

Ah. I feel like an idiot now. I have compiled it before but then I've replaced all the files with v7.0.0 to make sure it wasn't a version issue and I didn't re-compile.

The project builds just fine now, thanks for your help and patience :smile:

tasos-py commented 4 years ago

Awesome! I will close this issue in a couple of days and probably delete our last posts as they're somewhat off topic. First I want to see if I can make my code work with older versions of CryptoPP, and I hope I can do this by replacing CryptoPP::byte with unsigned char. I think byte is just a typedef of unsigned char, but in cryptopp v5 it's defined in global namespace, while in v7 and 8 it's defined in the CryptoPP namespace.

TR-SLimey commented 4 years ago

I've read something like that when researching the error message I got before. Specifically:

This: https://www.linuxquestions.org/questions/programming-9/c-and-cryptopp-byte-type-857794/ And this: https://www.cryptopp.com/wiki/Std::byte

That would be great if you got that working actually since it would be a lot easier to just use apt than compiling from source, especially for C++ noobs.

But yeah, this probably answered OP's question too.

tasos-py commented 4 years ago

Thanks for the sources. It's now obvious that this is a namespace issue and since byte is the same as unsigned char, replacing them souldn't cause any security, performance or other types of problems. I have already run some tests with G++ and it seems the issue is fixed. I'll update the repo after I test it with the Visual C++ compiler - probably tomorrow.

tasos-py commented 4 years ago

I updated the code, now you should be able to compile it with Cryptopp v5.6.4. I will keep this issue open for a couple of days, in case you come across any problems.

TR-SLimey commented 4 years ago

Great! Will test immediately

TR-SLimey commented 4 years ago

Compiles and runs flawlessly using the apt version of the library. Well done! This makes everything so much easier.

tasos-py commented 4 years ago

Thanks for helping me identifying and solving this problem @TR-SLimey. I think I can close this thread now.

TR-SLimey commented 4 years ago

Yep! And, no problem. Thanks for putting up with my noob questions :smile: