DESCryptoServiceProvider クラス
アセンブリ: mscorlib (mscorlib.dll 内)



次に示す例では、DESCryptoServiceProvider クラスを使用してデータを暗号化した後、そのデータを復号化します。
' This sample demonstrates using a key based on the cryptographic service provider (CSP) version ' of the Data Encryption Standard (DES)algorithm to encrypt a string to a byte array, and then ' to decrypt the byte array back to a string. Imports System Imports System.IO Imports System.Text Imports System.Security.Cryptography Class CryptoMemoryStream ' Main method. Public Shared Sub Main() ' Create a new DES key. Dim key As New DESCryptoServiceProvider() ' Encrypt a string to a byte array. Dim buffer As Byte() = Encrypt("This is some plaintext!", key) ' Decrypt the byte array back to a string. Dim plaintext As String = Decrypt(buffer, key) ' Display the plaintext value to the console. Console.WriteLine(plaintext) End Sub 'Main ' Encrypt the string. Public Shared Function Encrypt(PlainText As String, key As SymmetricAlgorithm) As Byte() ' Create a memory stream. Dim ms As New MemoryStream() ' Create a CryptoStream using the memory stream and the ' CSP DES key. Dim encStream As New CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write) ' Create a StreamWriter to write a string ' to the stream. Dim sw As New StreamWriter(encStream) ' Write the plaintext to the stream. sw.WriteLine(PlainText) ' Close the StreamWriter and CryptoStream. sw.Close() encStream.Close() ' Get an array of bytes that represents ' the memory stream. Dim buffer As Byte() = ms.ToArray() ' Close the memory stream. ms.Close() ' Return the encrypted byte array. Return buffer End Function 'Encrypt ' Decrypt the byte array. Public Shared Function Decrypt(CypherText() As Byte, key As SymmetricAlgorithm) As String ' Create a memory stream to the passed buffer. Dim ms As New MemoryStream(CypherText) ' Create a CryptoStream using the memory stream and the ' CSP DES key. Dim encStream As New CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read) ' Create a StreamReader for reading the stream. Dim sr As New StreamReader(encStream) ' Read the stream as a string. Dim val As String = sr.ReadLine() ' Close the streams. sr.Close() encStream.Close() ms.Close() Return val End Function 'Decrypt End Class 'CryptoMemoryStream
// This sample demonstrates using a key based on the cryptographic service provider (CSP) version // of the Data Encryption Standard (DES)algorithm to encrypt a string to a byte array, and then // to decrypt the byte array back to a string. using System; using System.IO; using System.Text; using System.Security.Cryptography; class CryptoMemoryStream { // Main method. public static void Main() { // Create a new DES key. DESCryptoServiceProvider key = new DESCryptoServiceProvider(); // Encrypt a string to a byte array. byte[] buffer = Encrypt("This is some plaintext!", key); // Decrypt the byte array back to a string. string plaintext = Decrypt(buffer, key); // Display the plaintext value to the console. Console.WriteLine(plaintext); } // Encrypt the string. public static byte[] Encrypt(string PlainText, SymmetricAlgorithm key) { // Create a memory stream. MemoryStream ms = new MemoryStream(); // Create a CryptoStream using the memory stream and the // CSP DES key. CryptoStream encStream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write); // Create a StreamWriter to write a string // to the stream. StreamWriter sw = new StreamWriter(encStream); // Write the plaintext to the stream. sw.WriteLine(PlainText); // Close the StreamWriter and CryptoStream. sw.Close(); encStream.Close(); // Get an array of bytes that represents // the memory stream. byte[] buffer = ms.ToArray(); // Close the memory stream. ms.Close(); // Return the encrypted byte array. return buffer; } // Decrypt the byte array. public static string Decrypt(byte[] CypherText, SymmetricAlgorithm key) { // Create a memory stream to the passed buffer. MemoryStream ms = new MemoryStream(CypherText); // Create a CryptoStream using the memory stream and the // CSP DES key. CryptoStream encStream = new CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read); // Create a StreamReader for reading the stream. StreamReader sr = new StreamReader(encStream); // Read the stream as a string. string val = sr.ReadLine(); // Close the streams. sr.Close(); encStream.Close(); ms.Close(); return val; } }
// This sample demonstrates using a key based on the cryptographic service provider (CSP) version // of the Data Encryption Standard (DES)algorithm to encrypt a string to a byte array, and then // to decrypt the byte array back to a string. using namespace System; using namespace System::IO; using namespace System::Text; using namespace System::Security::Cryptography; // Encrypt the string. array<Byte>^ Encrypt( String^ PlainText, SymmetricAlgorithm^ key ) { // Create a memory stream. MemoryStream^ ms = gcnew MemoryStream; // Create a CryptoStream using the memory stream and the // CSP DES key. CryptoStream^ encStream = gcnew CryptoStream( ms,key->CreateEncryptor(),CryptoStreamMode::Write ); // Create a StreamWriter to write a string // to the stream. StreamWriter^ sw = gcnew StreamWriter( encStream ); // Write the plaintext to the stream. sw->WriteLine( PlainText ); // Close the StreamWriter and CryptoStream. sw->Close(); encStream->Close(); // Get an array of bytes that represents // the memory stream. array<Byte>^buffer = ms->ToArray(); // Close the memory stream. ms->Close(); // Return the encrypted byte array. return buffer; } // Decrypt the byte array. String^ Decrypt( array<Byte>^CypherText, SymmetricAlgorithm^ key ) { // Create a memory stream to the passed buffer. MemoryStream^ ms = gcnew MemoryStream( CypherText ); // Create a CryptoStream using the memory stream and the // CSP DES key. CryptoStream^ encStream = gcnew CryptoStream( ms,key->CreateDecryptor(),CryptoStreamMode::Read ); // Create a StreamReader for reading the stream. StreamReader^ sr = gcnew StreamReader( encStream ); // Read the stream as a string. String^ val = sr->ReadLine(); // Close the streams. sr->Close(); encStream->Close(); ms->Close(); return val; } int main() { // Create a new DES key. DESCryptoServiceProvider^ key = gcnew DESCryptoServiceProvider; // Encrypt a string to a byte array. array<Byte>^buffer = Encrypt( "This is some plaintext!", key ); // Decrypt the byte array back to a string. String^ plaintext = Decrypt( buffer, key ); // Display the plaintext value to the console. Console::WriteLine( plaintext ); }
// This sample demonstrates using a key based on the cryptographic // service provider (CSP) version // of the Data Encryption Standard (DES)algorithm to encrypt a string // to a byte array, and then to decrypt the byte array back to a string. import System.*; import System.IO.*; import System.Text.*; import System.Security.Cryptography.*; class CryptoMemoryStream { // main method. public static void main(String[] args) { // Create a new DES key. DESCryptoServiceProvider key = new DESCryptoServiceProvider(); // Encrypt a string to a byte array. ubyte buffer[] = Encrypt("This is some plaintext!", key); // Decrypt the byte array back to a string. String plainText = Decrypt(buffer, key); // Display the plaintext value to the console. Console.WriteLine(plainText); } //main // Encrypt the string. public static ubyte[] Encrypt(String plainText, SymmetricAlgorithm key) { // Create a memory stream. MemoryStream ms = new MemoryStream(); // Create a CryptoStream using the memory stream and the // CSP DES key. CryptoStream encStream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write); // Create a StreamWriter to write a string // to the stream. StreamWriter sw = new StreamWriter(encStream); // Write the plaintext to the stream. sw.WriteLine(plainText); // Close the StreamWriter and CryptoStream. sw.Close(); encStream.Close(); // Get an array of bytes that represents // the memory stream. ubyte buffer[] = ms.ToArray(); // Close the memory stream. ms.Close(); // Return the encrypted byte array. return buffer; } //Encrypt // Decrypt the byte array. public static String Decrypt(ubyte cypherText[], SymmetricAlgorithm key) { // Create a memory stream to the passed buffer. MemoryStream ms = new MemoryStream(cypherText); // Create a CryptoStream using the memory stream and the // CSP DES key. CryptoStream encStream = new CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read); // Create a StreamReader for reading the stream. StreamReader sr = new StreamReader(encStream); // Read the stream as a string. String val = sr.ReadLine(); // Close the streams. sr.Close(); encStream.Close(); ms.Close(); return val; } //Decrypt } //CryptoMemoryStream

