Kann für nen Crypter ganz nützlich sein

Credits: Dr. Peter Flsicher


Code:
ption Explicit On
Option Infer On
Option Strict On

Imports System.Xml.Serialization
Imports System.IO
Imports System.Security.Cryptography

Module Module1

  Sub Main()
    Try
      '
      ' Teil 1 Datei verschlüsseln und ablegen
      '
      Using fs As New FileStream("c:\temp\x1.x", FileMode.Open)
        ' verschlüsseln
        SaveEncrypted("c:\temp\x2.x", fs)
      End Using
      '
      ' Teil 2 Datei wieder entschlüsseln
      '
      Using fs As New FileStream("c:\temp\x3.x", FileMode.Create)
        ' entschlüsseln
        LoadEncrypted("c:\temp\x2.x", fs)
      End Using
      '
      Console.WriteLine("Vorgang abgeschlossen")
    Catch ex As Exception
      Console.WriteLine(ex.Message)
    End Try
    '
    Console.ReadLine()
  End Sub

  ''' <summary>
  ''' Schlüssel für Ver- und Entschlüsselung
  ''' </summary>
  ''' <remarks></remarks>
  Dim desKey() As Byte = {1, 2, 3, 4, 5, 6, 7, 8}
  Dim desIV() As Byte = {1, 2, 3, 4, 5, 6, 7, 9}

  ''' <summary>
  ''' XML-Datenstrom verschlüsseln und in Datei ablegen 
  ''' </summary>
  ''' <param name="filename">Dateiname für Ablage</param>
  ''' <param name="str">IO-Stream (z.B. MemoryStream) mit Daten</param>
  ''' <remarks></remarks>
  Private Sub SaveEncrypted(ByVal filename As String, ByVal str As Stream)
    Dim fout As FileStream = Nothing
    Dim encStream As CryptoStream = Nothing
    Try
      'Create the file streams to handle the output files.
      fout = New FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write)
      fout.SetLength(0)
      'Create variables to help with read and write.
      Dim bin(99) As Byte 'This is intermediate storage for the encryption.
      Dim rdlen As Long = 0 'This is the total number of bytes written.
      Dim totlen As Long = str.Length 'Total length of the input file.
      Dim lng As Integer ' This is the number of bytes to be written at a time.
      'Creates the default implementation, which is RijndaelManaged.
      Dim des As New DESCryptoServiceProvider
      encStream = New CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write)
      'Read from the input file, then encrypt and write to the output file.
      str.Position = 0
      While rdlen < totlen
        lng = str.Read(bin, 0, bin.GetUpperBound(0))
        encStream.Write(bin, 0, lng)
        rdlen = Convert.ToInt32(rdlen + lng)
      End While
    Catch ex As Exception
      MsgBox(ex.Message)
    Finally
      If encStream IsNot Nothing Then encStream.Dispose()
      If fout IsNot Nothing Then fout.Close()
    End Try
  End Sub

  ''' <summary>
  ''' XML-Datenstrom aus Datei laden und entschlüsseln
  ''' </summary>
  ''' <param name="filename">Dateiname zum Einlesen</param>
  ''' <param name="str">IO-Stream (z.B. MemoryStream) mit Daten</param>
  ''' <remarks></remarks>
  Private Sub LoadEncrypted(ByVal filename As String, ByVal str As Stream)
    Dim fin As System.IO.FileStream = Nothing
    Dim decStream As CryptoStream = Nothing
    Try
      fin = New FileStream(filename, FileMode.Open, FileAccess.Read)
      'Create variables to help with read and write.
      Dim bin(10000) As Byte ' This is intermediate storage for the encryption.
      Dim rdlen As Long = 0 'This is the total number of bytes written.
      Dim totlen As Long = fin.Length 'Total length of the input file.
      Dim lng As Integer ' This is the number of bytes to be written at a time.
      'Creates the default implementation, which is RijndaelManaged.
      Dim des As New DESCryptoServiceProvider
      decStream = New CryptoStream(fin, des.CreateDecryptor(desKey, desIV), CryptoStreamMode.Read)
      'Read from the input file, then encrypt and write to the output file.
      lng = decStream.Read(bin, 0, bin.GetUpperBound(0) + 1)
      While lng > 0
        str.Write(bin, 0, lng)
        rdlen = Convert.ToInt32(rdlen + lng)
        lng = decStream.Read(bin, 0, bin.GetUpperBound(0) + 1)
      End While
    Catch ex As Exception
      MsgBox(ex.Message)
    Finally
      If decStream IsNot Nothing Then decStream.Dispose()
      If fin IsNot Nothing Then fin.Dispose()
    End Try
  End Sub

End Module