简体   繁体   English

在C#中,如何添加数据填充?

[英]In C#, how to add data padding?

Sometimes you need to pad data (stream of bytes) to discrete block sizes. 有时您需要将数据(字节流)填充为离散的块大小。 For example, ProtectedMemory can only operate on blocks of data in multiples of 16 bytes. 例如,ProtectedMemory只能对16字节倍数的数据块进行操作。 I know some other classes such as ProtectedData and AESCryptoServiceProvider and AESManaged have built-in support for padding, but for those classes that don't have it built-in ... How do you pad data for ProtectedMemory? 我知道其他一些类(例如ProtectedData和AESCryptoServiceProvider和AESManaged)具有对填充的内置支持,但对于那些没有内置填充的类……如何填充ProtectedMemory的数据?

Sure I can write it myself, adhering to standards such as ANSIX923 or ISO10126 or PKCS7, but there must be a good way to make .Net do it. 当然,我可以遵循ANSIX923或ISO10126或PKCS7等标准自己编写它,但是必须有一种使.Net做到这一点的好方法。 I would have expected that functionality to be part of Stream, or MemoryStream... I just can't find it anywhere except in specialized encryption classes. 我本来希望该功能成为Stream或MemoryStream的一部分……我只是在专门的加密类中找不到它。

Use ProtectedMemory to store a 16 or 32 byte encryption key and then use AESManaged or AESCryptoServiceProvider to protect & store your data with padding. 使用ProtectedMemory存储16或32字节的加密密钥,然后使用AESManaged或AESCryptoServiceProvider通过填充保护和存储数据。 Or use ProtectedMemory to store 16 or 32 byte optionalEntropy, and then use ProtectedData to protect & store your data with padding. 或使用ProtectedMemory存储16或32字节的optionalEntropy,然后使用ProtectedData通过填充保护和存储数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM