Advertisement
calfred2808

File Encryption/Decryption

Nov 11th, 2012
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.17 KB | None | 0 0
  1. Example of use:
  2. Code:
  3. '64 or 8bits
  4. Private Const sSecretKey As String = "Password"
  5.  
  6. To Encrypt:
  7. Code:
  8. EncryptFile("c:\test.txt", "c:\tEncrypted.txt", sSecretKey)
  9.  
  10. To Decrypt:
  11. Code:
  12. DecryptFile("c:\temp\Encrypted.txt",  "c:\temp\Decrypted.txt", sSecretKey)
  13.  
  14. The Class:
  15. Code:
  16. Imports System
  17. Imports System.IO
  18. Imports System.Security
  19. Imports System.Security.Cryptography
  20. Imports System.Text
  21. Public Class File_Crypt
  22.     Sub EncryptFile(ByVal sInputFilename As String, _
  23.                    ByVal sOutputFilename As String, _
  24.                    ByVal sKey As String)
  25.  
  26.         Dim fsInput As New FileStream(sInputFilename, _
  27.                                     FileMode.Open, FileAccess.Read)
  28.         Dim fsEncrypted As New FileStream(sOutputFilename, _
  29.                                     FileMode.Create, FileAccess.Write)
  30.  
  31.         Dim DES As New DESCryptoServiceProvider()
  32.  
  33.         'Establecer la clave secreta para el algoritmo DES.
  34.         'Se necesita una clave de 64 bits y IV para este proveedor
  35.         DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
  36.  
  37.         'Establecer el vector de inicialización.
  38.         DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
  39.  
  40.         'crear cifrado DES a partir de esta instancia
  41.         Dim desencrypt As ICryptoTransform = DES.CreateEncryptor()
  42.         'Crear una secuencia de cifrado que transforma la secuencia
  43.         'de archivos mediante cifrado DES
  44.         Dim cryptostream As New CryptoStream(fsEncrypted, _
  45.                                             desencrypt, _
  46.                                             CryptoStreamMode.Write)
  47.  
  48.         'Leer el texto del archivo en la matriz de bytes
  49.         Dim bytearrayinput(fsInput.Length - 1) As Byte
  50.         fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
  51.         'Escribir el archivo cifrado con DES
  52.         cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
  53.         cryptostream.Close()
  54.     End Sub
  55.  
  56.     Sub DecryptFile(ByVal sInputFilename As String, _
  57.         ByVal sOutputFilename As String, _
  58.         ByVal sKey As String)
  59.  
  60.         Dim DES As New DESCryptoServiceProvider()
  61.         'Se requiere una clave de 64 bits y IV para este proveedor.
  62.         'Establecer la clave secreta para el algoritmo DES.
  63.         DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
  64.         'Establecer el vector de inicialización.
  65.         DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
  66.  
  67.         'crear la secuencia de archivos para volver a leer el archivo cifrado
  68.         Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
  69.         'crear descriptor DES a partir de nuestra instancia de DES
  70.         Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()
  71.         'crear conjunto de secuencias de cifrado para leer y realizar
  72.         'una transformación de descifrado DES en los bytes entrantes
  73.         Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)
  74.         'imprimir el contenido de archivo descifrado
  75.         Dim fsDecrypted As New StreamWriter(sOutputFilename)
  76.         fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd)
  77.         fsDecrypted.Flush()
  78.         fsDecrypted.Close()
  79.     End Sub
  80.  
  81. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement