Click here to Skip to main content
15,891,787 members
Articles / Security / Encryption

Encrypting Editor Notepad Replacement

Rate me:
Please Sign up or sign in to vote.
4.85/5 (50 votes)
28 Jul 2014CPOL80 min read 178.8K   6.1K   112  
A C# .NET 3.5 (Win7) Windows Forms Application with source code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Messagebox_Class;

namespace CryptPad
{
	public partial class Encryption : Form
	{
		CryptPad Crypt_Pad;
		private string newstring;
		private bool verify;
		const string enter_new = "Enter a new encryption string.";
		const string enter_existing = "Enter the encryption string previously created for this file.";
		const string mismatch = "Mismatch! Try again.";

		public Encryption ()
		{
			InitializeComponent ();
			this.Font = SystemFonts.MessageBoxFont;
		}

		private void Encryption_Load (object sender, EventArgs e)
		{
			Crypt_Pad = (CryptPad) this.Owner;

			// encrypt checksum	action needed

			//	null	0		request new encryption string
			//	null	>0		request existing to match checksum (open)
			//	"--"	0		request old then change
			//	"--"	>0		verify match else request existing (open)

			if (null == Crypt_Pad.encryption_string)
			{
				if (0 == Crypt_Pad.encryption_checksum)
				{
					this.labelTop.Text = enter_new;
					verify = true;
				}
				else
				{
					this.labelTop.Text = enter_existing;
				}
			}
			else
				if (0 == Crypt_Pad.encryption_checksum)
				{
					this.labelTop.Text = "Enter the previous encryption string.";
				}
				else
				{
					if (Crypt_Pad.encryption_checksum == ChecksumXOR (Crypt_Pad.encryption_string))
					{
						Close ();
						return;
					}
					Crypt_Pad.encryption_string = null;
					this.labelTop.Text = enter_existing;
				}
		}

		private void buttonOK_Click (object sender, EventArgs e)
		{
			textBoxEncryption.Focus ();
			// query string for file open
			if (0 != Crypt_Pad.encryption_checksum)
			{
				if (Crypt_Pad.encryption_checksum != ChecksumXOR (textBoxEncryption.Text))
				{
					Messagebox.Show (mismatch);
					textBoxEncryption.Clear ();
					return;
				}
				Crypt_Pad.encryption_string = textBoxEncryption.Text;
				Crypt_Pad.random_seed = ChecksumAdd (Crypt_Pad.encryption_string);
				this.DialogResult = DialogResult.OK;
				return;
			}
			if (null == newstring)
			{
				if (verify)
				{
					int i = textBoxEncryption.Text.Length;
					if (0 != i && 4 > i)
					{
						Messagebox.Show ("Encryption string must be at least 4 characters!");
						return;
					}
					newstring = textBoxEncryption.Text;
					textBoxEncryption.Clear ();
					if (0 == i)
						this.labelTop.Text = "Verify removing encryption string.";
					else
						this.labelTop.Text = "Verify the new encryption string.";
					return;
				}
				if (textBoxEncryption.Text == Crypt_Pad.encryption_string)
				{
					this.labelTop.Text = enter_new;
					verify = true;
					textBoxEncryption.Clear ();
					return;
				}
				Messagebox.Show (mismatch);
				textBoxEncryption.Clear ();
				return;
			}
			if (newstring != textBoxEncryption.Text)
			{
				Messagebox.Show (mismatch);
				textBoxEncryption.Clear ();
				return;
			}
			Crypt_Pad.txtBody.Modified = true;
			if ("" == textBoxEncryption.Text)
			{
				Messagebox.Show ("Encryption string erased!");
				Crypt_Pad.encryption_string = null;
				Crypt_Pad.encryption_checksum = 0;
				Crypt_Pad.random_seed = 0;
			}
			else
			{
				Messagebox.Show ("New encryption string set!");
				Crypt_Pad.encryption_string = textBoxEncryption.Text;
				Crypt_Pad.encryption_checksum = ChecksumXOR (Crypt_Pad.encryption_string);
				Crypt_Pad.random_seed = ChecksumAdd (Crypt_Pad.encryption_string);
			}
			this.DialogResult = DialogResult.OK;
		}

		private void textBoxEncryption_KeyPress (object sender, KeyPressEventArgs e)
		{
			char chr = e.KeyChar;
			if (22!=chr && '\b' != chr && !Crypt_Pad.isASCII (chr))
			{
				e.Handled = true;
				Messagebox.Show ("Only ASCII characters are allowed.");
			}
		}

		// xor checksum for in the file header
		private uint ChecksumXOR (string s)
		{
			if (0 == s.Length)
				return 0;
			uint checksum = 0;
			int j = 32;
			if (32 < s.Length)
				j = s.Length;
			for (int i = 0; i < j; ++i)
			{
				uint t = 0;
				if (0 < (0x80000000 & checksum))
					t = 1;
				checksum <<= 1;
				checksum |= t;
				checksum ^= s[i % s.Length];
			}
			return checksum;
		}

		// plus checksum for randomization
		private int ChecksumAdd (string s)
		{
			int checksum = 0;
			for (int i = 0; i < s.Length; ++i)
			{
				checksum += s[i];
			}
			return checksum;
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Systems Engineer IAUA End Time Ministry
United States United States
I am a retired Software Systems Design Engineer experienced with IEEE standards and the entire IEEE software development life cycle. Concept Exploration, Requirements, Design, Implementation, Test, Installation and Checkout, Operation and Maintenance. I enjoy working with people and solving customer problems.

I am currently a writer for my personal ministry: IAUA End Time Ministry

Comments and Discussions