DESCryptoServiceProvider 类

定义

定义访问数据加密标准 (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
继承
DESCryptoServiceProvider
属性

示例

下面的代码示例使用具有指定键(Key)和初始化向量(IV)的 DESCryptoServiceProviderDES),对 inName指定的文件进行加密。The 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. 然后,它会将加密的结果输出到 outName指定的文件。It 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 类的新实例。Initializes a new instance of the DESCryptoServiceProvider class.

字段

BlockSizeValue

表示加密操作的块大小(以位为单位)。Represents the block size, in bits, of the cryptographic operation.

(继承自 SymmetricAlgorithm)
FeedbackSizeValue

表示加密操作的反馈大小(以位为单位)。Represents the feedback size, in bits, of the cryptographic operation.

(继承自 SymmetricAlgorithm)
IVValue

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

(继承自 SymmetricAlgorithm)
KeySizeValue

表示对称算法使用的密钥的大小(以位为单位)。Represents the size, in bits, of the secret key used by the symmetric algorithm.

(继承自 SymmetricAlgorithm)
KeyValue

表示对称算法的密钥。Represents the secret key for the symmetric algorithm.

(继承自 SymmetricAlgorithm)
LegalBlockSizesValue

指定对称算法支持的块大小(以位为单位)。Specifies the block sizes, in bits, that are supported by the symmetric algorithm.

(继承自 SymmetricAlgorithm)
LegalKeySizesValue

指定对称算法支持的密钥大小(以位为单位)。Specifies the key sizes, in bits, that are supported by the symmetric algorithm.

(继承自 SymmetricAlgorithm)
ModeValue

表示对称算法中使用的密码模式。Represents the cipher mode used in the symmetric algorithm.

(继承自 SymmetricAlgorithm)
PaddingValue

表示对称算法中使用的填充模式。Represents the padding mode used in the symmetric algorithm.

(继承自 SymmetricAlgorithm)

属性

BlockSize

获取或设置加密操作的块大小(以位为单位)。Gets or sets the block size, in bits, of the cryptographic operation.

(继承自 SymmetricAlgorithm)
FeedbackSize

获取或设置加密操作的反馈大小(以位为单位)。Gets or sets the feedback size, in bits, of the cryptographic operation.

(继承自 SymmetricAlgorithm)
IV

获取或设置对称算法的初始化向量 (IV)。Gets or sets the initialization vector (IV) for the symmetric algorithm.

(继承自 SymmetricAlgorithm)
Key

获取或设置数据加密标准 (DES) 算法的机密密钥。Gets or sets the secret key for the Data Encryption Standard (DES) algorithm.

(继承自 DES)
KeySize

获取或设置对称算法所用密钥的大小(以位为单位)。Gets or sets the size, in bits, of the secret key used by the symmetric algorithm.

(继承自 SymmetricAlgorithm)
LegalBlockSizes

获取对称算法支持的块大小(以位为单位)。Gets the block sizes, in bits, that are supported by the symmetric algorithm.

(继承自 SymmetricAlgorithm)
LegalKeySizes

获取对称算法支持的密钥大小(以位为单位)。Gets the key sizes, in bits, that are supported by the symmetric algorithm.

(继承自 SymmetricAlgorithm)
Mode

获取或设置对称算法的运算模式。Gets or sets the mode for operation of the symmetric algorithm.

(继承自 SymmetricAlgorithm)
Padding

获取或设置对称算法中使用的填充模式。Gets or sets the padding mode used in the symmetric algorithm.

(继承自 SymmetricAlgorithm)

方法

Clear()

释放 SymmetricAlgorithm 类使用的所有资源。Releases all resources used by the SymmetricAlgorithm class.

(继承自 SymmetricAlgorithm)
CreateDecryptor()

用当前的 Key 属性和初始化向量 (IV) 创建对称解密器对象。Creates a symmetric decryptor object with the current Key property and initialization vector (IV).

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()

用当前的 Key 属性和初始化向量 (IV) 创建对称加密器对象。Creates a symmetric encryptor object with the current Key property and initialization vector (IV).

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()

释放 SymmetricAlgorithm 类的当前实例所使用的所有资源。Releases all resources used by the current instance of the SymmetricAlgorithm class.

(继承自 SymmetricAlgorithm)
Dispose(Boolean)

释放由 SymmetricAlgorithm 占用的非托管资源,还可以另外再释放托管资源。Releases the unmanaged resources used by the SymmetricAlgorithm and optionally releases the managed resources.

(继承自 SymmetricAlgorithm)
Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GenerateIV()

生成用于该算法的随机初始化向量 (IV)。Generates a random initialization vector (IV) to use for the algorithm.

GenerateKey()

生成用于该算法的随机密钥 (Key)。Generates a random key (Key) to be used for the algorithm.

GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)
ValidKeySize(Int32)

确定指定的密钥大小对当前算法是否有效。Determines whether the specified key size is valid for the current algorithm.

(继承自 SymmetricAlgorithm)

显式界面实现

IDisposable.Dispose()

释放由 SymmetricAlgorithm 占用的非托管资源,还可以另外再释放托管资源。Releases the unmanaged resources used by the SymmetricAlgorithm and optionally releases the managed resources.

(继承自 SymmetricAlgorithm)

适用于

另请参阅