SymmetricAlgorithm 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示所有对称算法的实现都必须从中继承的抽象基类。
public ref class SymmetricAlgorithm abstract : IDisposable
	public abstract class SymmetricAlgorithm : IDisposable
	[System.Runtime.InteropServices.ComVisible(true)]
public abstract class SymmetricAlgorithm : IDisposable
	type SymmetricAlgorithm = class
    interface IDisposable
	[<System.Runtime.InteropServices.ComVisible(true)>]
type SymmetricAlgorithm = class
    interface IDisposable
	Public MustInherit Class SymmetricAlgorithm
Implements IDisposable
		- 继承
 - 
				SymmetricAlgorithm
 
- 派生
 
- 属性
 
- 实现
 
示例
下面的代码示例使用 Aes 具有指定 Key 属性的 类和初始化向量 (IV) 来加密 指定的 inName文件,并将加密结果输出到 指定的 outName文件。 
              desKey方法的 和 desIV 参数是 8 字节数组。 必须安装高加密包才能运行此示例。
private static void EncryptData(string inName, string outName, byte[] aesKey, byte[] aesIV)
 {
     //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.
     Aes aes = Aes.Create();
     CryptoStream encStream = new CryptoStream(fout, aes.CreateEncryptor(aesKey, aesIV), 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, _
rijnKey() As Byte, rijnIV() 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 'Total length of the input file.
    Dim len As Integer 'This is the number of bytes to be written at a time.
    'Creates the default implementation, which is RijndaelManaged.
    Dim rijn As SymmetricAlgorithm = SymmetricAlgorithm.Create()
    Dim encStream As New CryptoStream(fout, _
       rijn.CreateEncryptor(rijnKey, rijnIV), 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 = Convert.ToInt32(rdlen + len)
        Console.WriteLine("{0} bytes processed", rdlen)
    End While
    
    encStream.Close()
fout.Close()
fin.Close()
End Sub
	注解
从 SymmetricAlgorithm 类派生的类使用称为密码块链接 (CBC) 的链接模式,该模式需要密钥 (Key) 和初始化向量 (IV) 才能对数据执行加密转换。 若要解密使用其中一个SymmetricAlgorithm类加密的数据,必须将 属性和 IV 属性设置为Key用于加密的相同值。 要使对称算法有用,密钥必须只有发送方和接收方知道。
Aes、 DES、 RC2和 TripleDES 是对称算法的实现。
请注意,使用派生类时,从安全角度来看,仅仅在对象使用完后强制进行垃圾回收是不够的。 必须在对象上显式调用 Clear 方法,以在释放对象之前将对象中的任何敏感数据归零。 请注意,垃圾回收不会将收集的对象的内容归零,而只是将内存标记为可用于重新分配。 因此,垃圾回收对象中包含的数据可能仍存在于未分配内存的内存堆中。 对于加密对象,此数据可能包含敏感信息,例如密钥数据或纯文本块。
.NET Framework 中保存敏感数据的所有加密类都实现方法 Clear 。 调用 时, Clear 方法会用零覆盖 对象中的所有敏感数据,然后释放对象,以便可以安全地对其进行垃圾回收。 将对象归零并释放后,应调用 Dispose 参数设置为 True 的方法disposing,以释放与对象关联的所有托管和非托管资源。
实施者说明
从 SymmetricAlgorithm 类继承时,必须重写以下成员: CreateDecryptor(Byte[], Byte[])、 CreateEncryptor(Byte[], Byte[])、 GenerateIV()和 GenerateKey()。
构造函数
| SymmetricAlgorithm() | 
		 初始化 SymmetricAlgorithm 类的新实例。  | 
        	
字段
| BlockSizeValue | 
		 表示加密操作的块大小(以位为单位)。  | 
        	
| FeedbackSizeValue | 
		 表示加密操作的反馈大小(以位为单位)。  | 
        	
| IVValue | 
		 表示对称算法的初始化向量 (IV)。  | 
        	
| KeySizeValue | 
		 表示对称算法使用的密钥的大小(以位为单位)。  | 
        	
| KeyValue | 
		 表示对称算法的密钥。  | 
        	
| LegalBlockSizesValue | 
		 指定对称算法支持的块大小(以位为单位)。  | 
        	
| LegalKeySizesValue | 
		 指定对称算法支持的密钥大小(以位为单位)。  | 
        	
| ModeValue | 
		 表示对称算法中使用的密码模式。  | 
        	
| PaddingValue | 
		 表示对称算法中使用的填充模式。  | 
        	
属性
| BlockSize | 
		 获取或设置加密操作的块大小(以位为单位)。  | 
        	
| FeedbackSize | 
		 获取或设置针对密码反馈 (CFB) 和输出反馈 (OFB) 密码模式的加密操作的反馈大小(以位为单位)。  | 
        	
| IV | 
		 获取或设置对称算法的初始化向量 (IV)。  | 
        	
| Key | 
		 获取或设置对称算法的密钥。  | 
        	
| KeySize | 
		 获取或设置对称算法所用密钥的大小(以位为单位)。  | 
        	
| LegalBlockSizes | 
		 获取对称算法支持的块大小(以位为单位)。  | 
        	
| LegalKeySizes | 
		 获取对称算法支持的密钥大小(以位为单位)。  | 
        	
| Mode | 
		 获取或设置对称算法的运算模式。  | 
        	
| Padding | 
		 获取或设置对称算法中使用的填充模式。  | 
        	
方法
显式接口实现
| IDisposable.Dispose() | 
			 此 API 支持产品基础结构,不能在代码中直接使用。 释放由 SymmetricAlgorithm 占用的非托管资源,还可以另外再释放托管资源。  |