CryptoProvider.Encrypt(Byte[]) Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Encrypts clear text to cipher text.
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[]
The clear text content to encrypt.
Encrypted cipher text of the given clearText
.
clearText
is null.
Encryption is not permitted.
The following example shows how to use the Encrypt method to convert clear-text data to encrypted-text data.
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.")
The byte length of the clearText
buffer should be a multiple of the cipher BlockSize property.
The digital rights management system uses AES block cipher. With AES, blocks are encrypted independently such that two blocks of identical clear text produce identical cipher text results. To minimize potential decryption threats from independent block encryption, applications should employ methods to modify content, such as compression, to avoid encrypting identical clear text blocks.
Produto | Versões |
---|---|
.NET Framework | 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
Windows Desktop | 3.0, 3.1, 5, 6, 7, 8, 9 |
Comentários do .NET
O .NET é um projeto código aberto. Selecione um link para fornecer comentários: