TripleDES TripleDES TripleDES TripleDES Class

定义

表示三重数据加密标准算法的基类,TripleDES 的所有实现都必须从此基类派生。Represents the base class for Triple Data Encryption Standard algorithms from which all TripleDES implementations must derive.

public ref class TripleDES abstract : System::Security::Cryptography::SymmetricAlgorithm
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class TripleDES : System.Security.Cryptography.SymmetricAlgorithm
type TripleDES = class
    inherit SymmetricAlgorithm
Public MustInherit Class TripleDES
Inherits SymmetricAlgorithm
继承
派生
属性

示例

下面的代码示例方法使用TripleDESCryptoServiceProvider与指定的键 (Key) 和初始化向量 (IV) 来加密指定的文件inNameThe following code example method uses TripleDESCryptoServiceProvider 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>^tdesKey, array<Byte>^tdesIV )
{
   
   //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.

   TripleDESCryptoServiceProvider^ tdes = gcnew TripleDESCryptoServiceProvider;
   CryptoStream^ encStream = gcnew CryptoStream( fout,tdes->CreateEncryptor( tdesKey, tdesIV ),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();
}

private static void EncryptData(String inName, String outName, byte[] tdesKey, byte[] tdesIV)
{    
    //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.

    TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();          
    CryptoStream encStream = new CryptoStream(fout, tdes.CreateEncryptor(tdesKey, tdesIV), 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();                     
}
Private Shared Sub EncryptData(inName As String, outName As String, _
   tdesKey() As Byte, tdesIV() 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(100) 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 'This is the total length of the input file.
    Dim len As Integer 'This is the number of bytes to be written at a time.
    Dim tdes As New TripleDESCryptoServiceProvider()
    Dim encStream As New CryptoStream(fout, _
       tdes.CreateEncryptor(tdesKey, tdesIV), 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)
    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.

注解

TripleDES 使用的三个连续迭代DES算法。TripleDES uses three successive iterations of the DES algorithm. 它可以使用两个或三个 56 位密钥。It can use either two or three 56-bit keys.

备注

较新的对称加密算法,高级加密标准 (AES),这是可用。A newer symmetric encryption algorithm, Advanced Encryption Standard (AES), is available. 请考虑使用Aes类,而不是其派生的类TripleDES类。Consider using the Aes class and its derived classes instead of the TripleDES class. 使用TripleDES仅为与旧版应用程序和数据兼容。Use TripleDES only for compatibility with legacy applications and data.

此算法支持 64 位的增量为 192 位密钥长度 128 位。This algorithm supports key lengths from 128 bits to 192 bits in increments of 64 bits.

构造函数

TripleDES() TripleDES() TripleDES() TripleDES()

初始化 TripleDES 类的新实例。Initializes a new instance of the TripleDES class.

属性

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

获取或设置 TripleDES 算法的机密密钥。Gets or sets the secret key for the TripleDES algorithm.

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

方法

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

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

(Inherited from SymmetricAlgorithm)
Create() Create() Create() Create()

创建加密对象的实例以执行 TripleDES 算法。Creates an instance of a cryptographic object to perform the TripleDES algorithm.

Create(String) Create(String) Create(String) Create(String)

创建加密对象的实例以执行 TripleDES 算法的指定实现。Creates an instance of a cryptographic object to perform the specified implementation of the TripleDES algorithm.

CreateDecryptor() CreateDecryptor() CreateDecryptor() CreateDecryptor()

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

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

当在派生类中重写时,用指定的 Key 属性和初始化向量 (IV) 创建对称解密器对象。When overridden in a derived class, creates a symmetric decryptor object with the specified Key property and initialization vector (IV).

(Inherited from SymmetricAlgorithm)
CreateEncryptor() CreateEncryptor() CreateEncryptor() CreateEncryptor()

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

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

当在派生类中重写时,用指定的 Key 属性和初始化向量 (IV) 创建对称加密器对象。When overridden in a derived class, creates a symmetric encryptor object with the specified Key property and initialization vector (IV).

(Inherited from SymmetricAlgorithm)
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 占用的非托管资源,还可以另外再释放托管资源。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)。When overridden in a derived class, generates a random initialization vector (IV) to use for the algorithm.

(Inherited from SymmetricAlgorithm)
GenerateKey() GenerateKey() GenerateKey() GenerateKey()

当在派生类中重写时,生成用于该算法的随机密钥 (Key)。When overridden in a derived class, generates a random key (Key) to use for the algorithm.

(Inherited from SymmetricAlgorithm)
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)
IsWeakKey(Byte[]) IsWeakKey(Byte[]) IsWeakKey(Byte[]) IsWeakKey(Byte[])

确定指定的密钥是否为弱密钥。Determines whether the specified key is weak.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。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)

字段

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 占用的非托管资源,还可以另外再释放托管资源。Releases the unmanaged resources used by the SymmetricAlgorithm and optionally releases the managed resources.

(Inherited from SymmetricAlgorithm)

适用于

另请参阅