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.
我对C ++和Block Cipher加密都很陌生,我目前正在为AES(16字节种子/ 16字节块)编写解密函数。一切顺利,但我的总数据大小并不总是我的块大小的倍数。我想知道在数据结束时处理剩余数据的最佳方法是什么。
I'm using Crypto++ for the AES library.
我正在使用Crypto ++作为AES库。
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.
ProcessBlock()函数接受一个Input和Output char数组。我假设它期望它们至少足够大块作为块大小。
What would be the best way to process all 16 byte blocks in a block cipher, and then also process the leftover data?
处理块密码中所有16字节块的最佳方法是什么,然后还处理剩余数据?
3 个解决方案
#1
1
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.
它不仅仅是填充 - 您需要一种操作模式。 Good Math,Bad Math博客正在写一篇关于它们是什么以及如何在这里使用它们的精彩系列。另请参阅*条目。有一件事真的非常重要:永远不要使用ECB(电子代码簿)模式 - 你可以独立地加密每个块。这是显而易见的方法,但它提供了令人震惊的糟糕安全性。
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.
但理想情况下,你甚至不必自己做。你的加密库应该提供它。如果没有,我建议换成其他东西。喜欢OpenSSL。
#2
0
What you want is a padding system.
你想要的是一个填充系统。
Check out this CodeProject article on Crypto++:
查看有关Crypto ++的CodeProject文章:
When a message is not a multiple of the cipher's block size, ECB or CBC mode messages must be padded. The method and values of padding are a source of problem with respect to interoperability between Cryptographic libraries and APIs. As Garth Lancaster points out, if you're not aware of the particulars of padding, use the StreamTransformationFilter. In this case, the Crypto++ filter will pad for you.
当消息不是密码块大小的倍数时,必须填充ECB或CBC模式消息。填充的方法和值是关于加密库和API之间的互操作性的问题的来源。正如Garth Lancaster所指出的,如果你不知道填充的细节,请使用StreamTransformationFilter。在这种情况下,Crypto ++过滤器将为您填充。
#3
0
There's a PKCS standard for what's called "padding"
对于所谓的“填充”,有一个PKCS标准
See the wikipedia page, but it amounts to padding with one of:
请参阅*页面,但它相当于填充以下其中一个:
01
02 02
03 03 03
04 04 04 04
05 05 05 05 05
This way you know during decryption where the original message ends...
通过这种方式,您可以在解密期间知道原始邮件的结尾...
#1
1
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.
它不仅仅是填充 - 您需要一种操作模式。 Good Math,Bad Math博客正在写一篇关于它们是什么以及如何在这里使用它们的精彩系列。另请参阅*条目。有一件事真的非常重要:永远不要使用ECB(电子代码簿)模式 - 你可以独立地加密每个块。这是显而易见的方法,但它提供了令人震惊的糟糕安全性。
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.
但理想情况下,你甚至不必自己做。你的加密库应该提供它。如果没有,我建议换成其他东西。喜欢OpenSSL。
#2
0
What you want is a padding system.
你想要的是一个填充系统。
Check out this CodeProject article on Crypto++:
查看有关Crypto ++的CodeProject文章:
When a message is not a multiple of the cipher's block size, ECB or CBC mode messages must be padded. The method and values of padding are a source of problem with respect to interoperability between Cryptographic libraries and APIs. As Garth Lancaster points out, if you're not aware of the particulars of padding, use the StreamTransformationFilter. In this case, the Crypto++ filter will pad for you.
当消息不是密码块大小的倍数时,必须填充ECB或CBC模式消息。填充的方法和值是关于加密库和API之间的互操作性的问题的来源。正如Garth Lancaster所指出的,如果你不知道填充的细节,请使用StreamTransformationFilter。在这种情况下,Crypto ++过滤器将为您填充。
#3
0
There's a PKCS standard for what's called "padding"
对于所谓的“填充”,有一个PKCS标准
See the wikipedia page, but it amounts to padding with one of:
请参阅*页面,但它相当于填充以下其中一个:
01
02 02
03 03 03
04 04 04 04
05 05 05 05 05
This way you know during decryption where the original message ends...
通过这种方式,您可以在解密期间知道原始邮件的结尾...