Page 2 of 3

Re: How do you make a File Encrypter and a Decrypter?

Posted: Mon May 17, 2010 11:58 pm
by lawrence12
Okay.....
Well,I think I'm going to make a txt file encrypter and decrypter.
But that doesn't mean my question is here is done.

Re: How do you make a File Encrypter and a Decrypter?

Posted: Thu May 20, 2010 11:27 am
by lawrence12
If Visual basic 2008 had a tutorial on how to be a programmer.
Oh gees, i can't believe this no one is posting a comment or anything I'm guessing none of ya knows how to encrypt or decrypt a file,only guessing might not be real xD.

Re: How do you make a File Encrypter and a Decrypter?

Posted: Thu May 20, 2010 10:19 pm
by mandai
There are countless ways to encrypt/decrypt files.
You can also get reversible and non reversible encryption.
What are you after exactly?

Re: How do you make a File Encrypter and a Decrypter?

Posted: Sat May 22, 2010 7:00 am
by lawrence12
A File Encrypter and Decrypter
Features must be this:
Able to Encrypt and Decrypt files,txt files etc
Able to save the encrypted/decrypted txt files and able to view it.

Re: How do you make a File Encrypter and a Decrypter?

Posted: Sat May 22, 2010 7:45 am
by CodenStuff
Hello lawrence12,

If your after a tutorial/guide on using encryption to encrypt/decrypt files then theres this one on the Microsoft website with gives you code example and explains the process step-by-step.

http://support.microsoft.com/kb/301070

Re: How do you make a File Encrypter and a Decrypter?

Posted: Sun May 23, 2010 5:49 am
by lawrence12
Good Tutorial and detailed too but.
Why is it a module instead of 2 buttons with their names encrypt and decrypt File & txt File.
And i ain't really good at coding at vb yet so I'm confused a bit.
Code: Select all
A Complete Code List

Imports System
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Imports System.Runtime.InteropServices
Imports System.Text


Module Module1

   ' Call this function to remove the key from memory after it is used for security.
   <DllImport("kernel32.dll")> _
   Public Sub ZeroMemory(ByVal addr As IntPtr, ByVal size As Integer)
   End Sub

   ' Function to generate a 64-bit key.
   Function GenerateKey() As String
      ' Create an instance of a symmetric algorithm. The key and the IV are generated automatically.
      Dim desCrypto As DESCryptoServiceProvider = DESCryptoServiceProvider.Create()

      ' Use the automatically generated key for encryption. 
      Return ASCIIEncoding.ASCII.GetString(desCrypto.Key)

   End Function

   Sub EncryptFile(ByVal sInputFilename As String, _
                  ByVal sOutputFilename As String, _
                  ByVal 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()

      'Set secret key for DES algorithm.
      'A 64-bit key and an IV are required for this provider.
      DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)

      'Set the initialization vector.
      DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)

      'Create the DES encryptor from this instance.
      Dim desencrypt As ICryptoTransform = DES.CreateEncryptor()
      'Create the crypto stream that transforms the file stream by using DES encryption.
      Dim cryptostream As New CryptoStream(fsEncrypted, _
                                          desencrypt, _
                                          CryptoStreamMode.Write)

      'Read the file text to the byte array.
      Dim bytearrayinput(fsInput.Length - 1) As Byte
      fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
      'Write out the DES encrypted file.
      cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
      cryptostream.Close()
   End Sub

   Sub DecryptFile(ByVal sInputFilename As String, _
       ByVal sOutputFilename As String, _
       ByVal sKey As String)

      Dim DES As New DESCryptoServiceProvider()
      'A 64-bit key and an IV are required for this provider.
      'Set the secret key for the DES algorithm.
      DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
      'Set the initialization vector.
      DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)

      'Create the file stream to read the encrypted file back.
      Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
      'Create the DES decryptor from the DES instance.
      Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()
      'Create the crypto stream set to read and to do a DES decryption transform on incoming bytes.
      Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)
      'Print out the contents of the decrypted file.
      Dim fsDecrypted As New StreamWriter(sOutputFilename)
      fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd)
      fsDecrypted.Flush()
      fsDecrypted.Close()
   End Sub

   Public Sub Main()
      'Must be 64 bits, 8 bytes.
      Dim sSecretKey As String

      ' Get the key for the file to encrypt.
      ' You can distribute this key to the user who will decrypt the file.
      sSecretKey = GenerateKey()

      ' For additional security, pin the key.
      Dim gch As GCHandle = GCHandle.Alloc(sSecretKey, GCHandleType.Pinned)


      ' Encrypt the file.        
      EncryptFile("%USERPROFILE%\MyData.txt", _
                      "%USERPROFILE%\Encrypted.txt", _
                      sSecretKey)

      ' Decrypt the file.
      DecryptFile("%USERPROFILE%\Encrypted.txt", _
                  "%USERPROFILE%\Decrypted.txt", _
                  sSecretKey)

      ' Remove the key from memory. 
      ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2)
      gch.Free()
   End Sub

End Module

Re: How do you make a File Encrypter and a Decrypter?

Posted: Mon May 24, 2010 11:44 am
by lawrence12
Sorry guys if my replies are a bit confusing

Re: How do you make a File Encrypter and a Decrypter?

Posted: Sun May 30, 2010 1:33 am
by lawrence12
upperdrag wrote:
lol im not sure but try this lol i found http://www.codeproject.com/KB/files/Dot ... ption.aspx
Well I'll try the source code and make a project like it with txt stuff :),
____________________________________________________________________________________________________
Busy studying c++,autoit,vb.net and playing dragonica or DotA. wahooo;

Re: How do you make a File Encrypter and a Decrypter?

Posted: Wed Jun 09, 2010 11:30 am
by lawrence12
I am VERY confused with the codes right now :?

Re: How do you make a File Encrypter and a Decrypter?

Posted: Wed Jun 09, 2010 12:32 pm
by CodenStuff
Hello,

If you want to put it into application form using buttons to encrypt and decrypt you can do it like the attached project below.

Its quick and you will need to add/change things to make it work how you want it but it gets you started.

Download:
EnDeCrypt.zip
Hope that helps cooll;