CryptoProvider.BlockSize 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取密码块大小(以字节为单位)。
public:
property int BlockSize { int get(); };
public int BlockSize { get; }
member this.BlockSize : int
Public ReadOnly Property BlockSize As Integer
属性值
密码块大小(以字节为单位)。 高级加密标准 (AES) 的默认块大小为 8。
示例
以下示例演示如何使用 BlockSize 属性将明文数据转换为加密文本数据。
WriteStatus(" Binding the author's UseLicense and");
WriteStatus(" obtaining the CryptoProvider.");
using (CryptoProvider cryptoProvider =
authorsUseLicense.Bind(_secureEnv))
{
WriteStatus(" Writing encrypted content.");
using (Stream clearTextStream =
File.OpenRead(contentFile) )
{
using (Stream cryptoTextStream =
File.OpenWrite(encryptedFile))
{
// Write the length of the source content file
// as the first four bytes of the encrypted file.
cryptoTextStream.Write(
BitConverter.GetBytes(clearTextStream.Length),
0, sizeof(Int32));
// Allocate clearText buffer.
byte[] clearTextBlock =
new byte[cryptoProvider.BlockSize];
// Encrypt clearText to cryptoText block by block.
for(;;)
{ // Read clearText block.
int readCount = ReliableRead(
clearTextStream,
clearTextBlock, 0 ,
cryptoProvider.BlockSize);
// readCount of zero is end of data.
if (readCount == 0) break; // for
// Encrypt clearText to cryptoText.
byte[] cryptoTextBlock =
cryptoProvider.Encrypt(clearTextBlock);
// Write cryptoText block.
cryptoTextStream.Write(cryptoTextBlock, 0,
cryptoTextBlock.Length);
}
WriteStatus(" Closing '" + encryptedFilename + "'.");
}// end:using (Stream cryptoTextStream =
}// end:using (Stream clearTextStream =
}// end:using (CryptoProvider cryptoProvider =
WriteStatus(" Done - Content encryption complete.");
WriteStatus(" Binding the author's UseLicense and")
WriteStatus(" obtaining the CryptoProvider.")
Using cryptoProvider As CryptoProvider = authorsUseLicense.Bind(_secureEnv)
WriteStatus(" Writing encrypted content.")
Using clearTextStream As Stream = File.OpenRead(contentFile)
Using cryptoTextStream As Stream = File.OpenWrite(encryptedFile)
' Write the length of the source content file
' as the first four bytes of the encrypted file.
Dim expression As Int32
cryptoTextStream.Write(BitConverter.GetBytes(clearTextStream.Length), 0, Len(expression))
' Allocate clearText buffer.
Dim clearTextBlock(cryptoProvider.BlockSize - 1) As Byte
' Encrypt clearText to cryptoText block by block.
Do
Dim readCount As Integer = ReliableRead(clearTextStream, clearTextBlock, 0, cryptoProvider.BlockSize)
' readCount of zero is end of data.
If readCount = 0 Then ' for
Exit Do
End If
' Encrypt clearText to cryptoText.
Dim cryptoTextBlock() As Byte = cryptoProvider.Encrypt(clearTextBlock)
' Write cryptoText block.
cryptoTextStream.Write(cryptoTextBlock, 0, cryptoTextBlock.Length)
Loop
WriteStatus(" Closing '" & encryptedFilename & "'.")
End Using ' end:using (Stream cryptoTextStream =
End Using ' end:using (Stream clearTextStream =
End Using ' end:using (CryptoProvider cryptoProvider =
WriteStatus(" Done - Content encryption complete.")
注解
clearText
传递给 和 cipherText
Decrypt 的 Encrypt 和 缓冲区的长度必须为 n*BlockSize 个字节,其中“n”是大于或等于 1 的整数。
如果 CanMergeBlocks 为 false
,则传递给 的 Encrypt 缓冲区的长度必须与传递给 Decrypt的缓冲区的长度相同。
如果 CanMergeBlocks 为 true
,则传递给 的 Encrypt 缓冲区的长度可以不同于传递给 Decrypt 的缓冲区 (所有缓冲区大小仍必须始终是长度) 字节的 BlockSize 倍数。
为 BlockSize 1 的 表示密码是流密码; BlockSize 2 或更大的 表示块密码。