Results 1 to 16 of 16

Thread: Encrypting/Decrypting an Image and Folder

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2013
    Posts
    6

    Encrypting/Decrypting an Image and Folder

    Hi,
    I am new to this site and am in need to some help.

    I am a Student looking for some help encrypting an image and folder. I can encrypt and decrypt a text file with no problems, but can't get close to doing it with an image or foler.

    I am using DESCryptoServiceProvider if that is any help.

    Thank You.

  2. #2
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Music city, U.S.A.
    Posts
    1,253

    Re: Encrypting/Decrypting an Image and Folder

    Hey welcome to the forums.

    Can you post the code that you're using to try to encrypt and decrypt the image, along with any error messages you may be getting?
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Encrypting/Decrypting an Image and Folder

    What you're encrypting and decrypting is completely irrelevant to the encryption and decryption because encryption and decryption only works with one data type: Byte(), i.e. a Byte array. You should be writing your encryption code to take a Byte array as input and your decryption code to produce a Byte array as output. You can then encrypt and decrypt absolutely anything that you can convert into a Byte array, which means anything at all.

    What you need to do is refactor the code you already have so that the part that encrypts a Byte array and decrypts to a Byte array is extracted out from the part that converts the data to a Byte array and a Byte array back to the data. You can then simply write new methods that convert other object types to and from Byte arrays and then use the exactly the same encryption and decryption code.

    That said, if you can already encrypt a text file then you should be able to encrypt any type of file because you simply call File.ReadAllBytes to get a Byte array containing the file data. It makes absolutely no difference what the file contains because it's all just Bytes. It's when you want to convert an existing object to binary data that you have to do different things to different types. For instance, converting a String to a Byte array is different to converting an Image to a Byte array. If you have a Text file and an image file though, there's no need to create a String and an Image first and then convert to Byte arrays because you can simply go straight from the file.

  4. #4
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Music city, U.S.A.
    Posts
    1,253

    Re: Encrypting/Decrypting an Image and Folder

    Does .net have built in classes for encryption and decryption, JMC?
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Encrypting/Decrypting an Image and Folder

    Quote Originally Posted by dolot View Post
    Does .net have built in classes for encryption and decryption, JMC?
    It has an entire namespace dedicated to it: System.Security.Cryptography. That's where the DESCryptoServiceProvider class mentioned in post #1 lives. Mind you, the documentation for that class specifically states:
    Consider using the AesCryptoServiceProvider class instead of the DESCryptoServiceProvider class.
    Anyone who has read that documentation, which should include anyone thinking of using the class, should have read that and made the change accordingly. That namespace is also the place where types dedicated to hash algorithms like SHA1 and MD5 live.

    Basically, you should first create methods like this:
    vb.net Code:
    1. Private Function AesEncrypt(data As Byte()) As Byte()
    2.     '...
    3. End Function
    4.  
    5. Private Function AesDecrypt(data As Byte()) As Byte()
    6.     '...
    7. End Function
    Once implemented, those functions will encrypt and decrypt any data using the AES algorithm. You can then write as many methods as you need to encrypt and decrypt as many types of data as you need, e.g.
    vb.net Code:
    1. Private Sub AesEnctryptFile(sourceFilePath As String, destinationFilePath As String)
    2.     File.WriteAllBytes(destinationFilePath, AesEncrypt(File.ReadAllBytes(sourceFilePath)))
    3. End Sub
    4.  
    5. Private Sub AesDectryptFile(sourceFilePath As String, destinationFilePath As String)
    6.     File.WriteAllBytes(destinationFilePath, AesDecrypt(File.ReadAllBytes(sourceFilePath)))
    7. End Sub
    8.  
    9. Private Function AesEncryptText(text As String) As Byte()
    10.     Return AesEncryptText(text, Encoding.UTF8)
    11. End Function
    12.  
    13. Private Function AesEncryptText(text As String, encoding As Encoding) As Byte()
    14.     Return AesEncrypt(encoding.GetBytes(text))
    15. End Function
    16.  
    17. Private Function AesDecryptText(data As Byte()) As String
    18.     Return AesDecryptText(data, Encoding.UTF8)
    19. End Function
    20.  
    21. Private Function AesDecryptText(data As Byte(), encoding As Encoding) As String
    22.     Return encoding.GetString(AesDecrypt(data))
    23. End Function
    24.  
    25. Private Function AesEncryptImage(image As Image) As Byte()
    26.     Using stream As New MemoryStream
    27.         image.Save(stream, image.RawFormat)
    28.  
    29.         Return AesEncrypt(stream.GetBuffer())
    30.     End Using
    31. End Function
    32.  
    33. Private Function AesDecryptImage(data As Byte()) As Image
    34.     Using stream As New MemoryStream(AesDecrypt(data))
    35.         stream.Seek(0, SeekOrigin.Begin)
    36.  
    37.         Return Image.FromStream(stream)
    38.     End Using
    39. End Function
    None of those methods have any encryption or decryption code in them because it's all encapsulated in the first two methods, therefore there is no duplication of code.

  6. #6
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Music city, U.S.A.
    Posts
    1,253

    Re: Encrypting/Decrypting an Image and Folder

    Excellent, my friend - I'll keep that for future reference. Thanks much!
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  7. #7

    Thread Starter
    New Member
    Join Date
    Oct 2013
    Posts
    6

    Re: Encrypting/Decrypting an Image and Folder

    Hi,
    Thanks for the help, I can do encrypt and decrypt an image.
    I am struggling to do a folder as I have no idea where to start.

    my code for encryption is.....

    Dim outputFile As String
    outputFile = "M:\New Folder\Encrypted.txt"

    Dim fsInput As New FileStream(TextBox1.Text, FileMode.Open, FileAccess.Read)
    Dim fsEncrypted As New FileStream(outputFile, FileMode.Create, FileAccess.Write)
    Dim sKey As String
    sKey = "Helloabc"


    Dim DES As New DESCryptoServiceProvider
    DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)

    Dim desencrypt As ICryptoTransform
    desencrypt = DES.CreateEncryptor()


    Dim cryptostream As New CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write)
    Dim bytearrayinput(fsInput.Length) As Byte

    fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
    cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
    cryptostream.Close()
    fsInput.Close()
    fsEncrypted.Close()
    TextBox2.Text = "M:\New Folder\Encrypted.txt"

    My code for decrypting is....

    Dim DES As New DESCryptoServiceProvider

    Dim sKey As String
    sKey = "Helloabc"

    DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)

    Dim fsread As New FileStream(TextBox2.Text, FileMode.Open, FileAccess.Read)

    Dim desdecrypt As ICryptoTransform
    desdecrypt = DES.CreateDecryptor()


    Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)

    Dim fsDecrypted As New StreamWriter("M:\New Folder\Decrypted.txt")
    fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd())
    fsDecrypted.Flush()
    fsDecrypted.Close()
    TextBox3.Text = "M:\New Folder\Decrypted.txt"


    Thank You, please help.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Encrypting/Decrypting an Image and Folder

    Firstly, when you post code, please using formatting tags. You can see how much easier my code is to read than yours.

    As for the issue, you can't encrypt a folder because folder isn't a thing that can be represented by a Byte array, or at least there is no standard way of doing so. Are you actually talking about encrypting each file in a folder or creating a single file containing the entire contents of a folder? If it's the latter then you first have to decide how you're going to represent the folder in a single block.

  9. #9

    Thread Starter
    New Member
    Join Date
    Oct 2013
    Posts
    6

    Re: Encrypting/Decrypting an Image and Folder

    Sorry I am new to this so don't really know what I am doing.

    I need to be able to encrypt and decrypt every file individually within a folder.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Encrypting/Decrypting an Image and Folder

    Well, you already know how to encrypt and decrypt a file, so it's simply a matter of accessing each file within the folder. If you just want the immediate folder contents then it's a simple matter of:
    Code:
    For Each filePath In IO.Directory.GetFiles(folderPath)
        'Use filePath here.
    Next
    If you want the contents of subfolders too then you use Directory.GetFiles and Directory.GetDirectories recursively. There are many examples of recursive file searches about, including in our own CodeBank forum.

  11. #11
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Music city, U.S.A.
    Posts
    1,253

    Re: Encrypting/Decrypting an Image and Folder

    Quote Originally Posted by jmcilhinney View Post
    If it's the latter then you first have to decide how you're going to represent the folder in a single block.
    To me this statement implies that there's more than one way to represent a folder in a single block. The only way I've ever used is via a zip (compressed) file. Are there others?
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  12. #12
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Encrypting/Decrypting an Image and Folder

    You could use XML... with meta data about each file and its location, followed by thecontents of the file in a CDATA block. You could also write thefile meta data (name, size, offsets, location, etc) to a file then writethe bytes directly in a continuous stream... there's a number of ways to do it. Granted, the zip idea (which I've used before too) is probably the easiest & sanest way to go about it.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13

    Thread Starter
    New Member
    Join Date
    Oct 2013
    Posts
    6

    Re: Encrypting/Decrypting an Image and Folder

    Thank you for the help guys.

    One last thing, I want to the user to enter a password before the encryption happens. This password will then be used to decrypt a file. If the passwords to match then the decryption does'nt happen .

    Any idea?

    Thank you

  14. #14
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Encrypting/Decrypting an Image and Folder

    Then it needs to be stored with the file somehow... but not in plain text... you would then need to extract, it compare it to the entered password and if it matches, then continue. I would recommend hashing the password, storing it with the file... then after the user enters the password, has THAT, and compare it to the stored hash ... if the two hash values match, then continue.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  15. #15

    Thread Starter
    New Member
    Join Date
    Oct 2013
    Posts
    6

    Re: Encrypting/Decrypting an Image and Folder

    Thank You, I kinda understand what you mean, I am very new to programming sorry. Any chance you could give me some code to work from?

    Thank You

  16. #16

    Thread Starter
    New Member
    Join Date
    Oct 2013
    Posts
    6

    Re: Encrypting/Decrypting an Image and Folder

    Also what code would i use to plain text it?

    Thank You

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width