Click here to Skip to main content
15,886,693 members
Articles / Mobile Apps

Tiny Encryption Algorithm (TEA) for the Compact Framework

Rate me:
Please Sign up or sign in to vote.
4.57/5 (41 votes)
29 Feb 2004CPOL3 min read 291.5K   3K   66  
Learn how to secure sensitive data using TEA encryption.
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;

namespace TeaCrypto
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button cmdEncryptDecrypt;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox txtKey;
		private System.Windows.Forms.TextBox txtData;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.MainMenu mainMenu1;
		string cipher;

		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 )
		{
			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.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.cmdEncryptDecrypt = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.txtKey = new System.Windows.Forms.TextBox();
			this.txtData = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			// 
			// cmdEncryptDecrypt
			// 
			this.cmdEncryptDecrypt.Location = new System.Drawing.Point(8, 144);
			this.cmdEncryptDecrypt.Size = new System.Drawing.Size(120, 40);
			this.cmdEncryptDecrypt.Text = "Encrypt";
			this.cmdEncryptDecrypt.Click += new System.EventHandler(this.cmdEncryptDecrypt_Click);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 8);
			this.label1.Size = new System.Drawing.Size(36, 16);
			this.label1.Text = "Key:";
			// 
			// txtKey
			// 
			this.txtKey.Location = new System.Drawing.Point(44, 4);
			this.txtKey.Size = new System.Drawing.Size(184, 22);
			this.txtKey.Text = "0123456789ABCDEF";
			// 
			// txtData
			// 
			this.txtData.Location = new System.Drawing.Point(8, 48);
			this.txtData.Multiline = true;
			this.txtData.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.txtData.Size = new System.Drawing.Size(220, 88);
			this.txtData.Text = "This is a test string!";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 32);
			this.label2.Size = new System.Drawing.Size(76, 16);
			this.label2.Text = "Data:";
			// 
			// Form1
			// 
			this.Controls.Add(this.label2);
			this.Controls.Add(this.txtData);
			this.Controls.Add(this.txtKey);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.cmdEncryptDecrypt);
			this.Menu = this.mainMenu1;
			this.MinimizeBox = false;
			this.Text = "Tea Encryption";

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>

		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void cmdEncryptDecrypt_Click(object sender, System.EventArgs e)
		{
			XTea t = new XTea();
			if(cmdEncryptDecrypt.Text == "Encrypt")
			{
				cipher = t.EncryptString(txtData.Text, txtKey.Text);
				txtData.ReadOnly = true;
				txtData.Text = cipher;
			}
			else
			{
				txtData.Text = t.Decrypt(cipher, txtKey.Text);
				txtData.ReadOnly = false;
			}

			cmdEncryptDecrypt.Text = (cmdEncryptDecrypt.Text == "Encrypt")?"Decrypt":"Encrypt";
		}
	}
}

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
Software Developer (Senior)
United States United States
Check out my blog! http://www.pagebrooks.com

Comments and Discussions