使用Crypto ++时发现无效的PKCS#7块填充

时间:2017-11-25 06:55:18

标签: c++ encryption crypto++ pkcs#7

我们正在使用Crypto ++库中的代码。我们需要加密和解密char*。加密工作正常,没有任何问题,我们能够获得密文。但在解密时发现错误为“找到无效的pkcs#7块填充”。可能是什么问题......?

加密阻止:

char* cspl_crypto::encrypt_rijndael(byte key[], byte iv[], int keysize, char plainText[], int plainTextSize)
{
    vector<byte> cipher;

    std::vector<byte> plain(plainText, plainText + plainTextSize);

    CBC_Mode<AES>::Encryption enc; 
    enc.SetKeyWithIV(key, keysize, iv, keysize);

    // Make room for padding
    cipher.resize(plain.size()+AES::BLOCKSIZE);
    ArraySink cs(&cipher[0], cipher.size());

    ArraySource(plain.data(), plain.size(), true,
        new StreamTransformationFilter(enc, new Redirector(cs)));

    // Set cipher text length now that its known
    cipher.resize(cs.TotalPutLength());
    char returnValue[cipher.size()];
    copy(cipher.begin(), cipher.end(), returnValue);

    return returnValue;
}

Decyption Block:

char* cspl_crypto::decrypt_rijndael(byte key[], byte iv[], int keysize, char cipher[], int size )
{
    std::vector<byte> v(cipher, cipher + size);
    vector<byte> recover;
    CBC_Mode<AES>::Decryption dec;
    dec.SetKeyWithIV(key, keysize, iv, keysize);

    // Recovered text will be less than cipher text
    recover.resize(v.size());
    ArraySink rs(&recover[0], recover.size());

    ArraySource(v.data(), v.size(), true,
        new StreamTransformationFilter(dec, new Redirector(rs)));

    // Set recovered text length now that its known
    recover.resize(rs.TotalPutLength());

    char returnValue[recover.size()];
    copy(recover.begin(), recover.end(), returnValue);

    return returnValue;
}

0 个答案:

没有答案