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>
/// own created field
/// </summary>
About abt;
String file;
String password ;
/// <summary>
/// system created
/// </summary>
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.OpenFileDialog ofd;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
abt = new About(this);
}
/// <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.ofd = new System.Windows.Forms.OpenFileDialog();
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;
this.ffield.TextChanged += new System.EventHandler(this.ffield_TextChanged);
//
// 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;
this.pass.TextChanged += new System.EventHandler(this.pass_TextChanged);
//
// 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 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 ; 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;
}
MessageBox.Show(fileName + " is crypted");
}
catch (Exception x)
{
Console.WriteLine(x.Message) ;
}
finally
{
if (stream != null)
stream.Close ();
}
}
/// <summary>
///
/// </summary>
int FilterIndex = -1;
private void button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter= "Image Files (JPG,JPEG, GIF, BMP,TIFF,PNG, etc.)|*.jpg;*.jpeg;*.gif;*.bmp;*.tif;*.tiff;*.png|" +
"Executable Files ( *.bat, *.com, *.exe)|*.bat;*.com;*.exe|"+
"All files (*.*)|*.*";
if (FilterIndex != -1)
ofd.FilterIndex = FilterIndex;
if (ofd.ShowDialog() == DialogResult.OK)
{
string FileName = ofd.FileName ;
if (FileName.Length !=0)
{
FilterIndex = ofd.FilterIndex ;
try
{
ffield.Text = FileName ;
file = FileName ;
string [] parts = FileName.Split('\\');
Text = " ImageViewer - " + parts[parts.Length-1];
Invalidate();
}
catch(ArgumentException)
{
MessageBox.Show(String.Format
( "{0} is not a valid image file" , FileName) , "Error" ,
MessageBoxButtons.OK , MessageBoxIcon.Error);
}
}
}
}
private void ffield_TextChanged(object sender, System.EventArgs e)
{
file = ffield.Text ;
}
private void pass_TextChanged(object sender, System.EventArgs e)
{
password = pass.Text ;
}
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)
{
abt.Show();
}
}
}