DESCryptoServiceProvider DESCryptoServiceProvider DESCryptoServiceProvider DESCryptoServiceProvider Class

定義

定義包裝函式物件,以存取資料加密標準 (DES) 演算法的密碼編譯服務供應者 (CSP) 版本。Defines a wrapper object to access the cryptographic service provider (CSP) version of the Data Encryption Standard (DES) algorithm. 這個類別無法被繼承。This class cannot be inherited.

public ref class DESCryptoServiceProvider sealed : System::Security::Cryptography::DES
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class DESCryptoServiceProvider : System.Security.Cryptography.DES
type DESCryptoServiceProvider = class
    inherit DES
Public NotInheritable Class DESCryptoServiceProvider
Inherits DES
繼承
DESCryptoServiceProviderDESCryptoServiceProviderDESCryptoServiceProviderDESCryptoServiceProvider
屬性

範例

下列程式碼範例會使用DESCryptoServiceProvider(實作DES) 與指定的索引鍵 (Key) 和初始化向量 (IV) 來加密所指定的檔案inNameThe following code example uses DESCryptoServiceProvider (an implementation of DES) with the specified key (Key) and initialization vector (IV) to encrypt a file specified by inName. 然後會將所指定檔案的加密的結果outNameIt then outputs the encrypted result to the file specified by outName.

void EncryptData( String^ inName, String^ outName, array<Byte>^desKey, array<Byte>^desIV )
{
   
   //Create the file streams to handle the input and output files.
   FileStream^ fin = gcnew FileStream( inName,FileMode::Open,FileAccess::Read );
   FileStream^ fout = gcnew FileStream( outName,FileMode::OpenOrCreate,FileAccess::Write );
   fout->SetLength( 0 );
   
   //Create variables to help with read and write.
   array<Byte>^bin = gcnew array<Byte>(100);
   long rdlen = 0; //This is the total number of bytes written.

   long totlen = (long)fin->Length; //This is the total length of the input file.

   int len; //This is the number of bytes to be written at a time.

   DES^ des = gcnew DESCryptoServiceProvider;
   CryptoStream^ encStream = gcnew CryptoStream( fout,des->CreateEncryptor( desKey, desIV ),CryptoStreamMode::Write );
   Console::WriteLine( "Encrypting..." );
   
   //Read from the input file, then encrypt and write to the output file.
   while ( rdlen < totlen )
   {
      len = fin->Read( bin, 0, 100 );
      encStream->Write( bin, 0, len );
      rdlen = rdlen + len;
      Console::WriteLine( "{0} bytes processed", rdlen );
   }

   encStream->Close();
   fout->Close();
   fin->Close();
}

private static void EncryptData(String inName, String outName, byte[] desKey, byte[] desIV)
 {    
     //Create the file streams to handle the input and output files.
     FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
     FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
     fout.SetLength(0);
       
     //Create variables to help with read and write.
     byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
     long rdlen = 0;              //This is the total number of bytes written.
     long totlen = fin.Length;    //This is the total length of the input file.
     int len;                     //This is the number of bytes to be written at a time.
 
     DES des = new DESCryptoServiceProvider();          
     CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
                
     Console.WriteLine("Encrypting...");
 
     //Read from the input file, then encrypt and write to the output file.
     while(rdlen < totlen)
     {
         len = fin.Read(bin, 0, 100);
         encStream.Write(bin, 0, len);
         rdlen = rdlen + len;
         Console.WriteLine("{0} bytes processed", rdlen);
     }
 
     encStream.Close();  
     fout.Close();
     fin.Close();                   
 }
Private Shared Sub EncryptData(inName As String, outName As String, _
desKey() As Byte, desIV() As Byte)

    'Create the file streams to handle the input and output files.
    Dim fin As New FileStream(inName, FileMode.Open, FileAccess.Read)
    Dim fout As New FileStream(outName, FileMode.OpenOrCreate, _
       FileAccess.Write)
    fout.SetLength(0)
    
    'Create variables to help with read and write.
    Dim bin(4096) As Byte 'This is intermediate storage for the encryption.
    Dim rdlen As Long = 0 'This is the total number of bytes written.
    Dim totlen As Long = fin.Length 'Total length of the input file.
    Dim len As Integer 'This is the number of bytes to be written at a time.
    Dim des As New DESCryptoServiceProvider()
    Dim encStream As New CryptoStream(fout, _
       des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write)
    
    Console.WriteLine("Encrypting...")
    
    'Read from the input file, then encrypt and write to the output file.
    While rdlen < totlen
        len = fin.Read(bin, 0, 4096)
        encStream.Write(bin, 0, len)
        rdlen = Convert.ToInt32(rdlen + len / des.BlockSize * des.BlockSize)
        Console.WriteLine("Processed {0} bytes, {1} bytes total", len, _
           rdlen)
    End While
    
    encStream.Close()
End Sub

解密可以處理相同的方式;使用 CreateDecryptor而不是CreateEncryptorDecryption can be handled in the same way; use CreateDecryptor instead of CreateEncryptor. 相同的索引鍵 (Key) 和初始化向量 (IV) 用來加密檔案必須用來將它解密。The same key (Key) and initialization vector (IV) used to encrypt the file must be used to decrypt it.

備註

此演算法支援金鑰長度為 64 位元。This algorithm supports a key length of 64 bits.

建構函式

DESCryptoServiceProvider() DESCryptoServiceProvider() DESCryptoServiceProvider() DESCryptoServiceProvider()

初始化 DESCryptoServiceProvider 類別的新執行個體。Initializes a new instance of the DESCryptoServiceProvider class.

方法

Clear() Clear() Clear() Clear()

釋放 SymmetricAlgorithm 類別所使用的所有資源。Releases all resources used by the SymmetricAlgorithm class.

(Inherited from SymmetricAlgorithm)
CreateDecryptor() CreateDecryptor() CreateDecryptor() CreateDecryptor()
CreateDecryptor(Byte[], Byte[]) CreateDecryptor(Byte[], Byte[]) CreateDecryptor(Byte[], Byte[]) CreateDecryptor(Byte[], Byte[])

使用指定的金鑰 (DES) 和初始化向量 (Key),建立對稱資料加密標準 (IV) 解密子物件。Creates a symmetric Data Encryption Standard (DES) decryptor object with the specified key (Key) and initialization vector (IV).

CreateEncryptor() CreateEncryptor() CreateEncryptor() CreateEncryptor()
CreateEncryptor(Byte[], Byte[]) CreateEncryptor(Byte[], Byte[]) CreateEncryptor(Byte[], Byte[]) CreateEncryptor(Byte[], Byte[])

使用指定的金鑰 (DES) 和初始化向量 (Key),建立對稱資料加密標準 (IV) 加密子物件。Creates a symmetric Data Encryption Standard (DES) encryptor object with the specified key (Key) and initialization vector (IV).

Dispose() Dispose() Dispose() Dispose()

釋放 SymmetricAlgorithm 類別目前的執行個體所使用的全部資源。Releases all resources used by the current instance of the SymmetricAlgorithm class.

(Inherited from SymmetricAlgorithm)
Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

釋放 SymmetricAlgorithm 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。Releases the unmanaged resources used by the SymmetricAlgorithm and optionally releases the managed resources.

(Inherited from SymmetricAlgorithm)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GenerateIV() GenerateIV() GenerateIV() GenerateIV()

產生要使用於該演算法的隨機初始化向量 (IV)。Generates a random initialization vector (IV) to use for the algorithm.

GenerateKey() GenerateKey() GenerateKey() GenerateKey()

產生要使用於該演算法的隨機金鑰 (Key)。Generates a random key (Key) to be used for the algorithm.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)
ValidKeySize(Int32) ValidKeySize(Int32) ValidKeySize(Int32) ValidKeySize(Int32)

判斷指定的金鑰大小對目前的演算法是否有效。Determines whether the specified key size is valid for the current algorithm.

(Inherited from SymmetricAlgorithm)

屬性

BlockSize BlockSize BlockSize BlockSize

取得或設定密碼編譯作業的區塊大小,以位元為單位。Gets or sets the block size, in bits, of the cryptographic operation.

(Inherited from SymmetricAlgorithm)
FeedbackSize FeedbackSize FeedbackSize FeedbackSize

