Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Imports System
  2. Imports System.IO
  3. Imports System.Security
  4. Imports System.Security.Cryptography
  5. Imports System.Runtime.InteropServices
  6. Imports System.Text
  7.  
  8.  
  9. Module Module1
  10.  
  11.    ' Call this function to remove the key from memory after it is used for security.
  12.   <DllImport("kernel32.dll")> _
  13.    Public Sub ZeroMemory(ByVal addr As IntPtr, ByVal size As Integer)
  14.    End Sub
  15.  
  16.    ' Function to generate a 64-bit key.
  17.   Function GenerateKey() As String
  18.       ' Create an instance of a symmetric algorithm. The key and the IV are generated automatically.
  19.      Dim desCrypto As DESCryptoServiceProvider = DESCryptoServiceProvider.Create()
  20.  
  21.       ' Use the automatically generated key for encryption.
  22.      Return ASCIIEncoding.ASCII.GetString(desCrypto.Key)
  23.  
  24.    End Function
  25.  
  26.    Sub EncryptFile(ByVal sInputFilename As String, _
  27.                   ByVal sOutputFilename As String, _
  28.                   ByVal sKey As String)
  29.  
  30.       Dim fsInput As New FileStream(sInputFilename, _
  31.                                   FileMode.Open, FileAccess.Read)
  32.       Dim fsEncrypted As New FileStream(sOutputFilename, _
  33.                                   FileMode.Create, FileAccess.Write)
  34.  
  35.       Dim DES As New DESCryptoServiceProvider()
  36.  
  37.       'Set secret key for DES algorithm.
  38.      'A 64-bit key and an IV are required for this provider.
  39.      DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
  40.  
  41.       'Set the initialization vector.
  42.      DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
  43.  
  44.       'Create the DES encryptor from this instance.
  45.      Dim desencrypt As ICryptoTransform = DES.CreateEncryptor()
  46.       'Create the crypto stream that transforms the file stream by using DES encryption.
  47.      Dim cryptostream As New CryptoStream(fsEncrypted, _
  48.                                           desencrypt, _
  49.                                           CryptoStreamMode.Write)
  50.  
  51.       'Read the file text to the byte array.
  52.      Dim bytearrayinput(fsInput.Length - 1) As Byte
  53.       fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
  54.       'Write out the DES encrypted file.
  55.      cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
  56.       cryptostream.Close()
  57.    End Sub
  58.  
  59.    Sub DecryptFile(ByVal sInputFilename As String, _
  60.        ByVal sOutputFilename As String, _
  61.        ByVal sKey As String)
  62.  
  63.       Dim DES As New DESCryptoServiceProvider()
  64.       'A 64-bit key and an IV are required for this provider.
  65.      'Set the secret key for the DES algorithm.
  66.      DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
  67.       'Set the initialization vector.
  68.      DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
  69.  
  70.       'Create the file stream to read the encrypted file back.
  71.      Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
  72.       'Create the DES decryptor from the DES instance.
  73.      Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()
  74.       'Create the crypto stream set to read and to do a DES decryption transform on incoming bytes.
  75.      Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)
  76.       'Print out the contents of the decrypted file.
  77.      Dim fsDecrypted As New StreamWriter(sOutputFilename)
  78.       fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd)
  79.       fsDecrypted.Flush()
  80.       fsDecrypted.Close()
  81.    End Sub
  82.  
  83.    Public Sub Main()
  84.       'Must be 64 bits, 8 bytes.
  85.      Dim sSecretKey As String
  86.  
  87.       ' Get the key for the file to encrypt.
  88.      ' You can distribute this key to the user who will decrypt the file.
  89.      sSecretKey = GenerateKey()
  90.  
  91.       ' For additional security, pin the key.
  92.      Dim gch As GCHandle = GCHandle.Alloc(sSecretKey, GCHandleType.Pinned)
  93.  
  94.  
  95.       ' Encrypt the file.        
  96.      EncryptFile("%USERPROFILE%\MyData.txt", _
  97.                       "%USERPROFILE%\Encrypted.txt", _
  98.                       sSecretKey)
  99.  
  100.       ' Decrypt the file.
  101.      DecryptFile("%USERPROFILE%\Encrypted.txt", _
  102.                   "%USERPROFILE%\Decrypted.txt", _
  103.                   sSecretKey)
  104.  
  105.       ' Remove the key from memory.
  106.      ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2)
  107.       gch.Free()
  108.    End Sub
  109.  
  110. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement