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

Lock

By , 27 Nov 2007
Rate this:
Please Sign up or sign in to vote.

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 
I don't even know where to begin on this one. Just copy and paste this into Form1.cs and I'll let you figure it out.
 
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Text;
 
namespace Lock
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>

    public class Form1 : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;
 
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox ffield;
        private System.Windows.Forms.TextBox pass;
        private System.Windows.Forms.TextBox c_pass;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Button button4;
        private System.Windows.Forms.Button button5;
 
        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
 
            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }
 
        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.ffield = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.label2 = new System.Windows.Forms.Label();
            this.pass = new System.Windows.Forms.TextBox();
            this.button3 = new System.Windows.Forms.Button();
            this.label3 = new System.Windows.Forms.Label();
            this.c_pass = new System.Windows.Forms.TextBox();
            this.button4 = new System.Windows.Forms.Button();
            this.button5 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(24, 24);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(112, 16);
            this.label1.TabIndex = 0;
            this.label1.Text = "Enter File Name";
            // 
            // ffield
            // 
            this.ffield.BackColor = System.Drawing.SystemColors.Info;
            this.ffield.Location = new System.Drawing.Point(152, 24);
            this.ffield.Name = "ffield";
            this.ffield.Size = new System.Drawing.Size(152, 20);
            this.ffield.TabIndex = 1;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(328, 16);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(112, 24);
            this.button1.TabIndex = 2;
            this.button1.Text = "Browse";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(24, 136);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(120, 24);
            this.button2.TabIndex = 5;
            this.button2.Text = "Encrypt/Decrypt";
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(24, 64);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(112, 24);
            this.label2.TabIndex = 4;
            this.label2.Text = "Enter Password";
            // 
            // pass
            // 
            this.pass.BackColor = System.Drawing.SystemColors.Info;
            this.pass.Location = new System.Drawing.Point(152, 56);
            this.pass.Name = "pass";
            this.pass.PasswordChar = '*';
            this.pass.Size = new System.Drawing.Size(152, 20);
            this.pass.TabIndex = 3;
            // 
            // button3
            // 
            this.button3.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.button3.Location = new System.Drawing.Point(304, 136);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(104, 24);
            this.button3.TabIndex = 6;
            this.button3.Text = "Exit";
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(24, 96);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(112, 24);
            this.label3.TabIndex = 7;
            this.label3.Text = "Confirm Password";
            // 
            // c_pass
            // 
            this.c_pass.BackColor = System.Drawing.SystemColors.Info;
            this.c_pass.Location = new System.Drawing.Point(152, 88);
            this.c_pass.Name = "c_pass";
            this.c_pass.PasswordChar = '+';
            this.c_pass.Size = new System.Drawing.Size(152, 20);
            this.c_pass.TabIndex = 4;
            // 
            // button4
            // 
            this.button4.Location = new System.Drawing.Point(328, 56);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(112, 24);
            this.button4.TabIndex = 8;
            this.button4.Text = "Clear";
            this.button4.Click += new System.EventHandler(this.button4_Click);
            // 
            // button5
            // 
            this.button5.Location = new System.Drawing.Point(160, 136);
            this.button5.Name = "button5";
            this.button5.Size = new System.Drawing.Size(120, 24);
            this.button5.TabIndex = 9;
            this.button5.Text = "About";
            this.button5.Click += new System.EventHandler(this.button5_Click);
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))));
            this.ClientSize = new System.Drawing.Size(456, 190);
            this.Controls.Add(this.button5);
            this.Controls.Add(this.button4);
            this.Controls.Add(this.c_pass);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.pass);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.ffield);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Lock File";
            this.ResumeLayout(false);
            this.PerformLayout();
 
        }
        #endregion
 
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }
 
        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 is empty");
                return;
            }
 
            if ((pass.Text != c_pass.Text))
            {
                MessageBox.Show("Passwords do not match");
                return;
            }
 
            if (!File.Exists(ffield.Text))
            {
                MessageBox.Show("File " + ffield.Text + " does not exist");
                return;
            }
 
            try
            {
                byte[] keyBytes = ASCIIEncoding.ASCII.GetBytes(pass.Text);
                byte[] fileBuffer = File.ReadAllBytes(ffield.Text);
                for (int i = 0; i < fileBuffer.Length - 1; i++)
                {
                    fileBuffer[i] ^= keyBytes[i % keyBytes.Length];
                }
                File.WriteAllBytes(ffield.Text, fileBuffer);
            }
            catch (IOException ex)
            {
                MessageBox.Show(ex.Message, "IO Error");
            }
        }
 
        private void button1_Click(object sender, System.EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                Text = "Lock File - " + Path.GetFileName(ofd.FileName);
                ffield.Text = ofd.FileName;
            }
            ofd.Dispose();
        }
 
        private void button3_Click(object sender, System.EventArgs e)
        {
            Application.Exit();
        }
 
        private void button4_Click(object sender, System.EventArgs e)
        {
            ffield.Text = "";
            pass.Text = "";
            c_pass.Text = "";
        }
 
        private void button5_Click(object sender, EventArgs e)
        {
            About abt = new About(this);
            abt.ShowDialog();
            abt.Dispose();
        }
    }
}

GeneralRe: Yikes. PinmemberFun@learn27-Nov-07 17:25 
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
Web03 | 2.8.140415.2 | Last Updated 27 Nov 2007
Article Copyright 2007 by Fun@learn
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid