我对 C++ 和块密码加密都很陌生,目前正在为 AES(16 字节种子/16 字节块)编写解密函数。 一切都很顺利,但我的总数据大小并不总是我的块大小的倍数。 我想知道处理数据末尾剩余数据的最佳方法是什么。
我正在使用 Crypto++ 作为 AES 库。
ProcessBlock()
函数采用输入和输出字符数组。 我假设它们至少与块大小足够大。
处理分组密码中的所有 16 字节块,然后处理剩余数据的最佳方法是什么?
I'm pretty new to both C++ and Block Cipher encryption, and I am currently in the process of writing a decryption function for AES (16 byte seed / 16 byte blocks). All is going well, but my total data size is not always a multiple of my block size. I'm wondering what the best way to handle leftover data at the end of my data.
I'm using Crypto++ for the AES library.
The ProcessBlock()
function takes an Input and Output char array. I'm assuming it is expecting them to be at least big enough as the block size.
What would be the best way to process all 16 byte blocks in a block cipher, and then also process the leftover data?
发布评论
评论(3)
你想要的是一个填充系统。
查看这篇关于 Crypto++ 的 CodeProject 文章:
What you want is a padding system.
Check out this CodeProject article on Crypto++:
有一个所谓的“填充”的 PKCS 标准,
请参阅 wikipedia 页面,但它相当于用以下之一进行填充:
这样您就可以在解密过程中知道原始消息的结尾位置...
There's a PKCS standard for what's called "padding"
See the wikipedia page, but it amounts to padding with one of:
This way you know during decryption where the original message ends...
这不仅仅是填充——您需要一种操作模式。 好数学,坏数学博客正在撰写一个关于它们是什么以及如何使用它们的精彩系列 维基百科条目。 有一件事非常非常重要:永远不要使用 ECB(电子密码本)模式 - 在该模式下独立加密每个块。 这是显而易见的方法,但它提供的安全性却极差。
但理想情况下,您甚至不必亲自执行此操作。 您的加密库应该提供它。 如果没有,我建议换成其他的。 像 OpenSSL 一样。
It's more than just padding - you need a Mode of Operation. The Good Math, Bad Math blog is writing up an excellent series on what they are and how to use them here. Also see the wikipedia entry. One thing that's really, really important: Never, ever use ECB (Electronic Code Book) mode - where you encrypt each block independently. It's the obvious way to do it, but it provides appallingly poor security.
Ideally, though, you shouldn't even have to do this yourself. Your crypto library should provide it. If it doesn't, I'd suggest changing to something else. like OpenSSL.