Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / XML

P2P Secure File, Encrypt and Decrypt Any File with Password

3.53/5 (11 votes)
5 Apr 2008CPOL1 min read 1   1.7K  
Protect any file by encryption and decryption with password. It encrypts the file and saves as secure XML with binary format, and stores file password as custom PK encrypter routine.
secure_file

secure_file

Introduction

This article discusses how you can protect any file by encrypting with password. Download the source code of P2P Secure File written in VB.NET. It encrypts the file and saves it as secure XML with binary format, and stores file password as custom PK encrypter routine.

Background

This P2P Secure File program source code written in Visual Basic .NET programming language is capable of encrypting and decrypting any file. It converts any file into byte array and converts byte array to stream and writes it into XML file with *.sp2p extension.

Using the Code

Logic

Step 1: First select a file. Convert the file content into Byte array. For that, we create FileStream object and pass this FileStream object to BinaryReader to get the raw bytes.

VB.NET
Dim Fs As New System.IO.FileStream(FileName, System.IO.FileMode.Open)
Dim bn As New System.IO.BinaryReader(Fs)

Step 2: Now create a DataTable to a DataSet to store all the details including file name, binary content and encrypted password.

VB.NET
Dim DsImg As New DataSet
Dim Dt As New DataTable("Images")
Dt.Columns.Add(New DataColumn("sysid", _
System.Type.GetType("System.String")))
Dt.Columns.Add(New DataColumn("filename", _
System.Type.GetType("System.String")))
Dt.Columns.Add(New DataColumn("image", _
System.Type.GetType("System.Byte[]")))
Dt.Columns.Add(New DataColumn("filetag", _
System.Type.GetType("System.String")))
DsImg.Tables.Add(Dt)

Step 3: Now add the data to the DataTable and write XML file with *.sp2p extension, encrypt password with custom PK encryption. If you want, you can encrypt the binary content. I left this to the users.

VB.NET
Dim Dr As DataRow
Dr = DsImg.Tables("images").NewRow
Dr("sysid") = Now.ToString
Dr("filename") = TxtFileName.Text
Dr("image") = bn.ReadBytes(Int(bn.BaseStream.Length))
Dr("filetag") = StrEncrypt(TxtPassword.Text)
DsImg.Tables("images").Rows.Add(Dr)

'>>> write xml file from dataset with binary content
DsImg.WriteXml(TxtFileName.Text & ".sp2p") 

Step 4: For decryption, we load the XML file into dataset, then decrypt the password to check with the supplied password, If it matches, read content from dataset into byte array and write into the file stream, remove .sp2p from the final decrypted file.

VB.NET
Dim Content As Byte()
Content = DsImg.Tables(0).Rows(0).Item(2)
Dim Fs As New System.IO.FileStream(FileName, System.IO.FileMode.Create)
Fs.Write(Content, 0, Content.Length)
Fs.Close()

Points of Interest

Here I write a chunk of bytes to file system directly. If you want, you can encrypt or transpose the byte sequence for better security. You can find a more detailed explanation of encryption algorithm on my site programmer2programmer.

History

  • 5th April, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)