Question

I'm trying to use Crypto++ but I don't have very much C++ experience. I'm attempting to run performance tests on the encryption and decryption starting with AES. I make install'ed the source, and I am able to include it as shown below, but when I try to compile and run using Xcode it raises 38 linking errors starting with

CryptoPP::AlignedAllocate(unsigned long)", referenced from:
    CryptoPP::AllocatorWithCleanup<unsigned char, true>::allocate(unsigned long,
    void const*) in main.o`

Here is my code:

#include <iostream>
#include <fstream>
#include <sstream>

#include <string>
#include <string.h>

#include <chrono>
#include <unistd.h>

#include </usr/include/cryptopp/aes.h>
#include </usr/include/cryptopp/modes.h>
#include </usr/include/cryptopp/filters.h>

using namespace std;

const int stringlength = 1000;
char *stringdir = "/Users/Noah/Desktop/EncryptionTimer/EncryptionTimer/strings/";

char *read(const char *filepath)
{
    FILE *file = fopen(filepath, "r");

    fseek(file, 0, SEEK_END);
    long int length = ftell(file);
    char *buffer = (char*) malloc(sizeof(char) * length + 1);

    rewind(file);
    fread(buffer, 1, length, file);
    fclose(file);

    buffer[-1] = '\0';
    return buffer;
}

int main(int argc, const char * argv[])
{
    fstream fout("./output.txt");
    for (int fileindex = 1; fileindex <= 1; fileindex++)
    {
        ostringstream convert;
        convert << fileindex;
        string name = convert.str() + ".txt";

        char *filename = new char[name.size() + 1];
        copy(name.begin(), name.end(), filename);
        filename[-1] = '\0';

        char *filepath = new char[sizeof(*stringdir) + sizeof(*filename)];

        strncat(filepath, stringdir, strlen(stringdir));
        strncat(filepath, filename, strlen(filename));

        cout << filepath << "...";
        fout << filepath << "...";

        char *contents = read(filepath);
        string plaintext = string(contents);
        string ciphertext;

        byte key[CryptoPP::AES::DEFAULT_KEYLENGTH], iv[CryptoPP::AES::BLOCKSIZE];
        memset(key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH);
        memset(iv, 0x00, CryptoPP::AES::BLOCKSIZE);

        cout << "ready" << "...";
        fout << "ready" << "...";

        // Encrypt
        auto t_start1 = std::chrono::high_resolution_clock::now();

        for (int i = 0; i < 1000; i++)
        {
            CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
            CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, iv);

            CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink(ciphertext));
            stfEncryptor.Put(reinterpret_cast<const unsigned char*>(plaintext.c_str()), plaintext.length() + 1);
            stfEncryptor.MessageEnd();
        }

        auto t_end1 = std::chrono::high_resolution_clock::now();
        fout << "\nElapsed encrypt time: "
        << std::chrono::duration_cast<std::chrono::microseconds>(t_end1 - t_start1).count()
        << " microseconds\n" << endl;
        cout << "finished..." << endl;


        // Decrypt
        auto t_start2 = std::chrono::high_resolution_clock::now();

        for (int i = 0; i < 1000; i++)
        {
            CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
            CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv );

            CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink(ciphertext));
            stfDecryptor.Put(reinterpret_cast<const unsigned char*>( ciphertext.c_str() ), ciphertext.size());
            stfDecryptor.MessageEnd();
        }

        auto t_end2 = std::chrono::high_resolution_clock::now();
        fout << "\nElapsed decrypt time: "
        << std::chrono::duration_cast<std::chrono::microseconds>(t_end2 - t_start2).count()
        << " microseconds\n" << endl;
        cout << "\nDecryption Finished" << endl;


    }
    fout.close();
}

Any suggestions would be appreciated :D.

Was it helpful?

Solution

when I try to compile and run (using Xcode) it raises 38 linking errors...

You need to add the Crypto++ headers and library to your Xcode project. The images below were taken from the Crypto++ wiki, and presumes Crypto++ is installed in /usr/local/cryptopp.

Add the headers here:

enter image description here

Add the library here:

enter image description here

If you are cross-compiling for iOS (its Mach-O too), the Crypto++ wiki has a couple of pages on the subject:

If you want a pre-built Crypto++ for iOS with ARMv7, ARMv7s, ARM64 and i386 (simulator), see this Github account.


Before you make the library yourself, you should open the makefile (GNUmakefile), and uncomment CXXFLAGS = -fPIC.

Also, because of #include <chrono>, you might need to add CXXFLAGS += -stdc++11. I'm not sure because I don't recall trying to mic C++ 03 and C++ 11.


Crypto++ 5.6.2 and earlier also required -DCRYPTOPP_DISABLE_ASM on Intel platforms because of Clang's integrated assembler and Apple's downlevel linker.

Crypto++ 5.6.3 and above managed to work around most of the issues. Problems that could not be worked around effectively had -DCRYPTOPP_DISABLE_ASM applied to the offending translation unit rather than the entire library.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top