如何预先确定加密操作中生成的密文的长度?

时间:2021-09-22 17:21:14

I have an application which stores some information in an encrypted state, both on file and in a database. How can I calculate what the length of the resultant cipher text will be based on the plain text input?

我有一个应用程序,它以加密状态存储一些信息,包括文件和数据库。如何根据纯文本输入计算得到的密文的长度?

The encryption operation consists of using the .NET RijndaelManaged class/algorithm and then a conversion to a Base64 string prior to storage.

加密操作包括使用.NET RijndaelManaged类/算法,然后在存储之前转换为Base64字符串。

What I want to be able to do is to know beforehand how long the encrypted string will be for a given input so that I can limit the length of the input accordingly in relation to the storage space available for its encrypted form (if that makes sense!).

我希望能够做的是事先知道加密字符串对于给定输入的长度,以便相应于可用于其加密形式的存储空间相应地限制输入的长度(如果这是有意义的话) !)。

Thanks

2 个解决方案

#1


Rijndael's output is the same size as the input, rounded up to the next closest multiple of the block size (usually 128 bits, aka 16 bytes). Base64 expands its input to its output by 4/3 -- it takes 4 bytes of output to represent each 3 bytes of input.

Rijndael的输出与输入的大小相同,向上舍入到块大小的下一个最接近的倍数(通常为128位,也就是16个字节)。 Base64将其输入扩展到其输出4/3 - 它需要4个字节的输出来表示每个3字节的输入。

So if you have for example an input of 70 bytes, the encrypting step will produce 80 bytes of output (closest multiple of 16 that's > 70), Base64 will turn that into 108 (81/3 times 4).

因此,如果你有一个70字节的输入,加密步骤将产生80字节的输出(16的最接近的倍数> 70),Base64将其转换为108(81/3乘以4)。

#2


The encrypted text will be the first cipher block size multiple bigger than you text. You check your Algorithm BlockSize property. Pure Base64 encoding increases the output by a third, but this can vary if you also need to URL escape (percent encode) certain Base64 symbols (like '+' and '/').

加密文本将是第一个比文本更大的密码块大小。您检查您的Algorithm BlockSize属性。纯Base64编码将输出增加了三分之一,但如果您还需要URL转义(百分比编码)某些Base64符号(如'+'和'/'),则可能会有所不同。

#1


Rijndael's output is the same size as the input, rounded up to the next closest multiple of the block size (usually 128 bits, aka 16 bytes). Base64 expands its input to its output by 4/3 -- it takes 4 bytes of output to represent each 3 bytes of input.

Rijndael的输出与输入的大小相同,向上舍入到块大小的下一个最接近的倍数(通常为128位,也就是16个字节)。 Base64将其输入扩展到其输出4/3 - 它需要4个字节的输出来表示每个3字节的输入。

So if you have for example an input of 70 bytes, the encrypting step will produce 80 bytes of output (closest multiple of 16 that's > 70), Base64 will turn that into 108 (81/3 times 4).

因此,如果你有一个70字节的输入,加密步骤将产生80字节的输出(16的最接近的倍数> 70),Base64将其转换为108(81/3乘以4)。

#2


The encrypted text will be the first cipher block size multiple bigger than you text. You check your Algorithm BlockSize property. Pure Base64 encoding increases the output by a third, but this can vary if you also need to URL escape (percent encode) certain Base64 symbols (like '+' and '/').

加密文本将是第一个比文本更大的密码块大小。您检查您的Algorithm BlockSize属性。纯Base64编码将输出增加了三分之一,但如果您还需要URL转义(百分比编码)某些Base64符号(如'+'和'/'),则可能会有所不同。