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

<ComVisibleAttribute(True)> _ Public Class CryptoStream Inherits Stream Implements IDisposable

共通言語ランタイムは、ストリーム指向の暗号デザインを採用しています。このデザインの中核となっているのが CryptoStream です。CryptoStream を実装するすべての暗号オブジェクトは、Stream を実装する任意のオブジェクトと一緒にチェインにまとめることができるため、あるオブジェクトからの出力をストリーム経由で別のオブジェクトの入力に転送できます。このとき、中間結果 (最初のオブジェクトからの出力) を別個に格納する必要はありません。
CryptoStream オブジェクトを使用した場合は、必ず Close メソッドを呼び出して明示的に閉じる必要があります。これによりストリームがフラッシュされ、残りのデータ ブロックが CryptoStream オブジェクトによってすべて処理されます。ただし、Close メソッドを呼び出す前に例外が発生した場合、CryptoStream オブジェクトを閉じることができない場合もあります。Close メソッドが常に呼び出されるようにするには、Close メソッド呼び出しを try/catch ステートメントの finally ブロックに記述します。

CryptoStream を使用して文字列を暗号化する方法を次の例に示します。このメソッドでは、RijndaelManaged クラスで Key と初期化ベクタ (IV) が指定されています。
Imports System.Security.Cryptography Imports System.Text Imports System.IO Module RijndaelSample Sub Main() Try ' Create a new Rijndael object to generate a key ' and initialization vector (IV). Dim RijndaelAlg As Rijndael = Rijndael.Create ' Create a string to encrypt. Dim sData As String = "Here is some data to encrypt." Dim FileName As String = "CText.txt" ' Encrypt text to a file using the file name, key, and IV. EncryptTextToFile(sData, FileName, RijndaelAlg.Key, RijndaelAlg.IV) ' Decrypt the text from a file using the file name, key, and IV. Dim Final As String = DecryptTextFromFile(FileName, RijndaelAlg.Key, RijndaelAlg.IV) ' Display the decrypted string to the console. Console.WriteLine(Final) Catch e As Exception Console.WriteLine(e.Message) End Try Console.ReadLine() End Sub Sub EncryptTextToFile(ByVal Data As String, ByVal FileName As String, ByVal Key() As Byte, ByVal IV() As Byte) Try ' Create or open the specified file. Dim fStream As FileStream = File.Open(FileName, FileMode.OpenOrCreate) ' Create a new Rijndael object. Dim RijndaelAlg As Rijndael = Rijndael.Create ' Create a CryptoStream using the FileStream ' and the passed key and initialization vector (IV). Dim cStream As New CryptoStream(fStream, _ RijndaelAlg.CreateEncryptor(Key, IV), _ CryptoStreamMode.Write) ' Create a StreamWriter using the CryptoStream. Dim sWriter As New StreamWriter(cStream) Try ' Write the data to the stream ' to encrypt it. sWriter.WriteLine(Data) Catch e As Exception Console.WriteLine("An error occurred: {0}", e.Message) Finally ' Close the streams and ' close the file. sWriter.Close() cStream.Close() fStream.Close() End Try Catch e As CryptographicException Console.WriteLine("A Cryptographic error occurred: {0}", e.Message) Catch e As UnauthorizedAccessException Console.WriteLine("A file error occurred: {0}", e.Message) End Try End Sub Function DecryptTextFromFile(ByVal FileName As String, ByVal Key() As Byte, ByVal IV() As Byte) As String Try ' Create or open the specified file. Dim fStream As FileStream = File.Open(FileName, FileMode.OpenOrCreate) ' Create a new Rijndael object. Dim RijndaelAlg As Rijndael = Rijndael.Create ' Create a CryptoStream using the FileStream ' and the passed key and initialization vector (IV). Dim cStream As New CryptoStream(fStream, _ RijndaelAlg.CreateDecryptor(Key, IV), _ CryptoStreamMode.Read) ' Create a StreamReader using the CryptoStream. Dim sReader As New StreamReader(cStream) ' Read the data from the stream ' to decrypt it. Dim val As String = Nothing Try val = sReader.ReadLine() Catch e As Exception Console.WriteLine("An Cerror occurred: {0}", e.Message) Finally ' Close the streams and ' close the file. sReader.Close() cStream.Close() fStream.Close() End Try ' Return the string. Return val Catch e As CryptographicException Console.WriteLine("A Cryptographic error occurred: {0}", e.Message) Return Nothing Catch e As UnauthorizedAccessException Console.WriteLine("A file error occurred: {0}", e.Message) Return Nothing End Try End Function End Module
using System; using System.Security.Cryptography; using System.Text; using System.IO; class RijndaelSample { static void Main() { try { // Create a new Rijndael object to generate a key // and initialization vector (IV). Rijndael RijndaelAlg = Rijndael.Create(); // Create a string to encrypt. string sData = "Here is some data to encrypt."; string FileName = "CText.txt"; // Encrypt text to a file using the file name, key, and IV. EncryptTextToFile(sData, FileName, RijndaelAlg.Key, RijndaelAlg.IV); // Decrypt the text from a file using the file name, key, and IV. string Final = DecryptTextFromFile(FileName, RijndaelAlg.Key, RijndaelAlg.IV); // Display the decrypted string to the console. Console.WriteLine(Final); } catch (Exception e) { Console.WriteLine(e.Message); } Console.ReadLine(); } public static void EncryptTextToFile(String Data, String FileName, byte[] Key, byte[] IV) { try { // Create or open the specified file. FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate); // Create a new Rijndael object. Rijndael RijndaelAlg = Rijndael.Create(); // Create a CryptoStream using the FileStream // and the passed key and initialization vector (IV). CryptoStream cStream = new CryptoStream(fStream, RijndaelAlg.CreateEncryptor(Key, IV), CryptoStreamMode.Write); // Create a StreamWriter using the CryptoStream. StreamWriter sWriter = new StreamWriter(cStream); try { // Write the data to the stream // to encrypt it. sWriter.WriteLine(Data); } catch (Exception e) { Console.WriteLine("An error occurred: {0}", e.Message); } finally { // Close the streams and // close the file. sWriter.Close(); cStream.Close(); fStream.Close(); } } catch (CryptographicException e) { Console.WriteLine("A Cryptographic error occurred: {0}", e.Message); } catch (UnauthorizedAccessException e) { Console.WriteLine("A file error occurred: {0}", e.Message); } } public static string DecryptTextFromFile(String FileName, byte[] Key, byte[] IV) { try { // Create or open the specified file. FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate); // Create a new Rijndael object. Rijndael RijndaelAlg = Rijndael.Create(); // Create a CryptoStream using the FileStream // and the passed key and initialization vector (IV). CryptoStream cStream = new CryptoStream(fStream, RijndaelAlg.CreateDecryptor(Key, IV), CryptoStreamMode.Read); // Create a StreamReader using the CryptoStream. StreamReader sReader = new StreamReader(cStream); string val = null; try { // Read the data from the stream // to decrypt it. val = sReader.ReadLine(); } catch (Exception e) { Console.WriteLine("An error occurred: {0}", e.Message); } finally { // Close the streams and // close the file. sReader.Close(); cStream.Close(); fStream.Close(); } // Return the string. return val; } catch (CryptographicException e) { Console.WriteLine("A Cryptographic error occurred: {0}", e.Message); return null; } catch (UnauthorizedAccessException e) { Console.WriteLine("A file error occurred: {0}", e.Message); return null; } } }



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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


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

Public Sub New ( _ stream As Stream, _ transform As ICryptoTransform, _ mode As CryptoStreamMode _ )
Dim stream As Stream Dim transform As ICryptoTransform Dim mode As CryptoStreamMode Dim instance As New CryptoStream(stream, transform, mode)
public function CryptoStream ( stream : Stream, transform : ICryptoTransform, mode : CryptoStreamMode )

Stream から派生する任意のオブジェクトを stream パラメータに渡すことができます。HashAlgorithm などの ICryptoTransform を実装する任意のオブジェクトを transform パラメータに渡すことができます。

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


CryptoStream プロパティ

名前 | 説明 | |
---|---|---|
![]() | CanRead | オーバーライドされます。 現在の CryptoStream が読み取り可能かどうかを示す値を取得します。 |
![]() | CanSeek | オーバーライドされます。 現在の CryptoStream 内でシークできるかどうかを示す値を取得します。 |
![]() | CanTimeout | 現在のストリームがタイムアウトできるかどうかを決定する値を取得します。 ( Stream から継承されます。) |
![]() | CanWrite | オーバーライドされます。 現在の CryptoStream が書き込み可能かどうかを示す値を取得します。 |
![]() | Length | オーバーライドされます。 ストリーム長 (バイト単位) を取得します。 |
![]() | Position | オーバーライドされます。 現在のストリーム内の位置を取得または設定します。 |
![]() | ReadTimeout | ストリームがタイムアウト前に読み取りを試行する期間を決定する値を取得または設定します。 ( Stream から継承されます。) |
![]() | WriteTimeout | ストリームがタイムアウト前に書き込みを試行する期間を決定する値を取得または設定します。 ( Stream から継承されます。) |

CryptoStream メソッド


名前 | 説明 | |
---|---|---|
![]() | CreateWaitHandle | WaitHandle オブジェクトを割り当てます。 ( Stream から継承されます。) |
![]() | Dispose | オーバーロードされます。 オーバーライドされます。 |
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
![]() | MemberwiseClone | オーバーロードされます。 ( MarshalByRefObject から継承されます。) |


CryptoStream メンバ
データ ストリームを暗号変換にリンクするストリームを定義します。
CryptoStream データ型で公開されるメンバを以下の表に示します。


名前 | 説明 | |
---|---|---|
![]() | CanRead | オーバーライドされます。 現在の CryptoStream が読み取り可能かどうかを示す値を取得します。 |
![]() | CanSeek | オーバーライドされます。 現在の CryptoStream 内でシークできるかどうかを示す値を取得します。 |
![]() | CanTimeout | 現在のストリームがタイムアウトできるかどうかを決定する値を取得します。(Stream から継承されます。) |
![]() | CanWrite | オーバーライドされます。 現在の CryptoStream が書き込み可能かどうかを示す値を取得します。 |
![]() | Length | オーバーライドされます。 ストリーム長 (バイト単位) を取得します。 |
![]() | Position | オーバーライドされます。 現在のストリーム内の位置を取得または設定します。 |
![]() | ReadTimeout | ストリームがタイムアウト前に読み取りを試行する期間を決定する値を取得または設定します。 (Stream から継承されます。) |
![]() | WriteTimeout | ストリームがタイムアウト前に書き込みを試行する期間を決定する値を取得または設定します。 (Stream から継承されます。) |


名前 | 説明 | |
---|---|---|
![]() | CreateWaitHandle | WaitHandle オブジェクトを割り当てます。 (Stream から継承されます。) |
![]() | Dispose | オーバーロードされます。 オーバーライドされます。 |
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
![]() | MemberwiseClone | オーバーロードされます。 ( MarshalByRefObject から継承されます。) |


- CryptoStreamのページへのリンク