Having trouble decrypting a well-formed cipher text using Crypto++

后端 未结 2 1689
醉话见心
醉话见心 2020-12-12 02:02

Background

I\'ve been struggling with decrypting an apparently well-formed cipher text for about a day. Assume we\'ve got the following hex-encoded cipher text whi

相关标签:
2条回答
  • 2020-12-12 02:31

    Full source codes of those function can be found on my repo on GitHub

    I'd make these changes at minimum:

    QString CryptoUtils::encrypt(QString text, QString keyhex)
    {
        ...
    
        // name the variable, kill the memory leak
        SHA256 sha256;
        StringSource ss1(decodedKey, size, true, new HashFilter(sha256, new ArraySink(key, AES::MAX_KEYLENGTH)));
        ...
    
        // name the variable
        StringSource ss2(plain, true, new StreamTransformationFilter(Encryptor, new HexEncoder(new StringSink(encrypted))));
    
        // verify embedded NULLs don't affect results
        QString qs = QString::fromStdString(encrypted);
        assert(qs.length() == encrypted.length());
    }
    

    And:

    QString CryptoUtils::decrypt(QString text, QString keyhex)
    {
        // bad karma here...
        string encrypted = text.toStdString();
        assert(encrypted.length() == text.length());
        ...
    
        // name the variable, kill the memory leak
        SHA256 sha256;
        StringSource ss1(decodedKey, size, true, new HashFilter(sha256, new ArraySink(key, AES::MAX_KEYLENGTH)));
        ...
    
        // name the variable,
        StringSource ss2(encrypted, true, new HexDecoder(new StreamTransformationFilter(Decryptor, new StringSink(plain))));
    
        // verify embedded NULLs don't affect results
        QString qs = QString::fromStdString(plain);
        assert(qs.length() == plain.length());
    }
    
    0 讨论(0)
  • 2020-12-12 02:51

    The hexEncode function seems to misbehave:

    QString CryptoUtils::hexEncode(QString text)
    {
        byte *bytearray = (byte *) text.toLatin1().data();
        int length = text.toLatin1().length();
    
        return hexEncode(bytearray, length);
    }
    

    Should be replaced with:

    QString CryptoUtils::hexEncode(QString text)
    {
        byte *bytearray = (byte *) text.toStdString().data();
        int length = text.length();
    
        return hexEncode(bytearray, length);
    }
    
    0 讨论(0)
提交回复
热议问题