问题描述
我正在尝试使用来自Crypto ++库的AES加密:
I am trying to use AES encryption from Crypto++ libary:
CBC_Mode<AES>::Encryption e;
我有一个需要加密的二进制数据块。该类似乎为此提供了一个名为 ProcessData
的方法:
I have a binary data block that I need to encrypt. The class seems to provide a method called ProcessData
for this purpose:
virtual void ProcessData(byte *outString, const byte *inString, size_t length);
最后一个参数好像是输入数据的大小。尚不清楚的是,为什么该方法没有返回我加密数据的大小。是否假设输出数据块的大小与输入数据块的长度完全相同?即使输入数据的大小只有一个字节,这也有效吗?问候。
Looks like the last parameter is the size of the input data. What is not clear is why the method does not return me the size of the encrypted data. Is it assumed that the size of output data block is exactly the same as the length of input data block? Is this valid even if the size of input data is just one byte? Regards.
推荐答案
virtual void ProcessData(byte *outString, const byte *inString, size_t length);
最后一个参数好像是输入数据的大小。还不清楚的是为什么该方法不返回加密数据的大小...
Looks like the last parameter is the size of the input data. What is not clear is why the method does not return me the size of the encrypted data...
ProcessData
是所有分组密码的主力(但不是流密码或其他类型的对象)。另请参阅Crypto ++手册和。 cryptlib.h
被描述为为该库提供统一接口的抽象基类。。
ProcessData
is the workhorse of all block ciphers (but not stream ciphers or other types of objects). Also see the Crypto++ manual and cryptlib.h File Reference. cryptlib.h
is described as "Abstract base classes that provide a uniform interface to this library".
ProcessData
对块大小的数据进行操作。因此, INSIZE
等于 OUTSIZE
等于 BLOCKSIZE
。请注意,没有 INSIZE
或 OUTSIZE
-我将它们用于讨论。每个分组密码将为 BLOCKSIZE
提供一个常数。将有 AES :: BLOCKSIZE
, DES_EDE :: BLOCKSIZE
等。
ProcessData
operates on block-sized lengths of data. So INSIZE
is equal to OUTSIZE
is equal to BLOCKSIZE
. Note that there is no INSIZE
or OUTSIZE
- I used them for discussion. Each block cipher will provide a constant for BLOCKSIZE
. There will be a AES::BLOCKSIZE
, DES_EDE::BLOCKSIZE
, etc.
通常,您不要直接使用 ProcessData
。您可以直接使用它,但将负责所有相关的详细信息(有关以下详细信息)。
Typically you do not use ProcessData
directly. You can use it directly, but you will be responsible for all the associated details (more on the details below).
通常,您使用 StreamTransformationFilter
,但原因尚不明确。 StreamTransformationFilter
根据需要提供输入缓冲,输出缓冲和填充。在Wiki上的中进行了简要讨论。
Typically you use a StreamTransformationFilter
, but its not readily apparent why. StreamTransformationFilter
provides input buffering, output buffering, and padding as required. Its briefly discussed at Init-Update-Final on the wiki.
这是中的实际情况在Crypto ++ Wiki上的示例:
Here's how it looks in practice from the CBC mode example on the Crypto++ wiki:
try
{
cout << "plain text: " << plain << endl;
CBC_Mode< AES >::Encryption e;
e.SetKeyWithIV( key, key.size(), iv );
// The StreamTransformationFilter adds padding
// as required. ECB and CBC Mode must be padded
// to the block size of the cipher.
StringSource ss( plain, true,
new StreamTransformationFilter( e,
new StringSink( cipher )
) // StreamTransformationFilter
); // StringSource
}
catch( const CryptoPP::Exception& e )
{
cerr << e.what() << endl;
exit(1);
}
在上面, CBC_mode
和 StreamTransformationFilter
共同为您提供所需的结果。 CBC_mode
调用 ProcessData
并处理密码链接详细信息。 StreamTransformationFilter
以首选大小提供纯文本。如果没有足够的纯文本,则 StreamTransformationFilter
在输入时对其进行缓冲。如果没有输出缓冲区,则 StreamTransformationFilter
也会缓冲密文。
In the above, CBC_mode
and StreamTransformationFilter
work together to give you desired results. CBC_mode
calls ProcessData
and handles the cipher chaining details. StreamTransformationFilter
feeds the plain text in its preferred size. If there's not enough plain text, then StreamTransformationFilter
buffers it on input. If there's no output buffer, then StreamTransformationFilter
buffers cipher text, too.
StreamTransformationFilter
也将根据需要应用填充。有默认的填充,但是您可以覆盖它。 StreamTransformationFilter
知道要应用什么填充,因为它询问模式( CBC_mode
)是否需要填充以及填充的内容
StreamTransformationFilter
will also apply padding as required. There's a default padding, but you can override it. StreamTransformationFilter
knows what padding to apply because it asks the mode (CBC_mode
) if the padding is required and what the padding should be.
这是 StreamTransformationFilter
符合等式。
请务必查看。如果您习惯Java或OpenSSL编程,它应该为您提供帮助。它应该可以帮助您使其点击。
Be sure to check out Init-Update-Final on the wiki. It should provide the glue for you if you are used to Java or OpenSSL programming. It should help "make it click" for you.
这篇关于Crypto ++输出数据长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!