﻿<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Encrypt a String</Title>
      <Author>Microsoft Corporation</Author>
      <Description>Encrypts text from a file using the Rijndael encryption algorithm.</Description>
      <Shortcut>secEncrypt</Shortcut>
    </Header>
    <Snippet>
      <References>
        <Reference>
          <Assembly>System.Security.dll</Assembly>
        </Reference>
      </References>
      <Imports>
        <Import>
          <Namespace>Microsoft.VisualBasic</Namespace>
        </Import>
        <Import>
          <Namespace>System.IO</Namespace>
        </Import>
        <Import>
          <Namespace>System.Security.Cryptography</Namespace>
        </Import>
      </Imports>
      <Declarations>
        <Literal>
          <ID>fileName</ID>
          <Type>String</Type>
          <ToolTip>The file name into which the encrypted text is written.</ToolTip>
          <Default>"encrypted.txt"</Default>
        </Literal>
        <Object>
          <ID>privateKey</ID>
          <Type>Byte</Type>
          <ToolTip>The private Key used to encrypt the data.</ToolTip>
          <Default>RijndaelAlg.Key</Default>
        </Object>
        <Object>
          <ID>initializationVector</ID>
          <Type>Byte</Type>
          <ToolTip>The initialization vector used in the symmetric key encryption scheme.
</ToolTip>
          <Default>RijndaelAlg.IV</Default>
        </Object>
        <Literal>
          <ID>plainText</ID>
          <Type>String</Type>
          <ToolTip>The String variable that contains the text to be encrypted.</ToolTip>
          <Default>"Text to encrypt"</Default>
        </Literal>
      </Declarations>
      <Code Language="VB" Kind="method body"><![CDATA[Using fStream = File.Open("encrypted.txt", FileMode.OpenOrCreate)
    Dim RijndaelAlg As Rijndael = Rijndael.Create
    Using cStream As New CryptoStream(fStream,
                                      RijndaelAlg.CreateEncryptor(RijndaelAlg.Key, RijndaelAlg.IV),
                                      CryptoStreamMode.Write)

        Using sWriter As New StreamWriter(cStream)
            sWriter.WriteLine("Text to encrypt")
        End Using
    End Using
End Using]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>