获取.Net中CryptoStream的长度 [英] Get the length of a CryptoStream in .Net

查看:43
本文介绍了获取.Net中CryptoStream的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究用于加密/解密文件的软件.我希望能够猜测加密后的数据长度,但不能使用CryptoStream.Length(它引发NotSupportedException).有什么办法可以猜测吗?

I'm working on software which encrypts/decrypts files. I would like to be able to guess the length of the data after the encryption but I can't use CryptoStream.Length (It throws a NotSupportedException). Is there any way to guess it ?

我正在使用RijndaelManaged(.Net Framework 4.0)

I'm using RijndaelManaged (.Net Framework 4.0)

推荐答案

这比我说的要好得多 http://www.obviex.com/Articles/CiphertextSize.aspx

从那里:

在最普通的情况下,密文的大小可以计算为:

In the most generic case, the size of the ciphertext can be calculated as:

CipherText = PlainText +块-(PlainText MOD块)

CipherText = PlainText + Block - (PlainText MOD Block)

其中CipherText,PlainText和Block分别指示密文,明文和加密块的大小.基本上,将得到的密文大小计算为扩展到下一个块的明文大小.如果使用填充,并且明文的大小是块大小的整数倍,则会添加一个额外的包含填充信息的块.

where CipherText, PlainText, and Block indicate the sizes of the ciphertext, plaintext, and encryption block respectively. Basically, the resulting ciphertext size is computed as the size of the plaintext extended to the next block. If padding is used and the size of the plaintext is an exact multiple of the block size, one extra block containing padding information will be added.

假设您要使用Rijndael加密算法以128位(16字节)的块大小和PKCS#7填充来加密9位数的社会安全号码(SSN).(出于说明的目的,假设在加密之前从SSN值中删除了破折号,因此"123-45-6789"变为"123456789",并且该值被视为字符串,而不是数字.)如果将SSN中的数字定义为ASCII字符,则密文的大小可以计算为:

Let's say that you want to encrypt a nine-digit Social Security Number (SSN) using the Rijndael encryption algorithm with the 128-bit (16-byte) block size and PKCS #7 padding. (For the purpose of the illustration, assume that dashes are removed from the SSN value before the encryption, so that "123-45-6789" becomes "123456789", and the value is treated as a string, not as a number.) If the digits in the SSN are defined as ASCII characters, the size of the ciphertext can be calculated as:

CipherText = 9 + 16-(9 MOD 16)= 9 + 16-9 = 16(字节)

CipherText = 9 + 16 - (9 MOD 16) = 9 + 16 - 9 = 16 (bytes)

请注意,如果明文值的大小是块大小的精确倍数,则包含填充信息的额外块将被附加到密文中.例如,如果要加密16位信用卡号(定义为16个字符的ASCII字符串),则密文的大小将为:

Notice that if the size of the plaintext value is the exact multiple of the block size, an extra block containing padding information will be appended to the ciphertext. For example, if you are to encrypt a 16-digit credit card number (defined as a 16-character ASCII string), the size of the ciphertext will be:

CipherText = 16 + 16-(16 MOD 16)= 16 + 16-0 = 32(字节)

CipherText = 16 + 16 - (16 MOD 16) = 16 + 16 - 0 = 32 (bytes)

这篇关于获取.Net中CryptoStream的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