Viewing source for Recipe1509vb.aspx

<%@ Page %>
<script Language="vb" runat="server">

Private Sub Page_Load(sender As Object, e As System.EventArgs)
   Dim clearText As String = "Try not.  Do.  Or do not.  There is no try."
   Dim passPhrase As String = "Password"
   Dim encoder As New System.Text.UTF8Encoding()
   
   Response.Write(("Clear Text: " + clearText))
   Response.Write("<hr>")
   
   Dim cypherText As Byte() = EncryptData(clearText, passPhrase)
   
   Response.Write(("Encrypted Text: " + encoder.GetString(cypherText)))
   Response.Write("<hr>")
   Response.Write(("Decrypted Text: " + DecryptData(cypherText, passPhrase)))
   Response.Write("<hr>")
End Sub 'Page_Load


Private Function EncryptData(ClearText As String, password As String) As Byte()
   Dim keySize As Integer = 24 ' 192 bit
   Dim hash() As Byte
   Dim passwordArray() As Byte = New [Byte](password.Length) {}
   Dim tdeskey(keySize-1) As Byte
   Dim IV As Byte() =  {1, 2, 3, 4, 5, 6, 7, 8}
   Dim encoder As New System.Text.UTF8Encoding()
   encoder.GetBytes(password, 0, password.Length, passwordArray, 0)
   
   ' hash the password into the key byte array
   Dim SHA As New System.Security.Cryptography.SHA1CryptoServiceProvider()
   hash = SHA.ComputeHash(passwordArray)
   
   Dim i As Integer
   For i = 0 To keySize - 1
      tdeskey(i) = hash((i Mod hash.Length))
   Next i 
   ' Encrypt the string data
   ' Create a service provider
   Dim tdes As New System.Security.Cryptography.TripleDESCryptoServiceProvider()
   ' Create an ICryptTransform using the key and initialization vector
   Dim cryptoTransform As System.Security.Cryptography.ICryptoTransform = tdes.CreateEncryptor(tdeskey, IV)
   ' Create an output stream to hold the encrypted data
   Dim EncryptedStream As New System.IO.MemoryStream()
   ' Get the input string into a byte array
   Dim input As Byte() = encoder.GetBytes(ClearText)
   ' Create a CryptoStream to perform the encryption
   Dim crStream As New System.Security.Cryptography.CryptoStream(EncryptedStream, cryptoTransform, System.Security.Cryptography.CryptoStreamMode.Write)
   ' Write the input data to the output stream via the cryptoStream
   crStream.Write(input, 0, input.Length) 'Bug in 1st printing of book has -1 here
   crStream.FlushFinalBlock()
   ' Reset the output stream to its beginning
   EncryptedStream.Position = 0
   ' Read the encrypted stream back into a string
   Dim output(EncryptedStream.Length) As Byte
   EncryptedStream.Read(output, 0, CInt(EncryptedStream.Length))
   crStream.Close()
   EncryptedStream.Close()
   ' Return the result as a string
   Return output
End Function 'EncryptData


Private Function DecryptData(CypherText() As Byte, password As String) As String
   Dim keySize As Integer = 24 ' 192 bit
   Dim hash() As Byte
   Dim passwordArray() As Byte = New [Byte](password.Length) {}
   Dim tdeskey(keySize-1) As Byte
   Dim IV As Byte() =  {1, 2, 3, 4, 5, 6, 7, 8}
   Dim encoder As New System.Text.UTF8Encoding()
   encoder.GetBytes(password, 0, password.Length, passwordArray, 0)
   
   ' hash the password into the key byte array
   Dim SHA As New System.Security.Cryptography.SHA1CryptoServiceProvider()
   hash = SHA.ComputeHash(passwordArray)
   
   Dim i As Integer
   For i = 0 To keySize - 1
      tdeskey(i) = hash((i Mod hash.Length))
   Next i 
   ' Decrypt the string data
   ' Create a service provider
   Dim tdes As New System.Security.Cryptography.TripleDESCryptoServiceProvider()
   ' Create an ICryptTransform using the key and initialization vector
   Dim cryptoTransform As System.Security.Cryptography.ICryptoTransform = tdes.CreateDecryptor(tdeskey, IV)
   ' Create an output stream to hold the encrypted data
   Dim DecryptedStream As New System.IO.MemoryStream()
   ' Create a CryptoStream to perform the encryption
   Dim crStream As New System.Security.Cryptography.CryptoStream(DecryptedStream, cryptoTransform, System.Security.Cryptography.CryptoStreamMode.Write)
   ' Write the input data to the output stream via the cryptoStream
   crStream.Write(CypherText, 0, CypherText.Length - 1)
   crStream.FlushFinalBlock()
   ' Reset the output stream to its beginning
   DecryptedStream.Position = 0
   ' Read the encrypted stream back into a string
   Dim output(DecryptedStream.Length) As Byte
   DecryptedStream.Read(output, 0, CInt(DecryptedStream.Length))
   crStream.Close()
   DecryptedStream.Close()
   ' Return the result as a string
   Return encoder.GetString(output)
End Function 'DecryptData
</script>