System.Security.Cryptography.SymmetricAlgorithm
System.Security.Cryptography.DES
System.Security.Cryptography.DESCryptoServiceProvider


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


DESCryptoServiceProvider コンストラクタ
アセンブリ: mscorlib (mscorlib.dll 内)


例外の種類 | 条件 |
---|---|
CryptographicException | DES (Data Encryption Standard) 暗号サービス プロバイダは使用できません。 |

次に示す例では、DESCryptoServiceProvider クラスを使用してデータを暗号化した後、そのデータを復号化します。
' This sample demonstrates using a key based on the cryptographic service provider (CSP) version ' of the Data Encryption Standard (DES)algorithm to encrypt a string to a byte array, and then ' to decrypt the byte array back to a string. Imports System Imports System.IO Imports System.Text Imports System.Security.Cryptography Class CryptoMemoryStream ' Main method. Public Shared Sub Main() ' Create a new DES key. Dim key As New DESCryptoServiceProvider() ' Encrypt a string to a byte array. Dim buffer As Byte() = Encrypt("This is some plaintext!", key) ' Decrypt the byte array back to a string. Dim plaintext As String = Decrypt(buffer, key) ' Display the plaintext value to the console. Console.WriteLine(plaintext) End Sub 'Main ' Encrypt the string. Public Shared Function Encrypt(PlainText As String, key As SymmetricAlgorithm) As Byte() ' Create a memory stream. Dim ms As New MemoryStream() ' Create a CryptoStream using the memory stream and the ' CSP DES key. Dim encStream As New CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write) ' Create a StreamWriter to write a string ' to the stream. Dim sw As New StreamWriter(encStream) ' Write the plaintext to the stream. sw.WriteLine(PlainText) ' Close the StreamWriter and CryptoStream. sw.Close() encStream.Close() ' Get an array of bytes that represents ' the memory stream. Dim buffer As Byte() = ms.ToArray() ' Close the memory stream. ms.Close() ' Return the encrypted byte array. Return buffer End Function 'Encrypt ' Decrypt the byte array. Public Shared Function Decrypt(CypherText() As Byte, key As SymmetricAlgorithm) As String ' Create a memory stream to the passed buffer. Dim ms As New MemoryStream(CypherText) ' Create a CryptoStream using the memory stream and the ' CSP DES key. Dim encStream As New CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read) ' Create a StreamReader for reading the stream. Dim sr As New StreamReader(encStream) ' Read the stream as a string. Dim val As String = sr.ReadLine() ' Close the streams. sr.Close() encStream.Close() ms.Close() Return val End Function 'Decrypt End Class 'CryptoMemoryStream
// This sample demonstrates using a key based on the cryptographic service provider (CSP) version // of the Data Encryption Standard (DES)algorithm to encrypt a string to a byte array, and then // to decrypt the byte array back to a string. using System; using System.IO; using System.Text; using System.Security.Cryptography; class CryptoMemoryStream { // Main method. public static void Main() { // Create a new DES key. DESCryptoServiceProvider key = new DESCryptoServiceProvider(); // Encrypt a string to a byte array. byte[] buffer = Encrypt("This is some plaintext!", key); // Decrypt the byte array back to a string. string plaintext = Decrypt(buffer, key); // Display the plaintext value to the console. Console.WriteLine(plaintext); } // Encrypt the string. public static byte[] Encrypt(string PlainText, SymmetricAlgorithm key) { // Create a memory stream. MemoryStream ms = new MemoryStream(); // Create a CryptoStream using the memory stream and the // CSP DES key. CryptoStream encStream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write); // Create a StreamWriter to write a string // to the stream. StreamWriter sw = new StreamWriter(encStream); // Write the plaintext to the stream. sw.WriteLine(PlainText); // Close the StreamWriter and CryptoStream. sw.Close(); encStream.Close(); // Get an array of bytes that represents // the memory stream. byte[] buffer = ms.ToArray(); // Close the memory stream. ms.Close(); // Return the encrypted byte array. return buffer; } // Decrypt the byte array. public static string Decrypt(byte[] CypherText, SymmetricAlgorithm key) { // Create a memory stream to the passed buffer. MemoryStream ms = new MemoryStream(CypherText); // Create a CryptoStream using the memory stream and the // CSP DES key. CryptoStream encStream = new CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read); // Create a StreamReader for reading the stream. StreamReader sr = new StreamReader(encStream); // Read the stream as a string. string val = sr.ReadLine(); // Close the streams. sr.Close(); encStream.Close(); ms.Close(); return val; } }
// This sample demonstrates using a key based on the cryptographic service provider (CSP) version // of the Data Encryption Standard (DES)algorithm to encrypt a string to a byte array, and then // to decrypt the byte array back to a string. using namespace System; using namespace System::IO; using namespace System::Text; using namespace System::Security::Cryptography; // Encrypt the string. array<Byte>^ Encrypt( String^ PlainText, SymmetricAlgorithm^ key ) { // Create a memory stream. MemoryStream^ ms = gcnew MemoryStream; // Create a CryptoStream using the memory stream and the // CSP DES key. CryptoStream^ encStream = gcnew CryptoStream( ms,key->CreateEncryptor(),CryptoStreamMode::Write ); // Create a StreamWriter to write a string // to the stream. StreamWriter^ sw = gcnew StreamWriter( encStream ); // Write the plaintext to the stream. sw->WriteLine( PlainText ); // Close the StreamWriter and CryptoStream. sw->Close(); encStream->Close(); // Get an array of bytes that represents // the memory stream. array<Byte>^buffer = ms->ToArray(); // Close the memory stream. ms->Close(); // Return the encrypted byte array. return buffer; } // Decrypt the byte array. String^ Decrypt( array<Byte>^CypherText, SymmetricAlgorithm^ key ) { // Create a memory stream to the passed buffer. MemoryStream^ ms = gcnew MemoryStream( CypherText ); // Create a CryptoStream using the memory stream and the // CSP DES key. CryptoStream^ encStream = gcnew CryptoStream( ms,key->CreateDecryptor(),CryptoStreamMode::Read ); // Create a StreamReader for reading the stream. StreamReader^ sr = gcnew StreamReader( encStream ); // Read the stream as a string. String^ val = sr->ReadLine(); // Close the streams. sr->Close(); encStream->Close(); ms->Close(); return val; } int main() { // Create a new DES key. DESCryptoServiceProvider^ key = gcnew DESCryptoServiceProvider; // Encrypt a string to a byte array. array<Byte>^buffer = Encrypt( "This is some plaintext!", key ); // Decrypt the byte array back to a string. String^ plaintext = Decrypt( buffer, key ); // Display the plaintext value to the console. Console::WriteLine( plaintext ); }
// This sample demonstrates using a key based on the cryptographic // service provider (CSP) version // of the Data Encryption Standard (DES)algorithm to encrypt a string // to a byte array, and then to decrypt the byte array back to a string. import System.*; import System.IO.*; import System.Text.*; import System.Security.Cryptography.*; class CryptoMemoryStream { // main method. public static void main(String[] args) { // Create a new DES key. DESCryptoServiceProvider key = new DESCryptoServiceProvider(); // Encrypt a string to a byte array. ubyte buffer[] = Encrypt("This is some plaintext!", key); // Decrypt the byte array back to a string. String plainText = Decrypt(buffer, key); // Display the plaintext value to the console. Console.WriteLine(plainText); } //main // Encrypt the string. public static ubyte[] Encrypt(String plainText, SymmetricAlgorithm key) { // Create a memory stream. MemoryStream ms = new MemoryStream(); // Create a CryptoStream using the memory stream and the // CSP DES key. CryptoStream encStream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write); // Create a StreamWriter to write a string // to the stream. StreamWriter sw = new StreamWriter(encStream); // Write the plaintext to the stream. sw.WriteLine(plainText); // Close the StreamWriter and CryptoStream. sw.Close(); encStream.Close(); // Get an array of bytes that represents // the memory stream. ubyte buffer[] = ms.ToArray(); // Close the memory stream. ms.Close(); // Return the encrypted byte array. return buffer; } //Encrypt // Decrypt the byte array. public static String Decrypt(ubyte cypherText[], SymmetricAlgorithm key) { // Create a memory stream to the passed buffer. MemoryStream ms = new MemoryStream(cypherText); // Create a CryptoStream using the memory stream and the // CSP DES key. CryptoStream encStream = new CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read); // Create a StreamReader for reading the stream. StreamReader sr = new StreamReader(encStream); // Read the stream as a string. String val = sr.ReadLine(); // Close the streams. sr.Close(); encStream.Close(); ms.Close(); return val; } //Decrypt } //CryptoMemoryStream

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


