Click here to Skip to main content
15,884,176 members
Articles / Desktop Programming / Windows Forms
Article

Lock

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

Introduction<o:p>

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

Screenshots<o:p>

Screenshot - mainScreenShot.gif
<o:p>

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

History<o:p>

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)


Written By
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

 
GeneralYikes. Pin
Rich Insley27-Nov-07 9:13
Rich Insley27-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. Pin
Fun@learn27-Nov-07 17:25
Fun@learn27-Nov-07 17:25 
GeneralRe: Yikes. Pin
Rich Insley27-Nov-07 17:53
Rich Insley27-Nov-07 17:53 
GeneralRe: Yikes. Pin
Ri Qen-Sin27-Nov-07 18:29
Ri Qen-Sin27-Nov-07 18:29 
AnswerRe: Yikes. Pin
Fun@learn27-Nov-07 18:49
Fun@learn27-Nov-07 18:49 
GeneralRe: Yikes. Pin
Rich Insley28-Nov-07 6:08
Rich Insley28-Nov-07 6:08 
GeneralRe: Yikes. Pin
Ri Qen-Sin28-Nov-07 8:22
Ri Qen-Sin28-Nov-07 8:22 

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.