3

RSA 暗号化と復号化に C++ ライブラリ PolarSSL を使用しています。しかし、暗号化からの出力でない限り、暗号化された文字列を解読することはできません。次のコードは機能しません (リファクタリングされていません)。テキストを暗号化し、出力を Base64 にエンコードして戻します。strcmp の条件が機能します (文字列は同じです)。

AsymetricCipher::encrypt(const std::string &pathToPublicKey, std::istream &inputData, std::ostream &encryptedData) {
    if(initServerPublicCtx(pathToPublicKey, 512)) {
        std::cout << "Encryption error: Can't load public key from file: " << pathToPublicKey << std::endl;
        return false;
    }

    entropy_context entropy;
    ctr_drbg_context ctr_drbg;
    char *pers = "rsa_encrypt";

    entropy_init(&entropy);
    if(ctr_drbg_init(&ctr_drbg, entropy_func, &entropy, (unsigned char*)pers, strlen(pers)) != 0) {
        std::cout << "Encryption error: ctr_drbg_init failed" << std::endl;
        return false;
    }

    size_t inputSize = ::getStreamSize(inputData);
    char *input = new char[inputSize];
    memset(input, 0, inputSize);
    inputData.read(input, inputSize);
    input[inputSize] = '\0';

    unsigned char *buffer = new unsigned char[ctx.len];
    memset(buffer, 0, ctx.len);
    memcpy(buffer, input, inputSize);

    // This has to be rewritten
    size_t MAX_OUTPUT_LENGTH = ctx.len;
    unsigned char *outputBuffer = new unsigned char[MAX_OUTPUT_LENGTH];
    memset(outputBuffer, 0, MAX_OUTPUT_LENGTH);

    if(rsa_pkcs1_encrypt(&ctx, ctr_drbg_random, &ctr_drbg, RSA_PUBLIC, inputSize, buffer, outputBuffer) != 0) {
        std::cout << "Encryption error: rsa_pkcs1_encrypt failed" << std::endl;
        return false;
    }

    initServerPrivateCtx("data/private.key", 512);
    size_t outputSize = 0;

    std::string copyBuffer = "";

    std::stringstream encStream;

    std::string base64 = "";

    Base64Wrapper::encode(outputBuffer, strlen((char*)outputBuffer), base64);


    for(size_t i = 0; i < base64.length();) {
        encStream << base64[i++];
    }

    unsigned char *encBuffer = new unsigned char[MAX_OUTPUT_LENGTH+10];
    memset(encBuffer, 0, MAX_OUTPUT_LENGTH);
    encStream.read((char *)encBuffer, MAX_OUTPUT_LENGTH+10);
    copyBuffer.append((char *)encBuffer);

    unsigned char *decoded = NULL;
    size_t decodedSize = 0;

    Base64Wrapper::decode(copyBuffer, &decoded, &decodedSize);
    decoded[decodedSize] = '\0';

    if(strcmp((char*)outputBuffer, (char*)decoded) != 0) {
        std::cout << "Different";
    }

    memset(buffer, 0, ctx.len);

    if(rsa_pkcs1_decrypt(&ctx, RSA_PRIVATE, &outputSize, decoded, buffer, MAX_OUTPUT_LENGTH) != 0) {
        std::cout << "Decryption error: rsa_pkcs1_decrypt failed" << std::endl;
        return false;
    }

    ::cleanMemory(outputBuffer, MAX_OUTPUT_LENGTH);
    ::cleanMemory(buffer, ctx.len);

    delete [] outputBuffer;
    delete [] buffer;
    delete [] encBuffer;
    delete [] decoded;
    //delete [] input;

    return true;
}

ただし、暗号化から rsa_pkcs1_decrypt を呼び出すとoutputBuffer、すべて正常に動作します。

テキストを暗号化して送信し、コードの別の場所で復号化する必要があります。

私が間違っていることは何ですか?

4

