Click here to Skip to main content
15,393,863 members
Articles / Desktop Programming / Windows Forms
Posted 27 Nov 2007


8 bookmarked


Rate me:
Please Sign up or sign in to vote.
1.43/5 (11 votes)
27 Nov 2007CPOL1 min read
LOCK is used to lock and unlock the files using symmetric key cryptography


This article shows a classical approach to lock a file so that it doesn't reveal its original content in any software. For locking a file, a password is required. To view the file again in its original form, it has to be unlocked with the LOCK software with the same password which was used to lock the file.

Background <o:p>

The basic approach used is the symmetric key cryptography. The file is converted into blocks of 8 bytes and repeatedly XORed with the similar block of 8 bytes obtained by the password. Because the encryption used is symmetric key cryptography, the same process is used for unlocking\decryption.

Using the code<o:p>

Following is the code for main encryption routine. For more details, look at source code for the comments.

// Replicate the byte array in the key buffer to create
         // an encryption key whose size equals or exceeds the
         // size of the I/O buffer
         int count = (1024 + keyBytes.Length-1)/keyBytes.Length;
                 for( int i=0 ; i<count ; i++)
                 Array.Copy( keyBytes , 0 , keybuf , i*keyBytes.Length , keyBytes.Length);
        // Read the file in bufsize blocks, XOR-encrypt each block,
         // and write the encrypted block back to the file
         long lBytesRemaining = stream.Length;
         while (lBytesRemaining>0)
                 long lPosition = stream.Position ;
                 int nBytesRequested = (int)System.Math.Min(bufSize , lBytesRemaining); 
                 int nBytesRead = reader.Read (buffer, 0, nBytesRequested)

                 for (int i=0; i<nBytesRead; i++)
                          buffer[i] ^= keybuf[i];Stream.Seek (lPosition, SeekOrigin.Begin);
                          writer.Write (buffer, 0, nBytesRead);
                          lBytesRemaining -= nBytesRead;


Screenshot - mainScreenShot.gif

Points of Interest<o:p>

This is my second article at code project. For the first article which about Handling the Big Numbers, click here. I donot have much experience of article writing. I have just tried to minimize the shortcomings I have faced drafting the First article. Suggestion regarding the clarity, confusion, code, technical writing are welcomed. Comments are also welcomed. You can also mail me at with subject Lock Code Project Article regarding the same.


This is the first version of the program. I have some inputs for the next version of the code. I am expecting some more inputs from the readers of the code. After finalizing all the inputs I would write next version of the code.

If you feel any shortcoming of this code/article, please leave a message about the same, so that these pitfalls may not appear in the subsequent versions.


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


About the Author

Software Developer (Senior)
India India
Software Engineer based out in Noida.

Technology skillset – .NET, WPF, WCF, LINQ, XAML.

Started blogging on

Stackoverflow Profile ->

Comments and Discussions

GeneralMy vote of 1 Pin
geniaz16-Oct-09 12:41
Membergeniaz16-Oct-09 12:41 
QuestionWhat is this? Pin
Ri Qen-Sin27-Nov-07 11:27
MemberRi Qen-Sin27-Nov-07 11:27 
AnswerRe: What is this? Pin
Fun@learn27-Nov-07 17:20
MemberFun@learn27-Nov-07 17:20 
GeneralYikes. Pin
Rich Insley27-Nov-07 9:13
MemberRich Insley27-Nov-07 9:13 
GeneralRe: Yikes. Pin
Fun@learn27-Nov-07 17:25
MemberFun@learn27-Nov-07 17:25 
GeneralRe: Yikes. Pin
Rich Insley27-Nov-07 17:53
MemberRich Insley27-Nov-07 17:53 
GeneralRe: Yikes. Pin
Ri Qen-Sin27-Nov-07 18:29
MemberRi Qen-Sin27-Nov-07 18:29 
AnswerRe: Yikes. Pin
Fun@learn27-Nov-07 18:49
MemberFun@learn27-Nov-07 18:49 
either way it is the same.This is not something which has not appeared in my research before writing the code,In fact I started with this code, did some investigation and finally arrived to the current code which is again open to investigation.
If you would see, my code and the code snippet you have presented appears to be same wheteher you take is performance wise or method wise . Rather than improvement, it is just a restructring of the code.
Regarding the buffer size, if a file buffer of 3 gig can be made, than another buffer for the key can be made.
though for encrypting such a long file, some approach regarding the small chunks is appeared.
Anyway, the improvements shown would appear in the updates of the code

Integers are never a part of the algorithm apart from the arraysize or counters.
So I could not get your point.
As far as remaining bytes are concerned, i am looking for such a approach which considers the smaller chunk one at a time rather than all the file at a time.
Here the main issue is file handling rather than the encryption algorithm.
Though algorithm would also reflect the improvements.

destiny is the mirror of the past

GeneralRe: Yikes. Pin
Rich Insley28-Nov-07 6:08
MemberRich Insley28-Nov-07 6:08 
GeneralRe: Yikes. Pin
Ri Qen-Sin28-Nov-07 8:22
MemberRi Qen-Sin28-Nov-07 8:22 
Jokeloooool Pin
LegionFX27-Nov-07 8:38
MemberLegionFX27-Nov-07 8:38 
GeneralRe: loooool Pin
LegionFX27-Nov-07 8:39
MemberLegionFX27-Nov-07 8:39 
GeneralRe: loooool Pin
Fun@learn28-Nov-07 6:10
MemberFun@learn28-Nov-07 6:10 
QuestionImageViewer? Pin
canozurdo27-Nov-07 7:48
Membercanozurdo27-Nov-07 7:48 
AnswerRe: ImageViewer? Pin
Fun@learn27-Nov-07 17:28
MemberFun@learn27-Nov-07 17:28 
GeneralRe: ImageViewer? Pin
canozurdo28-Nov-07 1:37
Membercanozurdo28-Nov-07 1:37 
AnswerRe: ImageViewer? Pin
Fun@learn28-Nov-07 6:07
MemberFun@learn28-Nov-07 6:07 

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

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