Row Transposition cipher - encryption-decryption Csharp(C#)

Fri May 09, 2008 1:11 pm

Row transposition Security Algorithm code in C# ,you can download it freely.
csharp code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Row_transposition
{
public partial class Form1 : Form
{
static int x1 = -266;
string key="";
string plaintxt="";
string ciphertxt="";
string pureplaintxt="";
string purekey="";
int[] intKey;
int indexofmin;
int matrixRow, matrixColumn;
int index = 0;
string[,] matirxLetter;
public Form1()
{
InitializeComponent();
}
/*/////////////////////////////////////////*/
// initialize the variables
public void initialize()
{
key = "";
plaintxt = "";
ciphertxt = "";
pureplaintxt = "";
purekey = "";
index = 0;
}

/*/////////////////////////////////////////*/
// fill matrix with spaces
public void fillMatrix()
{
for (int i = 0; i < matrixRow; i++)
{
for (int j = 0; j < matrixColumn; j++)
{
matirxLetter[i, j] = " ";
}
}
}
/*/////////////////////////////////////////*/
// remove spaces from key and plain text
public string removespaces(string var)
{
string purevar = "";
for (int i = 0; i < var.Length; i++)
{
if (var.Substring(i, 1) != " ")
{
purevar += var.Substring(i, 1);
}
}
return purevar;
}
/*/////////////////////////////////////////*/
// get the index of the minimum number in key
public int getIndexOfMin(int min)
{
int indexMin = 0;
for (int j = 0; j < intKey.Length; j++)
{
if (min == intKey[j])
{
indexMin = j;
}
}
return indexMin;
}
/*/////////////////////////////////////////*/
// get the index of the minimum number in key
public int getIndexOfMin()
{
int min = 100;
int indexMin = 0;
for (int j = 0; j < intKey.Length; j++)
{
if (min > intKey[j])
{
min = intKey[j];
indexMin = j;
}
}
return indexMin;
}
/*//////////////////////////////////////////*/
public int getMin()
{
int min=100;
for (int j = 0; j < intKey.Length; j++)
{
if (min > intKey[j])
{
min = intKey[j];
}
}
return min;
}
/*//////////////////////////////////////////*/
// encrypte the plain text
public void encrypte()
{
intKey = new int[purekey.Length];

for (int i = 0; i < purekey.Length; i++)
{
intKey[i] = int.Parse(purekey.Substring(i, 1));
}
for (int k = 0; k < intKey.Length; k++)
{
indexofmin = getIndexOfMin();
intKey[indexofmin] = 100;
for (int x = 0; x < matrixRow; x++)
{
ciphertxt += matirxLetter[x, indexofmin];
}
}
}
/*//////////////////////////////////////////*/
// decrypte the cipher text
public void decrypte()
{
int min;
int position;
intKey = new int[purekey.Length];
for (int i = 0; i < purekey.Length; i++)
{
intKey[i] = int.Parse(purekey.Substring(i, 1));
}
for (int h = 0; h < matrixRow; h++)
{
position = 0;
for (int x = 0; x < matrixColumn; x++)
{
min = getMin() + position;
indexofmin = getIndexOfMin(min);
plaintxt += matirxLetter[h, indexofmin];
position++;
}
}
}
/*//////////////////////////////////////////*/
private void encrypteToolStripMenuItem_Click(object sender, EventArgs e)
{
fillMatrix();
textBox3.Text = "";
key = textBox1.Text;
plaintxt = textBox2.Text;
purekey = removespaces(key);
pureplaintxt = removespaces(plaintxt);
matrixColumn = purekey.Length;
//calculate the diminsition of the matrix letter
if (pureplaintxt.Length % purekey.Length != 0)
{
matrixRow = (pureplaintxt.Length / purekey.Length) + 1;
}
else
{
matrixRow = pureplaintxt.Length / purekey.Length;
}
matirxLetter = new string[matrixRow, matrixColumn];
// fill Matrix of letter with the plain text character
for (int i = 0; i < matrixRow; i++)
{
for (int j = 0; j < matrixColumn; j++)
{
if (index < pureplaintxt.Length)
{
matirxLetter[i, j] = pureplaintxt.Substring(index, 1);
index++;
}
else
{
matirxLetter[i, j] = " ";
}
}
}
encrypte();
textBox3.Text = ciphertxt;
initialize();
}

private void decrypteToolStripMenuItem_Click(object sender, EventArgs e)
{
fillMatrix();
index = 0;
textBox2.Text = "";
key = textBox1.Text;
ciphertxt = textBox3.Text;
purekey = removespaces(key);
matrixColumn = purekey.Length;
//calculate the diminsition of the matrix letter
if (ciphertxt.Length % purekey.Length != 0)
{
matrixRow = (ciphertxt.Length / purekey.Length) + 1;
}
else
{
matrixRow = ciphertxt.Length / purekey.Length;
}
matirxLetter = new string[matrixRow, matrixColumn];
// fill Matrix of letter with the cipher text character
for (int i = 0; i < matrixColumn; i++)
{
for (int j = 0; j < matrixRow; j++)
{
matirxLetter[j, i] = ciphertxt.Substring(index, 1);
index++;
}
}
decrypte();
textBox2.Text = plaintxt;
initialize();
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
menuStrip1.Enabled = false;
}
else
{
menuStrip1.Enabled = true;
}
}

private void Form1_Load(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
menuStrip1.Enabled = false;
}
else
{
menuStrip1.Enabled = true;
}
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult exit;
exit = MessageBox.Show("Are u sure ? ","Exit",MessageBoxButtons.YesNo);
if (exit == DialogResult.Yes)
{
MessageBox.Show("Thank for using my program ","Goooood bye");
Application.ExitThread();
}
else
{
e.Cancel = true;
}
}

private void timer1_Tick(object sender, EventArgs e)
{
DateTime.Now.TimeOfDay.Milliseconds.ToString();
x1 += 5;
Point p1 = new Point(x1, 266);
if (x1 >= 257)
{
x1 = -266;
p1.X = x1;
label4.Location = p1;

}
else
{
label4.Location = p1;
}
}
}
}


csharp code
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Row_transposition
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}


csharp code
namespace Row_transposition
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.components = new System.ComponentModel.Container();
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.encrypteToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.decrypteToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.label4 = new System.Windows.Forms.Label();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
//
// menuStrip1
//
this.menuStrip1.BackColor = System.Drawing.SystemColors.Control;
this.menuStrip1.GripStyle = System.Windows.Forms.ToolStripGripStyle.Visible;
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.encrypteToolStripMenuItem,
this.decrypteToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System;
this.menuStrip1.Size = new System.Drawing.Size(292, 24);
this.menuStrip1.TabIndex = 0;
this.menuStrip1.Text = "menuStrip1";
//
// encrypteToolStripMenuItem
//
this.encrypteToolStripMenuItem.Name = "encrypteToolStripMenuItem";
this.encrypteToolStripMenuItem.Size = new System.Drawing.Size(62, 20);
this.encrypteToolStripMenuItem.Text = "Encrypte";
this.encrypteToolStripMenuItem.Click += new System.EventHandler(this.encrypteToolStripMenuItem_Click);
//
// decrypteToolStripMenuItem
//
this.decrypteToolStripMenuItem.Name = "decrypteToolStripMenuItem";
this.decrypteToolStripMenuItem.Size = new System.Drawing.Size(63, 20);
this.decrypteToolStripMenuItem.Text = "Decrypte";
this.decrypteToolStripMenuItem.Click += new System.EventHandler(this.decrypteToolStripMenuItem_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 45);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 1;
this.label1.Text = "Key :";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(88, 42);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(183, 20);
this.textBox1.TabIndex = 2;
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 85);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(65, 13);
this.label2.TabIndex = 3;
this.label2.Text = "Plain text : ";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(88, 82);
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(183, 69);
this.textBox2.TabIndex = 4;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(12, 173);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(74, 13);
this.label3.TabIndex = 5;
this.label3.Text = "Cipher text : ";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(88, 170);
this.textBox3.Multiline = true;
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(183, 78);
this.textBox3.TabIndex = 6;
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// label4
//
this.label4.AutoSize = true;
this.label4.ForeColor = System.Drawing.Color.Blue;
this.label4.Location = new System.Drawing.Point(-266, 271);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(264, 13);
this.label4.TabIndex = 32;
this.label4.Text = "ÇáÍãÏ ááå ÇáÐí åÏÇäÇ áåÐÇ æ ãÇ ßäÇ áäåÊÏí áæáÇ Çä åÏÇäÇ Çááå";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 293);
this.Controls.Add(this.label4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.label3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.menuStrip1);
this.ForeColor = System.Drawing.Color.Blue;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MainMenuStrip = this.menuStrip1;
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Row Transposition";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
this.Load += new System.EventHandler(this.Form1_Load);
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem encrypteToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem decrypteToolStripMenuItem;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Label label4;
}
}



Attachments
Row transposition.rar
(40.6 KiB) Downloaded 1573 times

Re: Row Transposition cipher - encryption-decryption Csharp(C#)

Mon Jan 21, 2013 9:43 pm

updated.

  Related Posts  to : Row Transposition cipher - encryption-decryption Csharp(C#)
 Row Transposition cipher - encryption-decryption java     -  
 Encryption and Decryption encryption Affine cipher code     -  
 RSA encryption decryption cipher algorithm java     -  
 Ceasar encryption-decryption-cipher-decipher code     -  
 program for transposition encryption     -  
 program for transposition encryption     -  
 How Row Transposition Encryption Algorithm Work     -  
 encryption and decryption in c++     -  
 encryption/ decryption without key using C++     -  
 codes for encryption/decryption algorithms     -  

Topic Tags

C# Algorithms