CryptoProvider.Encrypt(Byte[]) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将明文加密为密文。
public:
cli::array <System::Byte> ^ Encrypt(cli::array <System::Byte> ^ clearText);
public byte[] Encrypt (byte[] clearText);
member this.Encrypt : byte[] -> byte[]
Public Function Encrypt (clearText As Byte()) As Byte()
参数
- clearText
- Byte[]
要加密的明文内容。
返回
Byte[]
指定 clearText
的已加密密文。
例外
clearText
为 null。
不允许加密。
示例
以下示例演示如何使用 Encrypt 方法将明文数据转换为加密文本数据。
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
字节长度应是 cipher BlockSize 属性的倍数。
数字版权管理系统使用 AES 块密码。 使用 AES,块是独立加密的,以便两个相同明文块生成相同的密码文本结果。 为了最大程度地减少来自独立块加密的潜在解密威胁,应用程序应使用方法来修改内容(例如压缩),以避免加密相同的明文块。