cifrar Carpetas
Tomas1982:
Buenos días ante todo, Estoy tratando de realizar un programa que me organiza los ficheros por extensiones, pero quiero que a terminal este me encripte la carpeta con contraseña para que nadie tenga acceso a ella, e buscado por la red pero no logro encontrar nada que me funcione, si alguien tuviese alguna idea para lograrlo y quisiera compartirlo lo agradecería...
MCKSys Argentina:
Seguro que haz buscado bien? Porque me sale ésto en el 3er resultado de Google How to encrypt and decrypt a file by using Visual C#
Saludos!
PD: Mi búsqueda.
Tomas1982:
Disculpen por no aclararlo antes, pero el lenguaje que estoy utilizando es vb.net
MCKSys Argentina:
Bueno, pasar de C# a VB.NET es cuestión de sintaxis solamente.
Incluso, hay herramientas que lo hacen en forma automática.
Saludos!
okik:
Cita de: MCKSys Argentina en 5 Octubre 2016, 14:17 pm
Seguro que haz buscado bien? Porque me sale ésto en el 3er resultado de Google How to encrypt and decrypt a file by using Visual C#
Saludos!
PD: Mi búsqueda.
Eso es para archivos no para carpetas... si no me equivoco
Ahora lo miro a ver... :P
Pues lo que decía ese código es para cifrar el contenido de un archivo.
Código
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Imports System.Runtime.InteropServices
Imports System.Text
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Must be 64 bits, 8 bytes.
' Distribute this key to the user who will decrypt this file.
Dim sSecretKey As String
sSecretKey = CSEncryptDecrypt.Class1.GenerateKey() ' Get the Key for the file to Encrypt.
Dim gch As GCHandle = GCHandle.Alloc(sSecretKey, GCHandleType.Pinned) ' For additional security Pin the key.
CSEncryptDecrypt.Class1.EncryptFile("C:\Prueba\Texto.txt", "C:\Prueba\TextoEncrypt.txt", sSecretKey) ' Encrypt the file.
CSEncryptDecrypt.Class1.ZeroMemory(gch.AddrOfPinnedObject(), CType(sSecretKey.Length * 2, IntPtr))
gch.Free()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
' Must be 64 bits, 8 bytes.
' Distribute this key to the user who will decrypt this file.
Dim sSecretKey As String
sSecretKey = CSEncryptDecrypt.Class1.GenerateKey() ' Get the Key for the file to Encrypt.
Dim gch As GCHandle = GCHandle.Alloc(sSecretKey, GCHandleType.Pinned)
CSEncryptDecrypt.Class1.DecryptFile("C:\Prueba\TextoEncrypt.txt", "C:\Prueba\Decrypt.txt", sSecretKey) ' Decrypt the file.
CSEncryptDecrypt.Class1.ZeroMemory(gch.AddrOfPinnedObject(), CType(sSecretKey.Length * 2, IntPtr)) ' Remove the Key from memory.
gch.Free()
End Sub
End Class
Namespace CSEncryptDecrypt
Class Class1
<DllImport("kernel32.dll")> _
Public Shared Sub ZeroMemory(ByVal addr As IntPtr, ByVal size As IntPtr)
End Sub
' Call this function to remove the key from memory after use for security
'<System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint:="RtlZeroMemory")> _
'Public Shared Function ZeroMemory(Destination As IntPtr, Length As Integer) As Boolean
'End Function
' Function to Generate a 64 bits Key.
Public Shared Function GenerateKey() As String
' Create an instance of Symetric Algorithm. Key and IV is generated automatically.
Dim desCrypto As DESCryptoServiceProvider = DirectCast(DESCryptoServiceProvider.Create(), DESCryptoServiceProvider)
' Use the Automatically generated key for Encryption.
Return ASCIIEncoding.ASCII.GetString(desCrypto.Key)
End Function
Public Shared Sub EncryptFile(sInputFilename As String, sOutputFilename As String, sKey As String)
Dim fsInput As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
Dim fsEncrypted As New FileStream(sOutputFilename, FileMode.Create, FileAccess.Write)
Dim DES As New DESCryptoServiceProvider()
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
Dim desencrypt As ICryptoTransform = DES.CreateEncryptor()
Dim cryptostream As New CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write)
Dim bytearrayinput As Byte() = New Byte(CInt(fsInput.Length - 1)) {}
fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
cryptostream.Close()
fsInput.Close()
fsEncrypted.Close()
End Sub
Public Shared Sub DecryptFile(sInputFilename As String, sOutputFilename As String, sKey As String)
Dim DES As New DESCryptoServiceProvider()
'A 64 bit key and IV is required for this provider.
'Set secret key For DES algorithm.
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
'Set initialization vector.
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
'Create a file stream to read the encrypted file back.
Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
'Create a DES decryptor from the DES instance.
Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()
'Create crypto stream set to read and do a
'DES decryption transform on incoming bytes.
Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)
'Print the contents of the decrypted file.
Dim fsDecrypted As New StreamWriter(sOutputFilename)
fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd())
fsDecrypted.Flush()
fsDecrypted.Close()
End Sub
End Class
End Namespace
Encontré este código lo he testado y funciona:
Código
Imports System.Security.AccessControl
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FolderBrowserDialog1 As New FolderBrowserDialog
With FolderBrowserDialog1
If .ShowDialog() = DialogResult.OK Then
TextBox1.Text = .SelectedPath
End If
End With
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim fs As FileSystemSecurity = File.GetAccessControl(TextBox1.Text)
fs.AddAccessRule(New FileSystemAccessRule(Environment.UserName, FileSystemRights.FullControl, AccessControlType.Deny))
File.SetAccessControl(TextBox1.Text, CType(fs, FileSecurity))
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim fs As FileSystemSecurity = File.GetAccessControl(TextBox1.Text)
fs.RemoveAccessRule(New FileSystemAccessRule(Environment.UserName, FileSystemRights.FullControl, AccessControlType.Deny))
File.SetAccessControl(TextBox1.Text, CType(fs, FileSecurity))
End Sub
End Class
FUENTE:
http://burnignorance.com/vb-net-tricks/how-to-lock-and-unlock-a-folder-through-code-in-vb-net/
Esto bloquea el acceso a la carpeta. Lo que no se es si será accesible desde un Linxu Live por ejemplo o algún programa de arranque, o quizás desde el modo seguro.
Lo se la contraseña se me ocurre que la metas cifrada, con el código de arriba por ejemplo dentro de la misma carpeta, desde el programa cuando se quiera desbloquear, el programa desbloquearía la carpeta el tiempo justo para poder acceder a la contraseña descifrarla y compararla con la que introduce el usuario. Se ve un poco, chapuza pero es lo que se me ocurre ahora. Podrías mirarte en ingeniería inversa, a ver.
Navegación
[#] Página Siguiente