DESCryptoServiceProvider プロパティ

名前 | 説明 | |
---|---|---|
![]() | BlockSize | 暗号操作のブロック サイズをビット単位で取得または設定します。 ( SymmetricAlgorithm から継承されます。) |
![]() | FeedbackSize | 暗号操作のフィードバック サイズをビット単位で取得または設定します。 ( SymmetricAlgorithm から継承されます。) |
![]() | IV | 対称アルゴリズムの初期化ベクタ (IV) を取得または設定します。 ( SymmetricAlgorithm から継承されます。) |
![]() | Key | DES (Data Encryption Standard) アルゴリズムの共有キーを取得または設定します。 ( DES から継承されます。) |
![]() | KeySize | 対称アルゴリズムで使用する共有キーのサイズをビット単位で取得または設定します。 ( SymmetricAlgorithm から継承されます。) |
![]() | LegalBlockSizes | 対称アルゴリズムでサポートされているブロック サイズを取得します。 ( SymmetricAlgorithm から継承されます。) |
![]() | LegalKeySizes | 対称アルゴリズムでサポートされているキー サイズを取得します。 ( SymmetricAlgorithm から継承されます。) |
![]() | Mode | 対称アルゴリズムの操作モードを取得または設定します。 ( SymmetricAlgorithm から継承されます。) |
![]() | Padding | 対称アルゴリズムで使用する埋め込みモードを取得または設定します。 ( SymmetricAlgorithm から継承されます。) |

