Encrypt and Decrypt File and Read it.

Post your questions regarding programming in C# in here.
6 posts Page 1 of 1
Contributors
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1892
Joined: Wed Dec 16, 2009 9:56 pm

How can i create a file ex (.dlx)*random extension*.
With encrypted data so the user can't open in a .txt file.
For example if someone opened it, it would look like this:
Code: Select all
ÃÑr&àùÃ3_#Ÿªž;K‘Ä6ÉxQåÍo¨úlµÔ}@["µ¾EäÇËà bÀ·Öð¨X›>9j=›ýÎñLð-{©–y»]Ù: à½_¼ûw˜è“ˆC}˜íU¬¡B
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that :)
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

You could use IO.File.Encrypt, it ensures that only the user that encrypted it can open or decrypt it. You could also use the Cryptography namespace inside System.Security. Here is an example
Code: Select all
        Dim Holder As New TripleDESCryptoServiceProvider  'There are many others too
        Holder.Key = System.Text.Encoding.ASCII.GetBytes("Password") 'Either 8 or 16 to 24 characters
        Holder.GenerateIV()
        Dim Encrypter As ICryptoTransform = Holder.CreateEncryptor

        Dim FileStream As New IO.FileStream("File Path", IO.FileMode.Open, IO.FileAccess.Read)
        Dim FileData(FileStream.Length) As Byte
        FileStream.Read(FileData, 0, FileStream.Length)
        FileStream.Close()

        Dim Encrypted As Byte() = Encrypter.TransformFinalBlock(FileData, 0, FileData.Length)

        IO.File.WriteAllBytes("File Path", Encrypted)
        Holder.Dispose()
        Encrypter.Dispose()
Just import System.Security.Cryptography first

To decrypt it use this:
Code: Select all
        Dim Holder As New TripleDESCryptoServiceProvider  'There are many others too
        Holder.Key = System.Text.Encoding.ASCII.GetBytes("Password") 'Either 8 or 16 to 24 characters
        Dim Decryptor As ICryptoTransform = Holder.CreateDecryptor

        Dim FileStream As New IO.FileStream("File Path", IO.FileMode.Open, IO.FileAccess.Read)
        Dim FileData(FileStream.Length) As Byte
        FileStream.Read(FileData, 0, FileStream.Length)
        FileStream.Close()

        Dim Decrypted As Byte() = Decryptor.TransformFinalBlock(FileData, 0, FileData.Length)

        IO.File.WriteAllBytes("File Path", Decrypted)
        Holder.Dispose()
        Decryptor.Dispose()
Oh dang... Its C#! Well wait a minute... :evil:
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1892
Joined: Wed Dec 16, 2009 9:56 pm

MrAksel wrote:
You could use IO.File.Encrypt, it ensures that only the user that encrypted it can open or decrypt it. You could also use the Cryptography namespace inside System.Security. Here is an example
Code: Select all
        Dim Holder As New TripleDESCryptoServiceProvider  'There are many others too
        Holder.Key = System.Text.Encoding.ASCII.GetBytes("Password") 'Either 8 or 16 to 24 characters
        Holder.GenerateIV()
        Dim Encrypter As ICryptoTransform = Holder.CreateEncryptor

        Dim FileStream As New IO.FileStream("File Path", IO.FileMode.Open, IO.FileAccess.Read)
        Dim FileData(FileStream.Length) As Byte
        FileStream.Read(FileData, 0, FileStream.Length)
        FileStream.Close()

        Dim Encrypted As Byte() = Encrypter.TransformFinalBlock(FileData, 0, FileData.Length)

        IO.File.WriteAllBytes("File Path", Encrypted)
        Holder.Dispose()
        Encrypter.Dispose()
Just import System.Security.Cryptography first

To decrypt it use this:
Code: Select all
        Dim Holder As New TripleDESCryptoServiceProvider  'There are many others too
        Holder.Key = System.Text.Encoding.ASCII.GetBytes("Password") 'Either 8 or 16 to 24 characters
        Dim Decryptor As ICryptoTransform = Holder.CreateDecryptor

        Dim FileStream As New IO.FileStream("File Path", IO.FileMode.Open, IO.FileAccess.Read)
        Dim FileData(FileStream.Length) As Byte
        FileStream.Read(FileData, 0, FileStream.Length)
        FileStream.Close()

        Dim Decrypted As Byte() = Decryptor.TransformFinalBlock(FileData, 0, FileData.Length)

        IO.File.WriteAllBytes("File Path", Decrypted)
        Holder.Dispose()
        Decryptor.Dispose()
Oh dang... Its C#! Well wait a minute... :evil:
I'll try and convert it :)
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that :)
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

I cant finish it now, but im nearly done with an example app

Edit, Here it is:
ICryptoTransform.zip
Its a command line app. If you want to encrypt a file, add this to the startup commands encrypt "FILENAME" Filename inside the quotes.
To decrypt just change encrypt to decrypt
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Zach, you got it? I got a nice command line app for you.
You can now encrypt multiple files at the same time. Just add those arguments
encrypt (or decrypt) "File1" "File2" "File3" Just separate the file names with spaces and put them in quotes.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1892
Joined: Wed Dec 16, 2009 9:56 pm

MrAksel wrote:
Zach, you got it? I got a nice command line app for you.
You can now encrypt multiple files at the same time. Just add those arguments
encrypt (or decrypt) "File1" "File2" "File3" Just separate the file names with spaces and put them in quotes.
Im going to be using the decrypt and encrypt for a game. the file will be the dlc information being read
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that :)
6 posts Page 1 of 1
Return to “General coding help”