DESCryptoServiceProvider 类 [C#]请参见 DESCryptoServiceProvider 成员 | System.Security.Cryptography 命名空间 要求 命名空间: System.Security.Cryptography平台: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows .NET Server family程序集: Mscorlib (在 Mscorlib.dll 中) 语言 C#C++JScriptVisual Basic全部显示 定义访问数据加密标准 (DES) 算法的加密服务提供程序 (CSP) 版本的包装对象。无法继承此类。有关此类型所有成员的列表,请参阅 DESCryptoServiceProvider 成员。System.Object System.Security.Cryptography.SymmetricAlgorithm System.Security.Cryptography.DES System.Security.Cryptography.DESCryptoServiceProvider[Visual Basic] NotInheritable Public Class DESCryptoServiceProvider Inherits DES [C#] public sealed class DESCryptoServiceProvider : DES [C++] public __gc __sealed class DESCryptoServiceProvider : public DES [JScript] public class DESCryptoServiceProvider extends DES 线程安全 此类型的所有公共静态(Visual Basic 中为 Shared)成员对多线程操作而言都是安全的。但不保证任何实例成员是线程安全的。示例 [Visual Basic, C#] 下面的示例方法使用带有指定的 Key 和初始化向量(IV)的 DESCryptoServiceProvider 来加密由 inName 指定的文件,并将加密结果输出到由 outName 指定的文件。[Visual Basic] 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 = 8 '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 [C#] 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(); } [Visual Basic, C#] 可以相同的方法进行解密;使用 CreateDecryptor 而不是 CreateEncryptor。必须使用加密该文件所用的同一 Key 和 IV 进行解密。[C++, JScript] 没有可用于 C++ 或 JScript 的示例。若要查看 Visual Basic 或 C# 示例,请单击页左上角的语言筛选器按钮 。要求 命名空间: System.Security.Cryptography平台: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows .NET Server family程序集: Mscorlib (在 Mscorlib.dll 中)
DESCryptoServiceProvider 成员 | System.Security.Cryptography 命名空间
要求
命名空间: System.Security.Cryptography平台: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows .NET Server family程序集: Mscorlib (在 Mscorlib.dll 中)
语言
C#C++JScriptVisual Basic全部显示
定义访问数据加密标准 (DES) 算法的加密服务提供程序 (CSP) 版本的包装对象。无法继承此类。有关此类型所有成员的列表,请参阅 DESCryptoServiceProvider 成员。System.Object
System.Security.Cryptography.SymmetricAlgorithm
System.Security.Cryptography.DES
System.Security.Cryptography.DESCryptoServiceProvider[Visual Basic]
NotInheritable Public Class DESCryptoServiceProvider
Inherits DES
[C#]
public sealed class DESCryptoServiceProvider : DES
[C++]
public __gc __sealed class DESCryptoServiceProvider : public DES
[JScript]
public class DESCryptoServiceProvider extends DES
线程安全
此类型的所有公共静态(Visual Basic 中为 Shared)成员对多线程操作而言都是安全的。但不保证任何实例成员是线程安全的。示例
[Visual Basic, C#] 下面的示例方法使用带有指定的 Key 和初始化向量(IV)的 DESCryptoServiceProvider 来加密由 inName 指定的文件,并将加密结果输出到由 outName 指定的文件。[Visual Basic]
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 = 8 '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
[C#]
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();
}
[Visual Basic, C#] 可以相同的方法进行解密;使用 CreateDecryptor 而不是 CreateEncryptor。必须使用加密该文件所用的同一 Key 和 IV 进行解密。[C++, JScript] 没有可用于 C++ 或 JScript 的示例。若要查看 Visual Basic 或 C# 示例,请单击页左上角的语言筛选器按钮 。要求
命名空间: System.Security.Cryptography平台: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows .NET Server family程序集: Mscorlib (在 Mscorlib.dll 中)