Click here to Skip to main content
Click here to Skip to main content

Lock

, 27 Nov 2007
Rate this:
Please Sign up or sign in to vote.
LOCK is used to lock and unlock the files using symmetric key cryptography

Introduction

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

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

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;
            }                   

Screenshots

Screenshot - mainScreenShot.gif

Points of Interest

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 funatlearn@ontheedge.co.in with subject Lock Code Project Article regarding the same.

History

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.

License

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

About the Author

Fun@learn
Software Developer (Senior)
India India
Software Engineer based out in Noida.
 
Technology skillset – .NET, WPF, WCF, LINQ, XAML.
 
Started blogging on http://1wpf.wordpress.com/
 

Stackoverflow Profile -> http://stackoverflow.com/users/649524/tilak

Comments and Discussions

 
GeneralMy vote of 1 Pinmembergeniaz16-Oct-09 12:41 
QuestionWhat is this? PinmemberRi Qen-Sin27-Nov-07 11:27 
AnswerRe: What is this? PinmemberFun@learn27-Nov-07 17:20 
GeneralYikes. PinmemberJohnnyLocust27-Nov-07 9:13 
GeneralRe: Yikes. PinmemberFun@learn27-Nov-07 17:25 
you have missed some part of the code while copying and pasting, which contains the real logic
 
I am posting the part you have missed. Please check the code below in your file
 

 
private void button2_Click(object sender, System.EventArgs e)
{
if(ffield.Text=="" )
{
MessageBox.Show("File Name is empty");
return;
}
if(pass.Text=="" )
{
MessageBox.Show("Password Can't be Null");
return;
}
 
if((pass.Text!=c_pass.Text)||pass.Text=="")
{
MessageBox.Show("password doesnot Match");
return;
}

const int bufSize =1048576 ;
String fileName = file;
String key = password;
FileStream stream = null;
try
{
try
{
// open file for reading & writing
stream = File.Open(fileName , FileMode.Open ,FileAccess.ReadWrite ) ;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
throw;
}
// wrap a reader & writer around the file
BinaryReader reader = new BinaryReader(stream);
BinaryWriter writer = new BinaryWriter(stream);

// convert the key to byte array
ASCIIEncoding enc = new ASCIIEncoding ();
byte[] keyBytes = enc.GetBytes (key);
 

// Allocate an I/O buffer & key buffer
byte[] buffer = new byte[bufSize];
byte[] keybuf = new byte[bufSize + keyBytes.Length - 1];

// 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 ; i0)
{
long lPosition = stream.Position ;
int nBytesRequested = (int)System.Math.Min(bufSize , lBytesRemaining) ;
int nBytesRead = reader.Read (buffer, 0, nBytesRequested);
 
for (int i=0; i MessageBox.Show(fileName + " is crypted");
}
catch (Exception x)
{
Console.WriteLine(x.Message) ;
}
finally
{
if (stream != null)
stream.Close ();
}
}
 
Fun@learn
--------------------------------
destiny is the mirror of the past

GeneralRe: Yikes. PinmemberJohnnyLocust27-Nov-07 17:53 
GeneralRe: Yikes. PinmemberRi Qen-Sin27-Nov-07 18:29 
AnswerRe: Yikes. PinmemberFun@learn27-Nov-07 18:49 
GeneralRe: Yikes. PinmemberJohnnyLocust28-Nov-07 6:08 
GeneralRe: Yikes. PinmemberRi Qen-Sin28-Nov-07 8:22 
Jokeloooool PinmemberLegionFX27-Nov-07 8:38 
GeneralRe: loooool PinmemberLegionFX27-Nov-07 8:39 
GeneralRe: loooool PinmemberFun@learn28-Nov-07 6:10 
QuestionImageViewer? Pinmembercanozurdo27-Nov-07 7:48 
AnswerRe: ImageViewer? PinmemberFun@learn27-Nov-07 17:28 
GeneralRe: ImageViewer? Pinmembercanozurdo28-Nov-07 1:37 
AnswerRe: ImageViewer? PinmemberFun@learn28-Nov-07 6:07 

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 | Mobile
Web04 | 2.8.140721.1 | Last Updated 27 Nov 2007
Article Copyright 2007 by Fun@learn
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid