使用Crypto ++时Apple Mach-O链接错误

时间:2014-03-08 21:41:37

标签: c++ encryption crypto++

我正在尝试使用Crypto ++,但我没有很多C ++经验。我正在尝试从AES开始对加密和解密进行性能测试。我make install'编辑了源代码,我能够将其包含在内,如下所示,但是当我尝试使用Xcode进行编译和运行时,它会引发38个以

开头的链接错误
CryptoPP::AlignedAllocate(unsigned long)", referenced from:
    CryptoPP::AllocatorWithCleanup<unsigned char, true>::allocate(unsigned long,
    void const*) in main.o`

这是我的代码:

#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();
}

任何建议都将受到赞赏:D。

1 个答案:

答案 0 :(得分:0)

  

当我尝试编译并运行(使用Xcode)时,它会引发38个链接错误...

您需要将Crypto ++标头和库添加到Xcode项目中。下面的图片来自Crypto++ wiki,并假设Crypto ++安装在/usr/local/cryptopp

在此处添加标题:

enter image description here

在此处添加库:

enter image description here

如果您正在为iOS(它的Mach-O)进行交叉编译,那么Crypto ++ wiki有几页关于这个主题:

如果您想要使用ARMv7,ARMv7s,ARM64和i386(模拟器)预先构建的适用于iOS的Crypto ++,请参阅此Github account


在您自己创建库之前,您应该打开makefile(GNUmakefile),然后取消注释CXXFLAGS = -fPIC

此外,由于#include <chrono>,您可能需要添加CXXFLAGS += -stdc++11。我不确定,因为我不记得尝试使用mic C ++ 03和C ++ 11。


Crypto ++ 5.6.2及更早版本在英特尔平台上也需要-DCRYPTOPP_DISABLE_ASM,因为Clang的集成汇编程序和Apple的下层链接器。

Crypto ++ 5.6.3及更高版本设法解决了大多数问题。无法有效解决的问题-DCRYPTOPP_DISABLE_ASM适用于违规的翻译单位而不是整个图书馆。