取得或設定密碼編譯作業的回饋大小,以位元為單位。Gets or sets the feedback size, in bits, of the cryptographic operation.

(Inherited from SymmetricAlgorithm)
IV IV IV IV

取得或設定對稱演算法的初始化向量 (IV)。Gets or sets the initialization vector (IV) for the symmetric algorithm.

(Inherited from SymmetricAlgorithm)
Key Key Key Key

取得或設定資料加密標準 (DES) 演算法的秘密金鑰。Gets or sets the secret key for the Data Encryption Standard (DES) algorithm.

(Inherited from DES)
KeySize KeySize KeySize KeySize

取得或設定對稱演算法使用之秘密金鑰的大小,以位元為單位。Gets or sets the size, in bits, of the secret key used by the symmetric algorithm.

(Inherited from SymmetricAlgorithm)
LegalBlockSizes LegalBlockSizes LegalBlockSizes LegalBlockSizes

取得對稱演算法所支援的區塊大小,以位元為單位。Gets the block sizes, in bits, that are supported by the symmetric algorithm.

(Inherited from SymmetricAlgorithm)
LegalKeySizes LegalKeySizes LegalKeySizes LegalKeySizes

取得對稱演算法所支援的金鑰大小,以位元為單位。Gets the key sizes, in bits, that are supported by the symmetric algorithm.

(Inherited from SymmetricAlgorithm)
Mode Mode Mode Mode

取得或設定對稱演算法的作業模式。Gets or sets the mode for operation of the symmetric algorithm.

(Inherited from SymmetricAlgorithm)
Padding Padding Padding Padding

取得或設定對稱演算法中使用的填補模式。Gets or sets the padding mode used in the symmetric algorithm.

(Inherited from SymmetricAlgorithm)

欄位

BlockSizeValue BlockSizeValue BlockSizeValue BlockSizeValue

表示密碼編譯作業的區塊大小,以位元為單位。Represents the block size, in bits, of the cryptographic operation.

(Inherited from SymmetricAlgorithm)
FeedbackSizeValue FeedbackSizeValue FeedbackSizeValue FeedbackSizeValue

表示密碼編譯作業的回饋大小,以位元為單位。Represents the feedback size, in bits, of the cryptographic operation.

(Inherited from SymmetricAlgorithm)
IVValue IVValue IVValue IVValue

表示對稱演算法的初始化向量 (IV)。Represents the initialization vector (IV) for the symmetric algorithm.

(Inherited from SymmetricAlgorithm)
KeySizeValue KeySizeValue KeySizeValue KeySizeValue

表示對稱演算法使用之秘密金鑰的大小,以位元為單位。Represents the size, in bits, of the secret key used by the symmetric algorithm.

(Inherited from SymmetricAlgorithm)
KeyValue KeyValue KeyValue KeyValue

表示對稱演算法的秘密金鑰。Represents the secret key for the symmetric algorithm.

(Inherited from SymmetricAlgorithm)
LegalBlockSizesValue LegalBlockSizesValue LegalBlockSizesValue LegalBlockSizesValue

指定對稱演算法所支援的區塊大小,以位元為單位。Specifies the block sizes, in bits, that are supported by the symmetric algorithm.

(Inherited from SymmetricAlgorithm)
LegalKeySizesValue LegalKeySizesValue LegalKeySizesValue LegalKeySizesValue

指定對稱演算法所支援的金鑰大小,以位元為單位。Specifies the key sizes, in bits, that are supported by the symmetric algorithm.

(Inherited from SymmetricAlgorithm)
ModeValue ModeValue ModeValue ModeValue

表示對稱演算法中使用的密碼模式。Represents the cipher mode used in the symmetric algorithm.

(Inherited from SymmetricAlgorithm)
PaddingValue PaddingValue PaddingValue PaddingValue

表示對稱演算法中使用的填補模式。Represents the padding mode used in the symmetric algorithm.

(Inherited from SymmetricAlgorithm)

明確介面實作

IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose()

釋放 SymmetricAlgorithm 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。Releases the unmanaged resources used by the SymmetricAlgorithm and optionally releases the managed resources.

(Inherited from SymmetricAlgorithm)

適用於

另請參閱