c++ - 使用 AES 使用 Common Crypto 和 Crypto++ 加密时的不同结果

标签 c++ objective-c encryption crypto++ commoncrypto

使用 Apple 的 Common Crypto 和 Crypto++ 使用相同的 key 加密相同的文件(二进制数据)时,我得到了不同的结果。我使用的算法是 AES。

这是使用 Common Crypto 的 Objective C 代码:

void FileUtil::writeToFileEncrypt(string fileName, const void *data, int size, string key, int *sizeOut)
{
    int numBytesEncrypted = 0;
    char keyPtr[kCCKeySizeAES256+1];

    if (key.length() > 32)
    {
        key = key.substr(0, 32);
    }

    memcpy(keyPtr, key.c_str(), sizeof(keyPtr));

    if (key.length() < 32)
    {
        for (int i = key.length(); i < 32; i++)
        {
            keyPtr[i] = '0';
        }
    }

    size_t bufferSize = size + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);

    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                      keyPtr, kCCKeySizeAES256,
                                      NULL /* initialization vector (optional) */,
                                      data, size, /* input */
                                      buffer, bufferSize, /* output */
                                      &numBytesEncrypted);

    if (cryptStatus == kCCSuccess) {
        cout << "encrypt success" << endl;
    }

    ofstream myfile;
    myfile.open (fileName.c_str(), ios::out | ios::binary);
    myfile.write((const char *)buffer, numBytesEncrypted);
    myfile.close();

    free(buffer);

    *sizeOut = numBytesEncrypted;
}

这是使用 Crypto++ 的 C++ 代码

void EncryptUtils::encrypt(string fileName, unsigned char *chars, string key, int *length, int dataLength)
{
    unsigned char *iv = (unsigned char *)"0000000000000000";

    char keyPtr[32 + 1];

    if (key.length() > 32)
    {
        key = key.substr(0, 32);
    }

    memcpy(keyPtr, key.c_str(), sizeof(keyPtr));

    if (key.length() < 32)
    {
        for (int i = key.length(); i < 32; i++)
        {
            keyPtr[i] = '0';
        }
    }
    keyPtr[32] = '\0';

    CryptoPP::AES::Encryption aesEncryption((unsigned char *)keyPtr, 32);
    CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, iv);

    int newBufSize = (sizeof(unsigned char *) * dataLength) + 32;
    unsigned char *newBuf = (unsigned char *)malloc(newBufSize);
    CryptoPP::ArraySink *arraySink = new CryptoPP::ArraySink(newBuf, newBufSize);

    CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, arraySink, CryptoPP::StreamTransformationFilter::PKCS_PADDING);
    stfEncryptor.Put(reinterpret_cast<const unsigned char*>(chars), (unsigned int)dataLength);
    stfEncryptor.MessageEnd();

    *length = arraySink->TotalPutLength();

    ofstream myfile;
    myfile.open (fileName.c_str(), ios::out | ios::binary);
    myfile.write((const char *)newBuf, *length);
    myfile.close();
}

我需要让它们都产生相同的结果。有什么我忽略的吗?

最佳答案

  1. “Objective-C”版本不是用 Objective-C 编写的,而是用 C++ 编写的。实际加密使用的是 CCCrypt,它是普通的“C”。

  2. “Objective-C”版本没有提供 iv,因此它默认为全零。 C++ 版本提供了 ASCII“0”字符的 iv,这与全零数据不同。这可能是错误。

  3. 在加密调用之前和之后立即为每个包括 key 、iv、数据输入和数据输出提供输入和输出十六进制数据转储。

关于c++ - 使用 AES 使用 Common Crypto 和 Crypto++ 加密时的不同结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28233023/

相关文章:

c++ - 在 WinAPI、POSIX 或 API-OS 的其他扩展中是否存在 C++11 的所有级别的内存屏障?

c++ - 无法转换此指针

ios - 在 iOS 中使用正向地理编码获取多个地址的纬度和经度时如何添加延迟?

android - 在 Cordova/Phonegap 应用程序中保护 Assets

php - Centos 7、Citadel 电子邮件、服务器、phpmailer 和 S/MIME

c++ - 监控正在运行的 qprocess 并在 qprocess 完成时返回值

c++ - 使用opencv在受控环境中进行视频跟踪的跟踪标记和跟踪算法选择

iOS :GMail API - Send Attachments with email

ios - Core Data 根据关系是否存在对描述符进行排序?

java - Android AES 加密从 C# 到 Java