繁体   English   中英

crypto++ 将 IV 发送到另一个 Function 但我收到错误:StreamTransformationFilter: invalid PKCS #7 block padding found

[英]crypto++ Sending IV to another Function but i get an error: StreamTransformationFilter: invalid PKCS #7 block padding found

测试程序使用crypto++进行加密,然后将iv和消息作为IV::EncryptedMessage发送。

我能够加密,但在解密时得到一个填充错误 #7,我已经搜索了几个小时,但无论如何都找不到将 std::string IV 传递给 crypto++。

我相信我的错误出现在decc function 中,我在哪里使用十六进制解码?

string encc(string plain) {
    using namespace CryptoPP;

    AutoSeededRandomPool prng;


    SecByteBlock iv(AES::BLOCKSIZE);
    std::string sKey = "UltraSecretKeyPhrase";
    SecByteBlock key((const unsigned char*)(sKey.data()), sKey.size());
    prng.GenerateBlock(iv, iv.size());
    std::string cipher, recovered;

    try
    {
        CBC_Mode< AES >::Encryption e;
        e.SetKeyWithIV(key, key.size(), iv);

        StringSource s(plain, true,
            new StreamTransformationFilter(e,
                new StringSink(cipher)
            ) 
        ); 
    }
    catch (const Exception& e)
    {
        exit(1);
    }

    string ciphertxt, ivString;

    HexEncoder encoder(new FileSink(std::cout));
    encoder.Detach(new StringSink(ivString));
    encoder.Put(iv, iv.size());
    encoder.MessageEnd();

    encoder.Detach(new StringSink(ciphertxt));
    encoder.Put((const byte*)&cipher[0], cipher.size());
    encoder.MessageEnd();

    string toSend = ivString + "::" + ciphertxt;
    return toSend;
}

string decc(string toDec) {
    using namespace CryptoPP;

    std::string sKey = "UltraSecretKeyPhrase";
    SecByteBlock key((const unsigned char*)(sKey.data()), sKey.size());
    
    
    std::string recovered;
    string str1 = "::";

   
    size_t found = toDec.find(str1);
    if (found != string::npos) {

        std::string sIv = toDec.substr(0, found);
        std::string encMessage = toDec.substr(found + 2, toDec.length());
        
        cout << endl << "IV: " << sIv << endl << "Encoded Msg: " << encMessage << endl;
        
        string iv;
        HexDecoder decoder;

        decoder.Attach(new StringSink(iv));
        decoder.Put((byte*)sIv.data(), sIv.size());
        decoder.MessageEnd();
        try
        {
            CBC_Mode< AES >::Decryption d;
            d.SetKeyWithIV(key.data(), key.size(), (byte *)iv.data(), AES::BLOCKSIZE);

            StringSource s(encMessage, true,
                new StreamTransformationFilter(d,
                    new StringSink(recovered)
                )
            );
            return recovered;
        }
        catch (const Exception& e)
        {
            std::cerr << e.what() << std::endl;
            exit(1);
        }
    }
    else return NULL;
}

int main(){
    string hh = encc("this is encoded");
    cout << hh << endl;
    string gg = decc(hh);
    cout << gg << endl;
    return 0;
}

只是似乎无法为谷歌使用正确的词:)

你有一个解码错误,而不是toDec.substr(found + 2, toDec.length())你应该写toDec.substr(found + 2, toDec.length() - (found + 2)) ,因为.substr( )接受符号数量,而不是结束位置。

也许还有其他错误,但这肯定会破坏解码过程,因为加密消息中有垃圾。

您也可以只使用toDec.substr(found + 2) ,即不指定计数,如果未指定第二个参数,则 substring 将被带到字符串的最后。

我解码了 IV 的十六进制,但从未解码实际的加密消息。 当然是我发了smh之后才看到的。。。

这是任何寻找它的人的 crypto++ 编码和解码功能的功能示例。


string encc(string plain) {
    using namespace CryptoPP;

    AutoSeededRandomPool prng;


    SecByteBlock iv(AES::BLOCKSIZE);
    std::string sKey = "UltraSecretKeyPhrase";
    SecByteBlock key((const unsigned char*)(sKey.data()), sKey.size());
    prng.GenerateBlock(iv, iv.size());
    std::string cipher, recovered;

    try
    {
        CBC_Mode< AES >::Encryption e;
        e.SetKeyWithIV(key, key.size(), iv);

        StringSource s(plain, true,
            new StreamTransformationFilter(e,
                new StringSink(cipher)
            ) 
        ); 
    }
    catch (const Exception& e)
    {
        exit(1);
    }

    string ciphertxt, ivString;

    HexEncoder encoder(new FileSink(std::cout));
    encoder.Detach(new StringSink(ivString));
    encoder.Put(iv, iv.size());
    encoder.MessageEnd();

    encoder.Detach(new StringSink(ciphertxt));
    encoder.Put((const byte*)&cipher[0], cipher.size());
    encoder.MessageEnd();

    string toSend = ivString + "::" + ciphertxt;
    return toSend;
}

string decc(string toDec) {
    using namespace CryptoPP;

    std::string sKey = "UltraSecretKeyPhrase";
    SecByteBlock key((const unsigned char*)(sKey.data()), sKey.size());
    
    
    std::string recovered;
    string str1 = "::";

   
    size_t found = toDec.find(str1);
    if (found != string::npos) {

        std::string sIv = toDec.substr(0, found);
        std::string encMessageHex = toDec.substr(found + 2);
        
        cout << endl << "IV: " << sIv << endl << "Encoded Msg: " << encMessageHex << endl;
        
        string iv, encMessage;
        HexDecoder decoder, msgDecoder;

        decoder.Attach(new StringSink(iv));
        decoder.Put((byte*)sIv.data(), sIv.size());
        decoder.MessageEnd();
        // Forgot to decode encrypted message hex. added here
        decoder.Attach(new StringSink(encMessage));
        decoder.Put((byte*)encMessageHex.data(), encMessageHex.size());
        decoder.MessageEnd();
        try
        {
            CBC_Mode< AES >::Decryption d;
            d.SetKeyWithIV(key.data(), key.size(), (byte *)iv.data(), AES::BLOCKSIZE);

            StringSource s(encMessage, true,
                new StreamTransformationFilter(d,
                    new StringSink(recovered)
                )
            );
            return recovered;
        }
        catch (const Exception& e)
        {
            std::cerr << e.what() << std::endl;
            exit(1);
        }
    }
    else return NULL;
}

int main(){
    string hh = encc("this is encoded");
    cout << hh << endl;
    string gg = decc(hh);
    cout << gg << endl;
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM