Total members 11895 |It is currently Mon Dec 30, 2024 7:07 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Contain the Data Encryption Standard (DES) Security Algorithm implementation using J#( Similar to C#) .You can download the code and try it .
csharp code
package DES;

import System.Collections.Generic.*;
import System.Data.*;
import System.Drawing.*;
import System.ComponentModel.*;
import System.Windows.Forms.*;
import System.Collections.ArrayList;

/**
* Summary description for Form1.
*/
public class Form1 extends System.Windows.Forms.Form
{
private TextBox textBox1;
private TextBox textBox2;
private TextBox textBox3;
private Label label1;
private Label label2;
private Label label3;
private Timer timer1;
private static int x1 = -266;
private static int step = 0;
private Label label4;
private MenuStrip menuStrip1;
private ToolStripMenuItem encrypteToolStripMenuItem;
private ToolStripMenuItem decrypteToolStripMenuItem;
private ToolStripMenuItem backColorToolStripMenuItem;
private ColorDialog colorDialog1;
/**
* Required designer variable.
*/
private System.ComponentModel.IContainer components;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

#region Windows Form Designer generated code
/**
* Clean up any resources being used.
*/
protected void Dispose(boolean disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
super.Dispose(disposing);
}

/**
* Required method for Designer support - do not modify
* the contents of this method with the code editor.
*/
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.label4 = new System.Windows.Forms.Label();
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.encrypteToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.decrypteToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.backColorToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.colorDialog1 = new System.Windows.Forms.ColorDialog();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.set_ForeColor(System.Drawing.Color.get_Blue());
this.textBox1.set_Location(new System.Drawing.Point(82, 43));
this.textBox1.set_Multiline(true);
this.textBox1.set_Name("textBox1");
this.textBox1.set_ScrollBars(System.Windows.Forms.ScrollBars.Vertical);
this.textBox1.set_Size(new System.Drawing.Size(179, 66));
this.textBox1.set_TabIndex(0);
//
// textBox2
//
this.textBox2.set_ForeColor(System.Drawing.Color.get_Blue());
this.textBox2.set_Location(new System.Drawing.Point(82, 118));
this.textBox2.set_Multiline(true);
this.textBox2.set_Name("textBox2");
this.textBox2.set_ScrollBars(System.Windows.Forms.ScrollBars.Vertical);
this.textBox2.set_Size(new System.Drawing.Size(179, 68));
this.textBox2.set_TabIndex(1);
//
// textBox3
//
this.textBox3.set_ForeColor(System.Drawing.Color.get_Blue());
this.textBox3.set_Location(new System.Drawing.Point(82, 195));
this.textBox3.set_Multiline(true);
this.textBox3.set_Name("textBox3");
this.textBox3.set_ScrollBars(System.Windows.Forms.ScrollBars.Vertical);
this.textBox3.set_Size(new System.Drawing.Size(179, 71));
this.textBox3.set_TabIndex(2);
//
// label1
//
this.label1.set_AutoSize(true);
this.label1.set_ForeColor(System.Drawing.Color.get_Blue());
this.label1.set_Location(new System.Drawing.Point(12, 46));
this.label1.set_Name("label1");
this.label1.set_Size(new System.Drawing.Size(31, 13));
this.label1.set_TabIndex(5);
this.label1.set_Text("Key :");
//
// label2
//
this.label2.set_AutoSize(true);
this.label2.set_ForeColor(System.Drawing.Color.get_Blue());
this.label2.set_Location(new System.Drawing.Point(8, 121));
this.label2.set_Name("label2");
this.label2.set_Size(new System.Drawing.Size(56, 13));
this.label2.set_TabIndex(6);
this.label2.set_Text("Plain text :");
//
// label3
//
this.label3.set_AutoSize(true);
this.label3.set_ForeColor(System.Drawing.Color.get_Blue());
this.label3.set_Location(new System.Drawing.Point(8, 198));
this.label3.set_Name("label3");
this.label3.set_Size(new System.Drawing.Size(63, 13));
this.label3.set_TabIndex(7);
this.label3.set_Text("Cipher text :");
//
// timer1
//
this.timer1.set_Enabled(true);
this.timer1.add_Tick(new System.EventHandler(this.timer1_Tick));
//
// label4
//
this.label4.set_AutoSize(true);
this.label4.set_ForeColor(System.Drawing.Color.get_Blue());
this.label4.set_Location(new System.Drawing.Point(8, 278));
this.label4.set_Name("label4");
this.label4.set_Size(new System.Drawing.Size(337, 13));
this.label4.set_TabIndex(33);
this.label4.set_Text("ÇáÍãÏ ááå ÇáÐí åÏÇäÇ áåÐÇ æ ãÇ ßäÇ áäåÊÏí áæáÇ Çä åÏÇäÇ Çááå");
//
// menuStrip1
//
this.menuStrip1.set_GripStyle(System.Windows.Forms.ToolStripGripStyle.Visible);
this.menuStrip1.get_Items().AddRange(new System.Windows.Forms.ToolStripItem[] {
this.encrypteToolStripMenuItem,
this.decrypteToolStripMenuItem,
this.backColorToolStripMenuItem}
);
this.menuStrip1.set_Location(new System.Drawing.Point(0, 0));
this.menuStrip1.set_Name("menuStrip1");
this.menuStrip1.set_Size(new System.Drawing.Size(275, 24));
this.menuStrip1.set_TabIndex(34);
this.menuStrip1.set_Text("menuStrip1");
//
// encrypteToolStripMenuItem
//
this.encrypteToolStripMenuItem.set_BackColor(System.Drawing.SystemColors.get_Control());
this.encrypteToolStripMenuItem.set_Name("encrypteToolStripMenuItem");
this.encrypteToolStripMenuItem.set_Size(new System.Drawing.Size(62, 20));
this.encrypteToolStripMenuItem.set_Text("Encrypte");
this.encrypteToolStripMenuItem.add_Click(new System.EventHandler(this.encrypteToolStripMenuItem_Click));
//
// decrypteToolStripMenuItem
//
this.decrypteToolStripMenuItem.set_Name("decrypteToolStripMenuItem");
this.decrypteToolStripMenuItem.set_Size(new System.Drawing.Size(81, 20));
this.decrypteToolStripMenuItem.set_Text(" Decrypte");
this.decrypteToolStripMenuItem.add_Click(new System.EventHandler(this.decrypteToolStripMenuItem_Click));
//
// backColorToolStripMenuItem
//
this.backColorToolStripMenuItem.set_Name("backColorToolStripMenuItem");
this.backColorToolStripMenuItem.set_Size(new System.Drawing.Size(90, 20));
this.backColorToolStripMenuItem.set_Text(" Back Color");
this.backColorToolStripMenuItem.add_Click(new System.EventHandler(this.backColorToolStripMenuItem_Click));
//
// Form1
//
this.set_AutoScaleDimensions(new System.Drawing.SizeF(6F, 13F));
this.set_AutoScaleMode(System.Windows.Forms.AutoScaleMode.Font);
this.set_ClientSize(new System.Drawing.Size(275, 306));
this.get_Controls().Add(this.label4);
this.get_Controls().Add(this.label3);
this.get_Controls().Add(this.label2);
this.get_Controls().Add(this.label1);
this.get_Controls().Add(this.textBox3);
this.get_Controls().Add(this.textBox2);
this.get_Controls().Add(this.textBox1);
this.get_Controls().Add(this.menuStrip1);
this.set_ForeColor(System.Drawing.Color.get_Blue());
this.set_FormBorderStyle(System.Windows.Forms.FormBorderStyle.FixedSingle);
this.set_MainMenuStrip(this.menuStrip1);
this.set_MaximizeBox(false);
this.set_Name("Form1");
this.set_StartPosition(System.Windows.Forms.FormStartPosition.CenterScreen);
this.set_Text("Data Encryption Standard");
this.add_FormClosing(new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing));
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();

}
#endregion

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("Thanks for using my program ","Goooooood bye");
Application.ExitThread();
}
if (exit == DialogResult.No)
{
e.set_Cancel(true);
}
}

private void timer1_Tick(Object sender, System.EventArgs e)
{
int u = System.DateTime.get_Now().get_Millisecond();
x1 += 5;
Point p1 = new Point(x1, 278);
if (x1 >= 257)
{
x1 = -266;
p1.set_X(x1);
label4.set_Location(p1);

}
else
{
label4.set_Location(p1);
}
}

private void encrypteToolStripMenuItem_Click(Object sender, System.EventArgs e)
{

char single_character;
int ASCCode;
String character;
String key_text = textBox1.get_Text();
String plaintext = textBox2.get_Text();
String plain_text = textBox2.get_Text();
textBox1.set_Text("");
textBox2.set_Text("");
textBox3.set_Text("");
key key1 = new key();

int count = 0;
////////////////////////////////////////////////////////////
if (plain_text.length() % 8 == 0)
{
count = plain_text.length() / 8;
}
else
{
while (plain_text.length() % 8 != 0)
{
plain_text += '0';
}
count = (plain_text.length() / 8);
}
////////////////////////////////////////////////////////////////////
if (key_text.length() != 8)
{
MessageBox.Show("Enter Another key .the key must equal to 8 character ", "Error");
textBox1.set_Text("");
}
else
{
if (textBox2.get_Text() == "")
{
MessageBox.Show("u must enter the plain text ", "Error");
textBox1.set_Text("");
}
for (int i = 0; i < key_text.length(); i++)
{
character = key_text.substring(i, i + 1);
single_character = character.toCharArray()[0];
ASCCode = (int)single_character;
key1.get_binary(ASCCode);
key1.set_origin();
textBox1.set_Text(key1.get_origin());
}
////////////////////////////////////////////////////////////
for (int i = 0; i < count; i++)
{
for (int j = 0; j < 8; j++)
{
character = plain_text.substring(step, step + 1);
single_character = character.toCharArray()[0];
if (single_character == '0')
{
ASCCode = 0;
}
else
{
ASCCode = (int)single_character;
}
key1.get_binary(ASCCode);
key1.set_text();
textBox2.set_Text(plain_text);
step++;
}
plaintext = key1.set_orgin_text();
key1.text(plaintext.substring(0, 64));
key1.encrypte();
textBox3.set_Text(textBox3.get_Text() + key1.cipher());
}
}
step = 0;
}

private void decrypteToolStripMenuItem_Click(Object sender, System.EventArgs e)
{
char single_character;
int ASCCode;
String character;
String key_text = textBox1.get_Text();
String ciphertext = textBox3.get_Text();
String cipher_text = textBox3.get_Text();
textBox1.set_Text("");
textBox2.set_Text("");
textBox3.set_Text("");
key key1 = new key();
int count = 0;
////////////////////////////////////////////////////////////
if (cipher_text.length() % 8 == 0)
{
count = cipher_text.length() / 8;
}
else
{
while (cipher_text.length() % 8 != 0)
{
cipher_text += '0';
}
count = (cipher_text.length() / 8);
}
////////////////////////////////////////////////////////////////////
if (key_text.length() != 8)
{
MessageBox.Show("Enter Another key .the key must equal to 8 character ", "Error");
textBox1.set_Text("");
}
else
{
if (textBox3.get_Text() == "")
{
MessageBox.Show("u must enter the cipher text ", "Error");
textBox1.set_Text("");
}
for (int i = 0; i < key_text.length(); i++)
{
character = key_text.substring(i, i + 1);
single_character = character.toCharArray()[0];
ASCCode = (int)single_character;
key1.get_binary(ASCCode);
key1.set_origin();
textBox1.set_Text(key1.get_origin());
}
////////////////////////////////////////////////////////////
for (int i = 0; i < count; i++)
{
for (int j = 0; j < 8; j++)
{
character = cipher_text.substring(step, step + 1);
single_character = character.toCharArray()[0];
if (single_character == '0')
{
ASCCode = 0;
}
else
{
ASCCode = (int)single_character;
}
key1.get_binary(ASCCode);
key1.set_text();
textBox3.set_Text(cipher_text);
step++;
}
ciphertext = key1.set_orgin_text();
key1.text(ciphertext.substring(0, 64));
key1.decrypte();
textBox2.set_Text(textBox2.get_Text() + key1.cipher());
}
}
step = 0;
}

private void backColorToolStripMenuItem_Click(Object sender, System.EventArgs e)
{
colorDialog1.ShowDialog();
this.set_BackColor(colorDialog1.get_Color());
//menuStrip1.set_BackColor(colorDialog1.get_Color());
}
}

