Click here to Skip to main content
11,806,931 members (62,754 online)
Click here to Skip to main content

P2P Secure File, Encrypt and Decrypt Any File with Password

, 5 Apr 2008 CPOL 47.6K 1.3K 36
Rate this:
Please Sign up or sign in to vote.
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.



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.


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


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.

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.

Dim DsImg As New DataSet
Dim Dt As New DataTable("Images")
Dt.Columns.Add(New DataColumn("sysid", _
Dt.Columns.Add(New DataColumn("filename", _
Dt.Columns.Add(New DataColumn("image", _
Dt.Columns.Add(New DataColumn("filetag", _

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.

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)

'>>> 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.

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)

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.


  • 5th April, 2008: Initial post


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


About the Author

Atanu Maity
CEO Programmer2Programmer
India India
Master in Computer Application
More than 8 Years of Software Developer Experience. MCSD.Net, In past 8 years I have developed Graphic Application, Telephony Application, CRM Application, Games, Internet Programming, TAPI, SAPI, OCR, ICR, Data Base application. Implementing Data Warehousing and Data mining, MIS Automation, Web Application.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Shubhranshu from Mumba1-Apr-12 19:28
memberShubhranshu from Mumba1-Apr-12 19:28 
GeneralMy vote of 4 Pin
Member 432084415-Aug-11 1:13
memberMember 432084415-Aug-11 1:13 
Questionwhat algorithm this project use? Pin
memberNURFAEZAH ABDUL MALIK19-Oct-10 5:01 
Generalblowing my own horn a bit sorry Pin
MartyK20074-Jun-08 0:42
memberMartyK20074-Jun-08 0:42 
Generalsorry, this code does not encrypt any files, it encrypts the specified password only Pin
Alexey Yakovlev9-Apr-08 6:55
memberAlexey Yakovlev9-Apr-08 6:55 
GeneralRe: sorry, this code does not encrypt any files, it encrypts the specified password only Pin
Atanu Maity9-Apr-08 18:56
memberAtanu Maity9-Apr-08 18:56 
GeneralRe: sorry, this code does not encrypt any files, it encrypts the specified password only Pin
Alexey Yakovlev11-Apr-08 5:50
memberAlexey Yakovlev11-Apr-08 5:50 
GeneralRe: sorry, this code does not encrypt any files, it encrypts the specified password only Pin
Atanu Maity11-Apr-08 21:12
memberAtanu Maity11-Apr-08 21:12 
GeneralAwesome Pin
suresh suthar9-Apr-08 3:18
membersuresh suthar9-Apr-08 3:18 
GeneralRe: Awesome Pin
Abhijit Jana10-May-08 1:43
memberAbhijit Jana10-May-08 1:43 
GeneralThank You Pin
prabhaChoudhary7-Apr-08 20:35
memberprabhaChoudhary7-Apr-08 20:35 
GeneralRe: Thank You Pin
Atanu Maity8-Apr-08 21:52
memberAtanu Maity8-Apr-08 21:52 
Questionthank you Pin
alaa masarweh7-Apr-08 5:16
memberalaa masarweh7-Apr-08 5:16 
GeneralRe: thank you Pin
Atanu Maity7-Apr-08 7:10
memberAtanu Maity7-Apr-08 7:10 
GeneralToo good... Pin
Sushilck7-Apr-08 3:11
memberSushilck7-Apr-08 3:11 
AnswerRe: Too good... Pin
Atanu Maity7-Apr-08 3:34
memberAtanu Maity7-Apr-08 3:34 
QuestionWant to encrypt text file? Pin
Joy_Dass19817-Apr-08 3:06
memberJoy_Dass19817-Apr-08 3:06 
AnswerRe: Want to encrypt text file? Pin
Atanu Maity7-Apr-08 3:32
memberAtanu Maity7-Apr-08 3:32 
GeneralNice Algorithm Pin
Sapana IR6-Apr-08 19:19
memberSapana IR6-Apr-08 19:19 
GeneralRe: Nice Algorithm Pin
Atanu Maity6-Apr-08 19:24
memberAtanu Maity6-Apr-08 19:24 
Questionlarge file issue? Pin
Dankarmy6-Apr-08 10:01
memberDankarmy6-Apr-08 10:01 
AnswerRe: large file issue? Pin
Atanu Maity6-Apr-08 19:10
memberAtanu Maity6-Apr-08 19:10 
GeneralRe: large file issue? Pin
Dankarmy7-Apr-08 12:29
memberDankarmy7-Apr-08 12:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151002.1 | Last Updated 5 Apr 2008
Article Copyright 2008 by Atanu Maity
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid