使用CryptoPP解密流后如何执行去填充 [英] How to perform unpadding after decryption of stream using CryptoPP

查看:342
本文介绍了使用CryptoPP解密流后如何执行去填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有要解密的流.我将其分为多个块,然后将每个块传递给下面的方法.我需要解密的数据由16个字节的块加密,如果最后一个块小于16,则其余所有字节均由填充填充.然后在解密的那一刻,我得到了最后一个块结果,作为包含这些额外填充字节的值.考虑到可以使用不同的填充,我如何确定原始数据的长度并仅返回它的长度,或者确定填充字节并删除它们?

I've got the stream to decrypt. I divide it into blocks and pass each block to the method below. The data I need to decrypt is encrypted by 16 - bytes blocks and if the last block is less than 16, then all the rest bytes are filled by padding. Then in the moment of decryption I'm getting my last block result as the value including these additional padding bytes. How can I determine the length of original data and return only it or determine the padding bytes and remove them, considering different paddings could be used?

void SymmetricAlgorithm::Decrypt(byte* buffer, size_t dataBytesSize) {    
     MeterFilter meter(new ArraySink(buffer, dataBytesSize));
     CBC_Mode<CryptoPP::Rijndael>::Decryption dec(&Key.front(), Key.size(), &IV.front());
        StreamTransformationFilter* filter = new StreamTransformationFilter(dec, new Redirector(meter), PKCS_PADDING);
        ArraySource(buffer, dataBytesSize, true, filter);
        dec.Resynchronize(&IV.front());
}

现在我正在尝试使用PKCS_PADDING和Rijndael,但总的来说,我可能需要使用任何算法和任何填充.

Now I'm trying with PKCS_PADDING and Rijndael, but in general I might need work with any algorithm and any padding.

推荐答案

碰巧的是,我偶尔从以下

As it happens, I found what I needed occasionally in the example from this question. Appreciate your help, Gabriel L., but I didn't want make my method not to use padding at all. Sorry for unclear explanations, I wanted to extract plain data from decrypted data, which includes padding symbols. And the bold row in this code shows how to find out plain data bytes count.

void SymmetricAlgorithm::Decrypt(byte* buffer, size_t dataBytesSize) {    
 MeterFilter meter(new ArraySink(buffer, dataBytesSize));
 CBC_Mode<CryptoPP::Rijndael>::Decryption dec(&Key.front(), Key.size(), &IV.front());
    StreamTransformationFilter* filter = new StreamTransformationFilter(dec, new Redirector(meter), PKCS_PADDING);
    ArraySource(buffer, dataBytesSize, true, filter);
    int t = meter.GetTotalBytes(); //plain data bytes count
    dec.Resynchronize(&IV.front());
}

这篇关于使用CryptoPP解密流后如何执行去填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