csharp code
package DES;
import System.Collections.ArrayList;
/**
* Summary description for key.
*/
public class key
{
private String[] key_round = new String[16];
public int count_plain;
public String plain_text;
private static int position;
private static int position_text;
private static int index;
private static int index_inverse;
private int[] orgin_key = new int[64];
private int[] test = new int[48];
private int[] sbox_arr1 = new int[6];
private int[] sbox_arr2 = new int[6];
private int[] sbox_arr3 = new int[6];
private int[] sbox_arr4 = new int[6];
private int[] sbox_arr5 = new int[6];
private int[] sbox_arr6 = new int[6];
private int[] sbox_arr7 = new int[6];
private int[] sbox_arr8 = new int[6];

private int[] permutedchioce1 ={
57, 49, 41, 33, 25, 17, 9,
1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27,
19, 11, 3, 60, 52, 44, 36,
63, 55, 47, 39, 31, 23, 15,
7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29,
21, 13, 5, 28, 20, 12, 4};

private int[] permutedchioce2 ={
14, 17, 11, 24, 1, 5, 3, 28,
15, 6, 21, 10, 23, 19, 12, 4,
26, 8, 16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55, 30, 40,
51, 45, 33, 48, 44, 49, 39, 56,
34, 53, 46, 42, 50, 36, 29, 32};

private int[] binary = new int[8];
private int[] binary_values = new int[4];
private int[] key_after_first_permution = new int[56];
private int[] key_after_second_permution = new int[48];
private int[] c = new int[28];
private int[] d = new int[28];
private int[] c_next = new int[28];
private int[] d_next = new int[28];
private int[] text = new int[64];
private int[] origin_text = new int[64];
private int[] after_sboxes = new int[32];
private int[] after_permutation_function = new int[32];


private int[] initailpermution ={
58,50,42,34,26,18,10,2,
60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6,
64,56,48,40,32,24,16,8,
57,49,41,33,25,17,9,1,
59,51,43,35,27,19,11,3,
61,53,45,37,29,21,13,5,
63,55,47,39,31,23,15,7};

private int[] expansion_permution ={
32,1,2,3,4,5,
4,5,6,7,8,9,
8,9,10,11,12,13,
12,13,14,15,16,17,
16,17,18,19,20,21,
20,21,22,23,24,25,
24,25,26,27,28,29,
28,29,30,31,32,1};
private int[] permutation_function ={
16,7,20,21,29,12,28,17,
1,15,23,26,5,18,31,10,
2,8,24,14,32,27,3,9,
19,13,30,6,22,11,4,25};

private int[] text_after_initailpermution = new int[64];
private int[] right_after_expansion_permution = new int[48];
private int[] left = new int[32];
private int[] right = new int[32];
private int[] next_right = new int[32];
private int[] next_left = new int[32];

private int[][] s1 = new int[][]{
{14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7},
{0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8},
{4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0},
{15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13}};



private int[][] s2 = new int[][]{
{15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10},
{3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5},
{0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15},
{13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9}};



private int[][] s3 = new int[][]{
{10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8},
{13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1},
{13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7},
{1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12}};


private int[][] s4 = new int[][]{
{7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15},
{13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9},
{10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4},
{3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14}};


private int[][] s5 = new int[][]{
{2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9},
{14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6},
{4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14},
{11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3}};


private int[][] s6 = new int[][]{
{12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11},
{10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8},
{9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6},
{4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13}};


private int[][] s7 = new int[][]{
{4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1},
{13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6},
{1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2},
{6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12}};


private int[][] s8 = new int[][]{
{13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7},
{1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2},
{7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8},
{2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11}};

private int[] after_bit_swap = new int[64];

private int[] inverse_initailpermution ={
40,8,48,16,56,24,64,32,
39,7,47,15,55,23,63,31,
38,6,46,14,54,22,62,30,
37,5,45,13,53,21,61,29,
36,4,44,12,52,20,60,28,
35,3,43,11,51,19,59,27,
34,2,42,10,50,18,58,26,
33,1,41,9,49,17,57,25};
private int[] after_inverse = new int[64];
/////////////////////////////////////////////////////////
public key()
{
position_text = 0;
position = 0;
index = 0;
index_inverse = 0;
}
/////////////////////////////////////////////////////////
public String get_origin()
{
String s = "";
for (int i = 0; i < orgin_key.length; i++)
{
s += orgin_key[i];
}
return s;
}
/////////////////////////////////////////////////////////
//////////// permutation one on the key
void set_key_after_permution_one()
{
for (int i = 0; i < 56; i++)
{
key_after_first_permution[i] = orgin_key[permutedchioce1[i] - 1];
}
}
///////////////////////////////////////////////////////////////
//////////////// divide the key to 28 character C and D
void decompose_key()
{
for (int j = 0; j < 28; j++)
{
c[j] = key_after_first_permution[j];
d[j] = key_after_first_permution[j + 28];
}
}
//////////////////////////////////////////
///////////////// do lift shift to the left"C" and right"D" part of the key
public void left_shift(int round_num)
{

int[] tempC = new int[28];
int[] tempD = new int[28];
if (round_num == 1 || round_num == 2 || round_num == 9 || round_num == 16)
{
for (int i = 0; i < 28; i++)
{
if (i == 27)
{
tempC[i] = c[0];
tempD[i] = d[0];
}
else
{
tempC[i] = c[i + 1];
tempD[i] = d[i + 1];
}
}
for (int j = 0; j < 28; j++)
{
c[j] = tempC[j];
d[j] = tempD[j];
c_next[j] = c[j];
d_next[j] = d[j];
}
}
else
{
for (int i = 0; i < 28; i++)
{
if (i == 27)
{
tempC[i] = c[1];
tempD[i] = d[1];
}
else
{
if (i == 26)
{
tempC[i] = c[0];
tempD[i] = d[0];
}
else
{
tempC[i] = c[i + 2];
tempD[i] = d[i + 2];
}
}
}
for (int j = 0; j < 28; j++)
{
c[j] = tempC[j];
d[j] = tempD[j];

c_next[j] = c[j];
d_next[j] = d[j];
}
}
}
/////////////////////////////////////////////////////////////
void key_after_leftshift()
{
set_C_D_key();
for (int j = 0; j < 28; j++)
{
key_after_first_permution[j] = c[j];
key_after_first_permution[j + 28] = d[j];
}
}
/////////////////////////////////////////////////////////////
//////////// permutation two on the key
void set_key_after_permution_two()
{
key_after_leftshift();
//set_C_D_key();
for (int i = 0; i < 48; i++)
{
key_after_second_permution[i] = key_after_first_permution[permutedchioce2[i] - 1];
}
}
///////////////////////////////////////////////////////////////
//////////// set the two part of the key of the next round
public void set_C_D_key()
{
for (int i = 0; i < 28; i++)
{
c[i] = c_next[i];
d[i] = d_next[i];
// key_after_first_permution[i] = c[i];
// key_after_first_permution[i + 28] = d[i];
}

}
///////////////////////////////////////////////////////////////
void get_binary(int value)
{
//if (value == 0)
//{
for (int j = 0; j < 8; j++)
{
binary[j] = 0;
}
//}
//else
//{
int i = 0;
while (value != 0)
{
if (value % 2 == 0)
{
binary[i] = 0;
i++;
value /= 2;
}
else
{
binary[i] = 1;
i++;
value = (value - 1) / 2;
}
}
//}
}
/////////////////////////////////////////////////////////////
////////////////// convert the decimal values to binary
void get_binary(int value, int[] a)
{
for (int j = 0; j < a.length; j++)
{
a[j] = 0;
}
int i = 0;
while (value != 0)
{

if (value % 2 == 0)
{
a[i] = 0;
i++;
value /= 2;
}
else
{
a[i] = 1;
i++;
value = (value - 1) / 2;
}
}
}
/////////////////////////////////////////////////////////////
/////////////// set the binary key to "orgin_key"
public void set_origin()
{
for (int j = 0; j < 8; j++)
{
orgin_key[position] = binary[j];
position++;
}
}
//////////////////////////////////////////////////////////////
/////////////// set the binary text to "origin_text"
public void set_text()
{
for (int i = 0; i < 8; i++)
{
origin_text[position_text] = binary[i];
position_text++;
}
}
//////////////////////////////////////////////////////////////
//////////// return the representation of the text in binary
public String set_orgin_text()
{
String s = "";
for (int i = 0; i < origin_text.length; i++)
{
s += origin_text[i];
}
return s;
}

//////////////////////////////////////////////////////////////
//////////// set the representation of the text in binary to "origin_text"
public void text(String s)
{
for (int i = 0; i < 64; i++)
{
origin_text[i] = Integer.parseInt(s.substring(i, i + 1));
}
text_plain();
}
//////////////////////////////////////////////////////////////////
//////////// put the representation of the text in "text"
public void text_plain()
{
for (int i = 0; i < 64; i++)
{
text[i] = origin_text[i];
}
}
/////////////////////////////////////////////////////////////////////
/////////// set plain text with initial permuataion in "text_after_initailpermution"
public void set_text_with_initailpermution()
{
for (int i = 0; i < 64; i++)
{
text_after_initailpermution[i] = text[(initailpermution[i] - 1)];
}
}
/////////////////////////////////////////////////////////////////////////
//////////// divide the plain text to two part left and right
public void Set_left_right_halves_txt()
{
for (int i = 0; i < 32; i++)
{
left[i] = text_after_initailpermution[i];
right[i] = text_after_initailpermution[i + 32];
next_left[i] = right[i];
}
}
///////////////////////////////////////////////////
///////////////// set right with expansion permutation in "right_after_expansion_permution"
public void se_right_with_ep()
{
for (int i = 0; i < 48; i++)
{
right_after_expansion_permution[i] = right[expansion_permution[i] - 1];
}
}
////////////////////////////////////////////////////////////////
///////////////// Xor function take two value to xor this values
public int xor(int i, int j)
{
if ((i == j))
return 0;
else
return 1;

}
/////////////////////////////////////////////////////////////////////
////// do xor with "right_after_expansion_permution" and "key_after_second_permution"
public void right_of_txt_xor_key(String s)
{
int[] arr = new int[48];
for (int i = 0; i < 48; i++)
{
arr[i] = Integer.parseInt(s.substring(i, i + 1));
}
for (int i = 0; i < 48; i++)
{
test[i] = xor(right_after_expansion_permution[i], arr[i]);
}
cut_six_element();
}
//////////////////////////////////////////////
///////// convert the binary values to decimal value
public int convert_to_decimal(int[] a)
{
int decimal = 0;
for (int i = 0; i < a.length; i++)
{
decimal += a[i] * Math.pow(2, i);
}
return decimal;
}

/////////////////////////////////////////////
/////// get six element to serach with it in the sboxes
public void cut_six_element()
{
int j = 0;
for (int i = 0; i < 6; i++)
{
sbox_arr1[j] = test[i];
j++;
}
/// if the program crach i will put j=0 here
j = 0;
for (int i = 6; i < 12; i++)
{
sbox_arr2[j] = test[i];
j++;
}
j = 0;
for (int i = 12; i < 18; i++)
{
sbox_arr3[j] = test[i];
j++;
}
j = 0;
for (int i = 18; i < 24; i++)
{
sbox_arr4[j] = test[i];
j++;
}
j = 0;
for (int i = 24; i < 30; i++)
{
sbox_arr5[j] = test[i];
j++;
}
j = 0;
for (int i = 30; i < 36; i++)
{
sbox_arr6[j] = test[i];
j++;
}
j = 0;
for (int i = 36; i < 42; i++)
{
sbox_arr7[j] = test[i];
j++;
}
j = 0;
for (int i = 42; i < 48; i++)
{
sbox_arr8[j] = test[i];
j++;
}

}
//////////////////////////////////////////////
//////// get values from "sbox_arr_num" and "sbox_num"
///s_arr like sbox_arr8 and s like s8
public void get_value_from_sboxes(int sbox_arr_num[], int sbox_num[][])
{
int column_var, row_var, value;
int[] row = { sbox_arr_num[0], sbox_arr_num[5] };
int[] column = { sbox_arr_num[1], sbox_arr_num[2], sbox_arr_num[3], sbox_arr_num[4] };
row_var = convert_to_decimal(row);
column_var = convert_to_decimal(column);
value = sbox_num[row_var][column_var];
get_binary(value, binary_values);
set_arr_after_sboxes();
}
//////////////////////////////////////////////
/////////////// set array after sboxes
public void set_arr_after_sboxes()
{
for (int i = 0; i < 4; i++)
{
after_sboxes[index] = binary_values[i];
index++;
}
}
///////////////////////////////////////////////
///////// calculate sboxes
public void sboxes()
{
get_value_from_sboxes(sbox_arr1, s1);
get_value_from_sboxes(sbox_arr2, s2);
get_value_from_sboxes(sbox_arr3, s3);
get_value_from_sboxes(sbox_arr4, s4);
get_value_from_sboxes(sbox_arr5, s5);
get_value_from_sboxes(sbox_arr6, s6);
get_value_from_sboxes(sbox_arr7, s7);
get_value_from_sboxes(sbox_arr8, s8);
}
///////////////////////////////////////////////
//////// do permutation function
void permutation_Function()
{
for (int i = 0; i < 32; i++)
{
after_permutation_function[i] = after_sboxes[permutation_function[i] - 1];
}
}
//////////////////////////////////////////////
/////// xor left part of the text with the "after_permutation_function"
public void left_of_txt_xor_after_permutation_function()
{
for (int i = 0; i < 32; i++)
{
next_right[i] = xor(after_permutation_function[i], left[i]);
}
}
///////////////////////////////////////////
//// set next left part of text and next right part of text
public void set_left_right_text()
{
for (int i = 0; i < 32; i++)
{
left[i] = next_left[i];
right[i] = next_right[i];
}
}
///////////////////////////////////////////////////////
//////// set next plain text
public void set_next_plain_text()
{
for (int i = 0; i < 32; i++)
{
text_after_initailpermution[i] = left[i];
text_after_initailpermution[i + 32] = right[i];
}
}
/////////////////////////////////////////////////////
//////// set 32 bit swap to the text
public void bit_swap()
{
for (int i = 0; i < 32; i++)
{
after_bit_swap[i] = text_after_initailpermution[i + 32];
after_bit_swap[i + 32] = text_after_initailpermution[i];
}
}
/////////////////////////////////////*//////////////////
//////// set inverse initial permutation
public void set_text_inverse_initailpermution()
{
for (int i = 0; i < 64; i++)
{
after_inverse[i] = after_bit_swap[(inverse_initailpermution[i] - 1)];
}
}
///////////////////////////////////////////////////////
//// get the cipher text
public String cipher()
{
int[] arr_cipher_letter = new int[8];
String s = "";
for (int j = 0; j < 8; j++)
{
for (int i = 0; i < 8; i++)
{
arr_cipher_letter[i] = after_inverse[index_inverse];
index_inverse++;
}
s += (char)convert_to_decimal(arr_cipher_letter);
}
return s;
}
//////////////////////////////////////////////////////////////////
public void function_key()
{
String s = "";
set_key_after_permution_one();
for (int i = 0; i < 16; i++)
{
decompose_key();
left_shift(i + 1);
set_C_D_key();
set_key_after_permution_two();
for (int j = 0; j < 48; j++)
{
s += key_after_second_permution[j];
}
key_round[i] = s;
s = "";
}
s = "";
}
///////////////////////////////////////////////////////
public void encrypte()
{
function_key();
set_text_with_initailpermution();
for (int i = 1; i <= 16; i++)
{
Set_left_right_halves_txt();
se_right_with_ep();
right_of_txt_xor_key(key_round[i - 1]);
sboxes();
permutation_Function();
left_of_txt_xor_after_permutation_function();
set_left_right_text();
set_next_plain_text();
index = 0;
}
bit_swap();
set_text_inverse_initailpermution();
position_text = 0;
position = 0;
index = 0;
index_inverse = 0;
//plain_text = "karim";
}
//////////////////////////////////////////////////////////////
public void decrypte()
{
function_key();
set_text_with_initailpermution();
for (int i = 1; i <= 16; i++)
{
Set_left_right_halves_txt();
se_right_with_ep();
right_of_txt_xor_key(key_round[16 - i]);
sboxes();
permutation_Function();
left_of_txt_xor_after_permutation_function();
set_left_right_text();
set_next_plain_text();
index = 0;
}
bit_swap();
set_text_inverse_initailpermution();
position_text = 0;
position = 0;
index = 0;
index_inverse = 0;
}
}





Attachments:
DES J#.rar [519.97 KiB]
Downloaded 1582 times

_________________
Please recommend my post if you found it helpful
Author:
Newbie
User avatar Posts: 9
Have thanks: 0 time

hello karim nice work but why when i tried to encryot an text more than 8 char it is encrypted but when i try to decrypt the cipher it didn't work?


Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time

updated.

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 3 posts ] 

  Related Posts  to : DES J# Data Encryption Standard
 Encryption Algorithm{Data Encryption Standard}     -  
 Data Encryption Standard (DES)     -  
 Data Encryption Standard(DES) using C Language     -  
 Advanced Encryption Standard (AES)     -  
 Advanced Encryption Standard (AES)-Example-Decipher (Step2)     -  
 Advanced Encryption Standard (AES)-Java Project     -  
 Advanced Encryption Standard (AES)-Example-Cipher (Step1)     -  
 Des java Applet ( Data Encryption Standards )     -  
 Encryption and Decryption encryption Affine cipher code     -  
 Standard Huffman Coding     -  



Topic Tags

C# Algorithms
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com