DES.Create Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Hiermee maakt u een exemplaar van een cryptografisch object om het algoritme Data Encryption Standard (DES) uit te voeren.
Overloads
| Name | Description |
|---|---|
| Create() |
Hiermee maakt u een exemplaar van een cryptografisch object om het algoritme Data Encryption Standard (DES) uit te voeren. |
| Create(String) |
Hiermee maakt u een exemplaar van een cryptografisch object om de opgegeven implementatie van het algoritme Data Encryption Standard (DES) uit te voeren. |
Create()
Hiermee maakt u een exemplaar van een cryptografisch object om het algoritme Data Encryption Standard (DES) uit te voeren.
public:
static System::Security::Cryptography::DES ^ Create();
public static System.Security.Cryptography.DES Create();
static member Create : unit -> System.Security.Cryptography.DES
Public Shared Function Create () As DES
Retouren
Een cryptografisch object.
Voorbeelden
In het volgende codevoorbeeld ziet u hoe u een DES object maakt en gebruikt om gegevens in een bestand te versleutelen en ontsleutelen.
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
class DESSample
{
static void Main()
{
try
{
byte[] key;
byte[] iv;
// Create a new DES object to generate a random key
// and initialization vector (IV).
using (DES des = DES.Create())
{
key = des.Key;
iv = des.IV;
}
// Create a string to encrypt.
string original = "Here is some data to encrypt.";
// The name/path of the file to write.
string filename = "CText.enc";
// Encrypt the string to a file.
EncryptTextToFile(original, filename, key, iv);
// Decrypt the file back to a string.
string decrypted = DecryptTextFromFile(filename, key, iv);
// Display the decrypted string to the console.
Console.WriteLine(decrypted);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
public static void EncryptTextToFile(string text, string path, byte[] key, byte[] iv)
{
try
{
// Create or open the specified file.
using (FileStream fStream = File.Open(path, FileMode.Create))
// Create a new DES object.
using (DES des = DES.Create())
// Create a DES encryptor from the key and IV
using (ICryptoTransform encryptor = des.CreateEncryptor(key, iv))
// Create a CryptoStream using the FileStream and encryptor
using (var cStream = new CryptoStream(fStream, encryptor, CryptoStreamMode.Write))
{
// Convert the provided string to a byte array.
byte[] toEncrypt = Encoding.UTF8.GetBytes(text);
// Write the byte array to the crypto stream.
cStream.Write(toEncrypt, 0, toEncrypt.Length);
}
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
throw;
}
}
public static string DecryptTextFromFile(string path, byte[] key, byte[] iv)
{
try
{
// Open the specified file
using (FileStream fStream = File.OpenRead(path))
// Create a new DES object.
using (DES des = DES.Create())
// Create a DES decryptor from the key and IV
using (ICryptoTransform decryptor = des.CreateDecryptor(key, iv))
// Create a CryptoStream using the FileStream and decryptor
using (var cStream = new CryptoStream(fStream, decryptor, CryptoStreamMode.Read))
// Create a StreamReader to turn the bytes back into text
using (StreamReader reader = new StreamReader(cStream, Encoding.UTF8))
{
// Read back all of the text from the StreamReader, which receives
// the decrypted bytes from the CryptoStream, which receives the
// encrypted bytes from the FileStream.
return reader.ReadToEnd();
}
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
throw;
}
}
}
Imports System.IO
Imports System.Security.Cryptography
Imports System.Text
Module DESSample
Sub Main()
Try
Dim key As Byte()
Dim iv As Byte()
' Create a new DES object to generate a key
' and initialization vector (IV).
Using des As DES = DES.Create
key = des.Key
iv = des.IV
End Using
' Create a string to encrypt.
Dim original As String = "Here is some data to encrypt."
' The name/path of the file to write.
Dim filename As String = "CText.enc"
' Encrypt the string to a file.
EncryptTextToFile(original, filename, key, iv)
' Decrypt the file back to a string.
Dim decrypted As String = DecryptTextFromFile(filename, key, iv)
' Display the decrypted string to the console.
Console.WriteLine(decrypted)
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub
Sub EncryptTextToFile(text As String, path As String, key As Byte(), iv As Byte())
Try
' Create or open the specified file.
' Create a new DES object,
' Create a DES encryptor from the key and IV,
' Create a CryptoStream using the MemoryStream And encryptor
Using fStream As FileStream = File.Open(path, FileMode.Create),
des As DES = DES.Create,
encryptor As ICryptoTransform = des.CreateEncryptor(key, iv),
cStream = New CryptoStream(fStream, encryptor, CryptoStreamMode.Write)
' Convert the passed string to a byte array.
Dim toEncrypt As Byte() = Encoding.UTF8.GetBytes(text)
' Write the byte array to the crypto stream.
cStream.Write(toEncrypt, 0, toEncrypt.Length)
End Using
Catch e As CryptographicException
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message)
Throw
End Try
End Sub
Function DecryptTextFromFile(path As String, key As Byte(), iv As Byte()) As String
Try
' Open the specified file
' Create a new DES object.
' Create a DES decryptor from the key and IV
' Create a CryptoStream using the MemoryStream and decryptor
' Create a StreamReader to turn the bytes back into text
Using mStream As FileStream = File.OpenRead(path),
des As DES = DES.Create,
decryptor As ICryptoTransform = des.CreateDecryptor(key, iv),
cStream = New CryptoStream(mStream, decryptor, CryptoStreamMode.Read),
reader = New StreamReader(cStream, Encoding.UTF8)
' Read back all of the text from the StreamReader, which receives
' the decrypted bytes from the CryptoStream, which receives the
' encrypted bytes from the FileStream.
Return reader.ReadToEnd()
End Using
Catch e As CryptographicException
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message)
Return Nothing
End Try
End Function
End Module
In het volgende codevoorbeeld ziet u hoe u een DES object maakt en gebruikt om gegevens in het geheugen te versleutelen en ontsleutelen.
using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;
class DESSample2
{
static void Main()
{
try
{
byte[] key;
byte[] iv;
// Create a new DES object to generate a random key
// and initialization vector (IV).
using (DES des = DES.Create())
{
key = des.Key;
iv = des.IV;
}
// Create a string to encrypt.
string original = "Here is some data to encrypt.";
// Encrypt the string to an in-memory buffer.
byte[] encrypted = EncryptTextToMemory(original, key, iv);
// Decrypt the buffer back to a string.
string decrypted = DecryptTextFromMemory(encrypted, key, iv);
// Display the decrypted string to the console.
Console.WriteLine(decrypted);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
public static byte[] EncryptTextToMemory(string text, byte[] key, byte[] iv)
{
try
{
// Create a MemoryStream.
using (MemoryStream mStream = new MemoryStream())
{
// Create a new DES object.
using (DES des = DES.Create())
// Create a DES encryptor from the key and IV
using (ICryptoTransform encryptor = des.CreateEncryptor(key, iv))
// Create a CryptoStream using the MemoryStream and encryptor
using (var cStream = new CryptoStream(mStream, encryptor, CryptoStreamMode.Write))
{
// Convert the provided string to a byte array.
byte[] toEncrypt = Encoding.UTF8.GetBytes(text);
// Write the byte array to the crypto stream and flush it.
cStream.Write(toEncrypt, 0, toEncrypt.Length);
// Ending the using statement for the CryptoStream completes the encryption.
}
// Get an array of bytes from the MemoryStream that holds the encrypted data.
byte[] ret = mStream.ToArray();
// Return the encrypted buffer.
return ret;
}
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
throw;
}
}
public static string DecryptTextFromMemory(byte[] encrypted, byte[] key, byte[] iv)
{
try
{
// Create a buffer to hold the decrypted data.
// DES-encrypted data will always be slightly bigger than the decrypted data.
byte[] decrypted = new byte[encrypted.Length];
int offset = 0;
// Create a new MemoryStream using the provided array of encrypted data.
using (MemoryStream mStream = new MemoryStream(encrypted))
{
// Create a new DES object.
using (DES des = DES.Create())
// Create a DES decryptor from the key and IV
using (ICryptoTransform decryptor = des.CreateDecryptor(key, iv))
// Create a CryptoStream using the MemoryStream and decryptor
using (var cStream = new CryptoStream(mStream, decryptor, CryptoStreamMode.Read))
{
// Keep reading from the CryptoStream until it finishes (returns 0).
int read = 1;
while (read > 0)
{
read = cStream.Read(decrypted, offset, decrypted.Length - offset);
offset += read;
}
}
}
// Convert the buffer into a string and return it.
return Encoding.UTF8.GetString(decrypted, 0, offset);
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
throw;
}
}
}
Imports System.Security.Cryptography
Imports System.Text
Imports System.IO
Module DESSample
Sub Main()
Try
Dim key As Byte()
Dim iv As Byte()
' Create a new DES object to generate a key
' and initialization vector (IV).
Using des As DES = DES.Create
key = des.Key
iv = des.IV
End Using
' Create a string to encrypt.
Dim original As String = "Here is some data to encrypt."
' Encrypt the string to an in-memory buffer.
Dim encrypted As Byte() = EncryptTextToMemory(original, key, iv)
' Decrypt the buffer back to a string.
Dim decrypted As String = DecryptTextFromMemory(encrypted, key, iv)
' Display the decrypted string to the console.
Console.WriteLine(decrypted)
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub
Function EncryptTextToMemory(text As String, key As Byte(), iv As Byte()) As Byte()
Try
' Create a MemoryStream.
Using mStream As New MemoryStream
' Create a new DES object,
' Create a DES encryptor from the key and IV,
' Create a CryptoStream using the MemoryStream And encryptor
Using des As DES = DES.Create,
encryptor As ICryptoTransform = des.CreateEncryptor(key, iv),
cStream = New CryptoStream(mStream, encryptor, CryptoStreamMode.Write)
' Convert the passed string to a byte array.
Dim toEncrypt As Byte() = Encoding.UTF8.GetBytes(text)
' Write the byte array to the crypto stream and flush it.
cStream.Write(toEncrypt, 0, toEncrypt.Length)
' Ending the using block for the CryptoStream completes the encryption.
End Using
' Get an array of bytes from the MemoryStream that holds the encrypted data.
Dim ret As Byte() = mStream.ToArray()
' Return the encrypted buffer.
Return ret
End Using
Catch e As CryptographicException
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message)
Throw
End Try
End Function
Function DecryptTextFromMemory(encrypted As Byte(), key As Byte(), iv As Byte()) As String
Try
' Create a buffer to hold the decrypted data.
' DES-encrypted data will always be slightly bigger than the decrypted data.
Dim decrypted(encrypted.Length - 1) As Byte
Dim offset As Integer = 0
' Create a new MemoryStream using the provided array of encrypted data.
' Create a new DES object.
' Create a DES decryptor from the key and IV
' Create a CryptoStream using the MemoryStream and decryptor
Using mStream As New MemoryStream(encrypted),
des As DES = DES.Create,
decryptor As ICryptoTransform = des.CreateDecryptor(key, iv),
cStream = New CryptoStream(mStream, decryptor, CryptoStreamMode.Read)
' Keep reading from the CryptoStream until it finishes (returns 0).
Dim read As Integer = 1
While (read > 0)
read = cStream.Read(decrypted, offset, decrypted.Length - offset)
offset += read
End While
End Using
' Convert the buffer into a string and return it.
Return New ASCIIEncoding().GetString(decrypted, 0, offset)
Catch e As CryptographicException
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message)
Return Nothing
End Try
End Function
End Module
Opmerkingen
Hiermee maakt u een exemplaar van het DES object dat kan worden gebruikt voor het versleutelen en ontsleutelen van gegevens.
Zie ook
Van toepassing op
Create(String)
Hiermee maakt u een exemplaar van een cryptografisch object om de opgegeven implementatie van het algoritme Data Encryption Standard (DES) uit te voeren.
public:
static System::Security::Cryptography::DES ^ Create(System::String ^ algName);
public static System.Security.Cryptography.DES Create(string algName);
static member Create : string -> System.Security.Cryptography.DES
Public Shared Function Create (algName As String) As DES
Parameters
Retouren
Een cryptografisch object.