DESCryptoServiceProvider メソッド


DESCryptoServiceProvider メンバ
DES (Data Encryption Standard) アルゴリズムの暗号サービス プロバイダ (CSP : Cryptographic Service Provider) バージョンにアクセスする、ラッパー オブジェクトを定義します。このクラスは継承できません。
DESCryptoServiceProvider データ型で公開されるメンバを以下の表に示します。


名前 | 説明 | |
---|---|---|
![]() | BlockSize | 暗号操作のブロック サイズをビット単位で取得または設定します。(SymmetricAlgorithm から継承されます。) |
![]() | FeedbackSize | 暗号操作のフィードバック サイズをビット単位で取得または設定します。(SymmetricAlgorithm から継承されます。) |
![]() | IV | 対称アルゴリズムの初期化ベクタ (IV) を取得または設定します。(SymmetricAlgorithm から継承されます。) |
![]() | Key | DES (Data Encryption Standard) アルゴリズムの共有キーを取得または設定します。(DES から継承されます。) |
![]() | KeySize | 対称アルゴリズムで使用する共有キーのサイズをビット単位で取得または設定します。(SymmetricAlgorithm から継承されます。) |
![]() | LegalBlockSizes | 対称アルゴリズムでサポートされているブロック サイズを取得します。(SymmetricAlgorithm から継承されます。) |
![]() | LegalKeySizes | 対称アルゴリズムでサポートされているキー サイズを取得します。(SymmetricAlgorithm から継承されます。) |
![]() | Mode | 対称アルゴリズムの操作モードを取得または設定します。(SymmetricAlgorithm から継承されます。) |
![]() | Padding | 対称アルゴリズムで使用する埋め込みモードを取得または設定します。(SymmetricAlgorithm から継承されます。) |


- DESCryptoServiceProviderのページへのリンク