2 に答える 2

1

クラシック メモリ オーバーフロー

char *input = new char[inputSize];
input[inputSize] = '\0';
于 2012-12-10T01:24:41.920 に答える
1

私はついに解決策を見つけました。

問題は、からの出力が で始まるため、strlen((char*)outputBuffer)常にあったためです。0rsa_pkcs1_encrypt\0

正しい解決策は

bool AsymetricCipher::encrypt(const std::string &pathToPublicKey, std::istream &inputData, std::ostream &encryptedData) {
    if(initServerPublicCtx(pathToPublicKey, 512)) {
        std::cout << "Encryption error: Can't load public key from file: " << pathToPublicKey << std::endl;
        return false;
    }

    entropy_context entropy;
    ctr_drbg_context ctr_drbg;
    char *pers = "rsa_encrypt";

    entropy_init(&entropy);
    if(ctr_drbg_init(&ctr_drbg, entropy_func, &entropy, (unsigned char*)pers, strlen(pers)) != 0) {
        std::cout << "Encryption error: ctr_drbg_init failed" << std::endl;
        return false;
    }


    size_t inputSize = ::getStreamSize(inputData);
    unsigned char *buffer = new unsigned char[inputSize];
    memset(buffer, 0, inputSize);
    inputData.read((char *)buffer, inputSize);


    size_t MAX_OUTPUT_LENGTH = ctx.len;
    unsigned char *outputBuffer = new unsigned char[MAX_OUTPUT_LENGTH];
    memset(outputBuffer, 0, MAX_OUTPUT_LENGTH);

    bool retVal = true;
    if(rsa_pkcs1_encrypt(&ctx, ctr_drbg_random, &ctr_drbg, RSA_PUBLIC, inputSize, buffer, outputBuffer) != 0) {
        std::cout << "Encryption error: rsa_pkcs1_encrypt failed" << std::endl;
        retVal = false;
    }

    if(retVal) {
        std::string base64;
        Base64Wrapper::encode(outputBuffer, MAX_OUTPUT_LENGTH, base64);
        encryptedData << base64;
        ::cleanMemory(base64);
    }

    ::cleanMemory(outputBuffer, MAX_OUTPUT_LENGTH);
    ::cleanMemory(buffer, ctx.len);

    delete [] outputBuffer;
    delete [] buffer;

    return retVal;
}

そして解読のために

bool AsymetricCipher::decrypt( const std::string &pathToPrivateKey, std::istream &encryptedData, std::ostream &decryptedData ) {
    if(initServerPrivateCtx(pathToPrivateKey, 512)) {
        std::cout << "Decrypt error: Can't load private key from file: " << pathToPrivateKey << std::endl;
        return false;
    }

    size_t inputSize = ::getStreamSize(encryptedData);
    size_t outputSize = 0;

    unsigned char* buffer = NULL;
    std::string base64;
    size_t bufferSize = 0;
    encryptedData >> base64;

    Base64Wrapper::decode(base64, &buffer, &bufferSize);
    ::cleanMemory(base64);

    size_t MAX_OUTPUT_LENGTH = ctx.len;
    unsigned char *outputBuffer = new unsigned char[MAX_OUTPUT_LENGTH];

    bool retVal = true;
    if(rsa_pkcs1_decrypt(&ctx, RSA_PRIVATE, &outputSize, buffer, outputBuffer, MAX_OUTPUT_LENGTH) != 0) {
        std::cout << "Decryption error: rsa_pkcs1_decrypt failed" << std::endl;
        retVal = false;
    }

    if(retVal) {
        outputBuffer[outputSize] = '\0';
        decryptedData << outputBuffer;
    }

    ::cleanMemory(buffer, bufferSize);
    ::cleanMemory(outputBuffer, outputSize);

    delete [] outputBuffer;
    delete [] buffer;

    return retVal;
}
于 2012-12-11T15:50:07.930 に答える