-
Oct 18th, 2013, 10:06 AM
#1
Thread Starter
New Member
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.
-
Oct 18th, 2013, 02:23 PM
#2
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
-
Oct 18th, 2013, 08:56 PM
#3
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.
-
Oct 19th, 2013, 09:39 AM
#4
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
-
Oct 19th, 2013, 08:22 PM
#5
Re: Encrypting/Decrypting an Image and Folder
Originally Posted by dolot
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:
Private Function AesEncrypt(data As Byte()) As Byte() '... End Function Private Function AesDecrypt(data As Byte()) As Byte() '... 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:
Private Sub AesEnctryptFile(sourceFilePath As String, destinationFilePath As String) File.WriteAllBytes(destinationFilePath, AesEncrypt(File.ReadAllBytes(sourceFilePath))) End Sub Private Sub AesDectryptFile(sourceFilePath As String, destinationFilePath As String) File.WriteAllBytes(destinationFilePath, AesDecrypt(File.ReadAllBytes(sourceFilePath))) End Sub Private Function AesEncryptText(text As String) As Byte() Return AesEncryptText(text, Encoding.UTF8) End Function Private Function AesEncryptText(text As String, encoding As Encoding) As Byte() Return AesEncrypt(encoding.GetBytes(text)) End Function Private Function AesDecryptText(data As Byte()) As String Return AesDecryptText(data, Encoding.UTF8) End Function Private Function AesDecryptText(data As Byte(), encoding As Encoding) As String Return encoding.GetString(AesDecrypt(data)) End Function Private Function AesEncryptImage(image As Image) As Byte() Using stream As New MemoryStream image.Save(stream, image.RawFormat) Return AesEncrypt(stream.GetBuffer()) End Using End Function Private Function AesDecryptImage(data As Byte()) As Image Using stream As New MemoryStream(AesDecrypt(data)) stream.Seek(0, SeekOrigin.Begin) Return Image.FromStream(stream) End Using 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.
-
Oct 19th, 2013, 09:12 PM
#6
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
-
Oct 21st, 2013, 08:04 AM
#7
Thread Starter
New Member
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.
-
Oct 21st, 2013, 06:09 PM
#8
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.
-
Oct 21st, 2013, 06:45 PM
#9
Thread Starter
New Member
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.
-
Oct 21st, 2013, 06:49 PM
#10
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.
-
Oct 22nd, 2013, 07:10 AM
#11
Re: Encrypting/Decrypting an Image and Folder
Originally Posted by jmcilhinney
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
-
Oct 22nd, 2013, 07:18 AM
#12
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
-
Oct 22nd, 2013, 07:20 AM
#13
Thread Starter
New Member
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
-
Oct 22nd, 2013, 07:30 AM
#14
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
-
Oct 22nd, 2013, 07:34 AM
#15
Thread Starter
New Member
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
-
Oct 22nd, 2013, 07:47 AM
#16
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|