Click here to Skip to main content
Licence CPOL
First Posted 5 Apr 2008
Views 34,695
Downloads 526
Bookmarked 30 times

P2P Secure File, Encrypt and Decrypt Any File with Password

By | 5 Apr 2008 | Article
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.

 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", _
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.

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.

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)

About the Author

Atanu Maity

CEO
Programmer2Programmer
India India

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberShubhranshu from Mumba19:28 1 Apr '12  
GeneralMy vote of 4 PinmemberMember 43208441:13 15 Aug '11  
Questionwhat algorithm this project use? PinmemberNURFAEZAH ABDUL MALIK5:01 19 Oct '10  
Generalblowing my own horn a bit sorry PinmemberMartyK20070:42 4 Jun '08  
Generalsorry, this code does not encrypt any files, it encrypts the specified password only PinmemberAlexey Yakovlev6:55 9 Apr '08  
GeneralRe: sorry, this code does not encrypt any files, it encrypts the specified password only PinmemberAtanu Maity18:56 9 Apr '08  
GeneralRe: sorry, this code does not encrypt any files, it encrypts the specified password only PinmemberAlexey Yakovlev5:50 11 Apr '08  
OK, here you go. I've used P2P Secure File program to "encrypt" your avatar picture, then run this:
 

Module Module1
Sub Main()
Dim FileName As String
FileName = "C:\TEMP\atanu.gif.sp2p"
 
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)
 
'>>> read the content from the xml file
DsImg.ReadXml(FileName)
 
'>>> remove sp2p from the file name before save
FileName = Mid(FileName, 1, Len(FileName) - 5)
 
'>>> and write to file stream
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()
End Sub
End Module
 

All I'm trying to say is, it's a nice sample code that shows how to XOR a string, but the article title is misleading. It should NOT be called "Secure File program" because file IS NOT secure as it IS NOT encrypted in any way.
GeneralRe: sorry, this code does not encrypt any files, it encrypts the specified password only PinmemberAtanu Maity21:12 11 Apr '08  
GeneralAwesome Pinmembersuresh suthar3:18 9 Apr '08  
GeneralRe: Awesome PinmemberAbhijit Jana1:43 10 May '08  
GeneralThank You PinmemberprabhaChoudhary20:35 7 Apr '08  
GeneralRe: Thank You PinmemberAtanu Maity21:52 8 Apr '08  
Questionthank you Pinmemberalaa masarweh5:16 7 Apr '08  
GeneralRe: thank you PinmemberAtanu Maity7:10 7 Apr '08  
GeneralToo good... PinmemberSushilck3:11 7 Apr '08  
AnswerRe: Too good... PinmemberAtanu Maity3:34 7 Apr '08  
QuestionWant to encrypt text file? PinmemberJoy_Dass19813:06 7 Apr '08  
AnswerRe: Want to encrypt text file? PinmemberAtanu Maity3:32 7 Apr '08  
GeneralNice Algorithm PinmemberSapana IR19:19 6 Apr '08  
GeneralRe: Nice Algorithm PinmemberAtanu Maity19:24 6 Apr '08  
Questionlarge file issue? PinmemberDankarmy10:01 6 Apr '08  
AnswerRe: large file issue? PinmemberAtanu Maity19:10 6 Apr '08  
GeneralRe: large file issue? PinmemberDankarmy12:29 7 Apr '08  

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.

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