Total members 11889 |It is currently Fri Mar 29, 2024 1:04 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka



Go to page 1, 2  Next



* Project Name:   Data Encryption Standard
* Programmer:   msi_333
* Type:   Encryption Algorithms
* Technology:  Java
* IDE:   NetBeans
Attachment:
File comment: DES
DES.gif
DES.gif [ 22.54 KiB | Viewed 51132 times ]


* Description:   Data Encryption Standard implementation without using Java security package
Data Encryption Standard (DES) is a widely-used method of data encryption using a private (secret) key that was judged so difficult to break by the U.S. government that it was restricted for exportation to other countries. There are 72,000,000,000,000,000 (72 quadrillion) or more possible encryption keys that can be used. For each given message, the key is chosen at random from among this enormous number of keys. Like other private key cryptographic methods, both the sender and the receiver must know and use the same private key.

It was developed in the 1970s by the National Bureau of Standards with the help of the National Security Agency. Its purpose is to provide a standard method for protecting sensitive commercial and unclassified data. IBM created the first draft of the algorithm, calling it LUCIFER. DES officially became a federal standard in November of 1976.

To understand this implementation, you'll first need to understand bit operations and read some more general explanations of DES.

In general, DES takes as input a 64 bit key, of which only 56 bits are used. From these 56 bits, 16 48 bit subkeys are created. The message is split into 64 bit chunks, and a complex series of steps enciphers the message using each subkey. As Javascript only supports 32 bit integers, all 48 or 64 bit integers are represented by two 32 bit integers in an array.
Creating the Keys - PC1
DES must first create the 16 subkeys. First, the 8 character string representing the key is turned into two integers which are passed to the des_createKeys function. The bits of these two integers are then reorganized according to PC1 (permuted choice 1). The bits end up in the following places:

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 0 0 0 0
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 0 0 0 0

This means that the first 57th bit of the original key becomes the first bit, etc. The permuting is done using a permutation sequence, which is a clever way of rotating and switching bits between 2 integers. For instance, temp = ((left >>> 4) ^ right) & 0x0f0f0f0f; right ^= temp; left ^= (temp << 4); rotates 4x4 blocks of bits:

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

I found this permutation function in Eric Young's C implementation. He gives credit to John Fletcher for determining the most efficient way to do PC1.

Creating the Keys - Left Shifts DES Explanation
Data Encryption Standard (DES) is a widely-used method of data encryption using a private (secret) key that was judged so difficult to break by the U.S. government that it was restricted for exportation to other countries. There are 72,000,000,000,000,000 (72 quadrillion) or more possible encryption keys that can be used. For each given message, the key is chosen at random from among this enormous number of keys. Like other private key cryptographic methods, both the sender and the receiver must know and use the same private key.

It was developed in the 1970s by the National Bureau of Standards with the help of the National Security Agency. Its purpose is to provide a standard method for protecting sensitive commercial and unclassified data. IBM created the first draft of the algorithm, calling it LUCIFER. DES officially became a federal standard in November of 1976.

To understand this implementation, you'll first need to understand bit operations and read some more general explanations of DES.

In general, DES takes as input a 64 bit key, of which only 56 bits are used. From these 56 bits, 16 48 bit sub-keys are created. The message is split into 64 bit chunks, and a complex series of steps enciphers the message using each sub-key. As JavaScript only supports 32 bit integers, all 48 or 64 bit integers are represented by two 32 bit integers in an array.
Creating the Keys - PC1
DES must first create the 16 sub keys. First, the 8 character string representing the key is turned into two integers which are passed to the des_createKeys function. The bits of these two integers are then reorganized according to PC1 (permuted choice 1). The bits end up in the following places:

Code:
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 0 0 0 0
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 0 0 0 0


This means that the first 57th bit of the original key becomes the first bit, etc. The permuting is done using a permutation sequence, which is a clever way of rotating and switching bits between 2 integers. For instance, temp = ((left >>> 4) ^ right) & 0x0f0f0f0f; right ^= temp; left ^= (temp << 4); rotates 4x4 blocks of bits:
Code:
33 34 35 36 1 2 3 4
41 42 43 44 9 10 11 12
49 50 51 52 17 18 19 20
57 58 59 60 25 26 27 28
37 38 39 40 5 6 7 8
45 46 47 48 13 14 15 16
53 54 55 56 21 22 23 24
61 62 63 64 29 30 31 32


I found this permutation function in Eric Young's C implementation. He gives credit to John Fletcher for determining the most efficient way to do PC1.

Creating the Keys - Left Shifts DES Explanation

Data Encryption Standard (DES) is a widely-used method of data encryption using a private (secret) key that was judged so difficult to break by the U.S. government that it was restricted for exportation to other countries. There are 72,000,000,000,000,000 (72 quadrillion) or more possible encryption keys that can be used. For each given message, the key is chosen at random from among this enormous number of keys. Like other private key cryptographic methods, both the sender and the receiver must know and use the same private key.

It was developed in the 1970s by the National Bureau of Standards with the help of the National Security Agency. Its purpose is to provide a standard method for protecting sensitive commercial and unclassified data. IBM created the first draft of the algorithm, calling it LUCIFER. DES officially became a federal standard in November of 1976.

To understand this implementation, you'll first need to understand bit operations and read some more general explanations of DES.

In general, DES takes as input a 64 bit key, of which only 56 bits are used. From these 56 bits, 16 48 bit sub-keys are created. The message is split into 64 bit chunks, and a complex series of steps enciphers the message using each subkey. As Javascript only supports 32 bit integers, all 48 or 64 bit integers are represented by two 32 bit integers in an array.
Creating the Keys - PC1
DES must first create the 16 sub-keys. First, the 8 character string representing the key is turned into two integers which are passed to the des_createKeys function. The bits of these two integers are then reorganized according to PC1 (permuted choice 1). The bits end up in the following places:

Code:
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 0 0 0 0
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 0 0 0 0


This means that the first 57th bit of the original key becomes the first bit, etc. The permuting is done using a permutation sequence, which is a clever way of rotating and switching bits between 2 integers. For instance, temp = ((left >>> 4) ^ right) & 0x0f0f0f0f; right ^= temp; left ^= (temp << 4); rotates 4x4 blocks of bits:

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


I found this permutation function in Eric Young's C implementation. He gives credit to John Fletcher for determining the most efficient way to do PC1.

Creating the Keys - Left Shifts


In other DES explanations, the first 28 bits out of PC1 is called C, and the last 28 bits is D. (Note that PC1 only uses 56 of the original 64 bits.) These are then left shifted according to a schedule until 16 subkeys are created. For instance, C1 and D1 are created by left shifting C and D by one place, C2 and D2 by a further one place, etc. Here is what C1 and D1 look like relative to C and D:

Code:
2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17
18 19 20 21 22 23 24 25
26 27 28 1 0 0 0 0
34 35 36 37 38 39 40 41
42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57
58 59 60 33 0 0 0 0


Creating the Keys - PC2
The 16 sub-keys (C1 and D1 through to C16 and D16) are then permuted according to PC2 to create 16 sub-keys of 48 bits each. PC2 seems to be a random bit sequence and so the permutation sequence above can't be used. Instead the bits are permuted 4 at a time using the pc2bytes array of arrays. pc2bytes contains 14 sub-arrays (one for each 4 bits of the 56 bit sub-keys). Each sub-array contains 16 elements. Every four bits of a subkey are looked up in the corresponding array and the results are all added together. For example, if the first four bits of C1 are 0100 in binary, then the fourth element of pc2bytes[0], 0x00010000 is added to the result. This has the result of moving the 2nd bit of C1 to the 16th bit of the result (some bit shifting is also applied at the end to put everything in the right place). Here is what the resulting K1 though K16 look like relative to C1 and D1 through C16 and D16. Note that this is different from the PC2 given in other DES explanations, the reason for this is explained further down.

Code:
0 0 3 28 15 6 21 10
0 0 16 7 27 20 13 2
0 0 34 44 55 49 37 52
0 0 50 46 54 40 33 36
0 0 14 17 11 24 1 5
0 0 23 19 12 4 26 8
0 0 45 56 35 41 51 59
0 0 48 53 43 60 38 57


Encrypting the Message - IP1
With 16 subkeys created, we are now ready to deal with the message. First the whole message is turned into and array of 32 bit integers, then the message is encrypted or decrypted 64 bits at a time. The first step is to perform the permutation IP. This again uses the permutation sequence. Eric Young gives credit to Richard Outer-bridge for helping make this more efficient.

Code:
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


Encrypting the Message - E
The resulting 64 bits are then encrypted or decrypted using the 16 subkeys. This requires 16 steps, during which the left and right half of the 64 bit message are alternatively operated on. The operation involves taking 32 bits (called R in other explanations), permuting it according to E (which expands are from 32 to 48 bits), XORing with one of the 48 bit subkeys to produce a different 48 bits, then passing it through the S selection functions to get it back down to 32 bits, and finally permuting through P. Note that the E used here (displayed below) is different from other Es, for the reason explained below.
Code:
0 0 3 4 5 6 7 8
0 0 11 12 13 14 15 16
0 0 19 20 21 22 23 24
0 0 27 28 29 30 31 32
0 0 31 32 1 2 3 4
0 0 7 8 9 10 11 12
0 0 15 16 17 18 19 20
0 0 23 24 25 26 27 28

Encrypting the Message - S Selection Functions

The step above expands 32 bits into 48 bits, and then Xor them with a subkey. The result is passed through the S selection functions. There are 8 of these functions (labelled S1 through S8 would you believe) which convert 6 bits into 4 bits. (Their randomness is part of the reason why DES is so powerful, because a 1 bit uncertainty in the input yields 4 bits of uncertainty in the output.) The first 6 bits of the output from the E permutation above are passed through S2, the next 6 through S4, then S6, S8, S1, S3, S5 and S7. Using this ordering makes it easier to perform E (as it is essentially done by duplicating the 32 bits), and PC2 was also changed to compensate for this. As with PC2, a series of 8 spfunction arrays of 64 elements are used to perform the S functions. I can't show the output of the S functions because it is not about bit movements. The 6 input bits to each S function could produce any one of 16 results. Note that a further efficiency is applied by shifting the input one bit to the left before encrypting, instead of during each encryption step (and the spfunction are also shifted one left to compensate).

Encrypting the Message - P

The 32 bit output from the S functions is permuted again according to P. For efficiency, this permutation is done as part of the S functions array. For example, the first 6 bits out of E are looked up in the second S array, spfunction2. If these 6 bits are all zero, then 0x40084010 is added to the result. If you refer to other DES descriptions, they will tell you exactly what the S functions are, and you would see that the zeroth element of S2 is 15. This means that if you pass binary 000000 into S2, you get 1111 as output. These four bits are then moved around according to P (remembering to shift them left once as well) and the result is 0x80108020. Specifically, the bits are moved to positions 12, 27, 1, and 17.

Encrypting the Message - IP-1
After going through the above steps 16 times, the 64 bits are passed through IP-1 to produce the cipher text. Decryption is performed using exactly the same process but the subkeys are applied in the reverse order.

Code:
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


DES Modes

There are several different modes in which DES can be run. ECB (Electronic Codebook) mode is the simplest mode. In this mode, each 64 bit chunk of the input message is treated independently, thus it is also the least secure. In CBC (Cipher Block Chaining) mode, you supply a 64 bit input vector. The first 64 bits of the message are Xor ed with this before encrypting. The second 64 bits are then XORed with the cipher text from the first 64 bit before encrypting it, and so on.

In triple DES, every eight bytes are operated on three times (for instance, a triple DES encryption will encrypt, decrypt, and encrypt the data) before being appended to the result. CBC mode uses the result of the first 8 byte triple DES encryption to feed back into the algorithm.

Algorithm Speed


The first JavaScript DES implementation worked fine for small data sets, but slowed down incredibly on larger blocks. By making a few changes, the speed has been increased dramatically. These changes were: storing the message and result as strings rather than arrays (which take a comparatively long time to look things up); creating the result string in blocks rather than one character at a time (periodically adding 512 characters to a 32000 character string is much faster than adding one character at a time); and bringing all global variables inside the functions (which also meant undoing the previous permute function). Below is a table showing the speed increase on a Pentium 200 with Windows 98 and Internet Explorer 5. I also tested it in the Galleon browser under Redhat Linux 7.2 on the same machine (sadly it was quite a bit slower).


Here all the classes :
Attachment:
File comment: files
structure.gif
structure.gif [ 2.39 KiB | Viewed 35875 times ]

Code:
encrypt.java


java code
import java.util.Scanner;

public class encrypt {

/** Creates a new instance of encrypt */
public encrypt( String Plaintext, String Key) {




PlainText=Plaintext;
keyword=Key;


}

public String CheckLenPlain(String plain){
if(plain.length()%8!=0) {
int len=8-plain.length()%8;

for(int i=0; i<len; i++)
plain=plain.concat("*");

} else {
return plain;
}
return plain;

}


public void DoEncrpyt_Plaintext(String plain) {

for(int i=0;i<8&&i<plain.length();i++) {
block[i]=getBinaryBits(plain.charAt(i));// 2D array [8][8]

}
int index=0;
for(int i=0; i<8; i++){
for(int j=0; j<8;j++){
perm[index]=(int)block[i][j];
blockS[i][j]=Integer.toString((int)block[i][j]);
index++;

}
}
arrayprinter.printarray(blockS,"PlainText Text in Binary");
DesPanel.StepsText.append("****************************************************************"+'\n');

}


public void DoEncrpyt_keyword() {



for(int i=0;i<8&&i<keyword.length();i++) {
block[i]=getBinaryBits(keyword.charAt(i));
for(int j=0;j<8;j++) {

blockS[i][j]=Integer.toString((int)block[i][j]);


}
// System.out.println(" ");
}


int index=0;
for(int i=0; i<8; i++){
for(int j=0; j<8;j++){
key_in[index]=(int)block[i][j];
index++;

}
}

arrayprinter.printarray(blockS,"KeyWord Text in Binary");
DesPanel.StepsText.append("****************************************************************"+'\n');

}

public byte[] getBinaryBits(int ch) {
byte[] bin=new byte[8];
int tag=1;
for(int i=0;i<8;i++) {
bin[7-i]=(byte)((ch&((tag<<i)))>>i);
}
return bin;
}

public void DoSegmentation(int[] perm_out){
int index=0;
for(int i=0; i<32; i++)
Left[i]=perm_out[i];

for(int i=32; i<64; i++){
Right[index]=perm_out[i];

index++;
}
index=0;
for(int i=0; i<4;i++){
for(int j=0; j<8; j++){
LeftS[i][j]=Integer.toString(Left[index]);
RightS[i][j]=Integer.toString(Right[index]);
index++;
}
}
arrayprinter.printarray(LeftS,"Left Part");
arrayprinter.printarray(RightS,"Right Part");

}
public void XOR(int side1[], int [] side2,int [] result){
int index=0;
for(int i=0; i<side1.length; i++){
if(side1[i]==side2[i])
result[index]=0;
else
result[index]=1;
index++;

}





}
public void FillC_D(){
int index=28;
for(int i=0; i<28; i++)
C_D[i]=C[i];

for(int i=0; i<28; i++){
C_D[index]=D[i];
index++;
}


}
public void contancate() {
int index=32;
for(int i=0; i<32; i++)
Block64[i]=Left[i];

for(int i=0; i<32; i++){
Block64[index]=Right[i];
index++;
}
}
public void swap32() {
int temp;
for(int i=0;i<32;i++) {
temp=Left[i];
Left[i]=Right[i];
Right[i]=temp;
}
ind=0;
for(int i=0; i<4;i++){
for(int j=0; j<8; j++){
LeftS[i][j]=Integer.toString(Left[ind]);
RightS[i][j]=Integer.toString(Right[ind]);
ind++;
}
}
arrayprinter.printarray(LeftS,"Left Part");
arrayprinter.printarray(RightS,"Right Part");
ind=0;
}

public static int[] getByteFromBits(int bits64[]) {
// int ch[]=new int[8];
int index=0;
System.out.println();
for(int i=0;i<8;i++)
for(int j=1;j<=8;j++) {


ch[i]+=(int)Math.pow(2,(8-j))*bits64[index];
index++;

}

return ch;
}

public void Chooser(int choice){
if(choice==1){
System.out.println("before One left shift " + index);
for(int j=0; j<C.length; j++)
System.out.print(C[j]);
key.Do_OneLeftShitf(C,D);
System.out.println();
System.out.println("After One left shift " + index);
for(int j=0; j<C.length; j++)
System.out.print(C[j]);
}

else{

for(int j=0; j<C.length; j++)
System.out.print(C[j]);

key.Do_OneLeftShitf(C,D);
key.Do_OneLeftShitf(C,D);
System.out.println(" LEFT= ");

for(int j=0; j<C.length; j++)
System.out.print(C[j]);

System.out.println(" Right= ");

for(int j=0; j<D.length; j++)
System.out.print(D[j]);

}

}

public void DoDecryption() {
DesPanel.StepsText.append("************************DECIPHER***********************************"+'\n');
int start=0;

int counter=0;
int end=64;
for(int f=0; f<PlainText.length()/8; f++){

int Round=1;
for(int h=start; h<end; h++ ){
newBlock64_[counter]=Integer.parseInt(finalEncry.substring(h,h+1));
counter++;
}
DesPanel.StepsText.append("*********Block Number *********"+(f+1)+'\n');
ind=0;
DesPanel.StepsText.append("*********Cipher Block In Round ********* "+ Round+'\n');
for(int d=0 ; d<8; d++){
for(int j=0; j<8; j++){
newBlock64_S[d][j]=Integer.toString(newBlock64[ind]);
ind++;
}
}
arrayprinter.printarray(newBlock64_S,"Cipher Block");



p= new Permutation();

p.FillPermutation();//step1 from 2D to 1D
p.DoIP(newBlock64_,perm_out);
DoSegmentation(perm_out);

int adder=48;

while(Round<=16) {
DesPanel.StepsText.append("*********Round Number********* "+Round+'\n');
for(int i=0; i<48; i++) {
reversedkey[i]=Integer.parseInt(key_reverse.substring((key_reverse.length()-adder)+i,(key_reverse.length()-adder)+i+1));

}
System.out.println();
ESTable Etable = new ESTable();
Etable.FillETable();//step3 array from 2D to 1D
Etable.DoETable(Right,Right_out);//step3
DesPanel.StepsText.append("*********Right Part XORED with Round Key********* "+Round+'\n');
XOR(Right_out,reversedkey,XOR_Out);//step1
ind=0;
for(int g=0; g<6; g++){
for(int j=0; j<8; j++){
XORS[g][j]=Integer.toString(XOR_Out[ind]);
ind++;

}
}
arrayprinter.printarray(XORS,"XOR Result");
ind=0;
SBox sbox= new SBox();

sbox.DoSBox(XOR_Out,after_SBox);//step2 32bits - include permitation
DesPanel.StepsText.append("*********Left Part XORED with Output Function in Round********* "+Round+'\n');
XOR(Left,after_SBox,aft_XOR_fuc);//XOR



for(int g=0; g<4; g++){
for(int j=0; j<8; j++){
aft_XOR_fucS[g][j]=Integer.toString(aft_XOR_fuc[ind]);
ind++;

}
}
ind=0;
arrayprinter.printarray(aft_XOR_fucS,"XOR Result");

adder=adder+48;
Round++;
DesPanel.StepsText.append("*********Left=Right & Right=Left*********"+'\n');
for(int i=0;i<32;i++) {
Left[i]=Right[i];
Right[i]=aft_XOR_fuc[i];
}


}

ind=0;
for(int g=0; g<4; g++){
for(int j=0; j<8; j++){
LeftS[g][j]=Integer.toString(Right[ind]);
RightS[g][j]=Integer.toString(aft_XOR_fuc[ind]);
ind++;

}
}
arrayprinter.printarray(LeftS,"Left Part");
arrayprinter.printarray(RightS,"Right Part");

DesPanel.StepsText.append("********After Swap Operation********* " +'\n');
swap32();




contancate();
System.out.println("/nBit Swap :");
for(int i=0;i<64;i++) {
System.out.print(Block64[i]);
}

p.FillInversePermutation();//step1 from 2D to 1D
p.DoInverseIP(Block64,newBlock64);//step1 -array

System.out.println("The decyption code: ");
for(int i=0; i<64; i++) {
if(binDec==null)
binDec=Integer.toString(newBlock64[i]);
else
binDec+=Integer.toString(newBlock64[i]);
System.out.print(newBlock64[i]);
}
System.out.println(" ");
ch=getByteFromBits(newBlock64);


for(int i=0; i<8; i++){
System.out.print((char)ch[i]);
if(finalDecry==null)
finalDecry=Character.toString((char)ch[i]);
else
finalDecry+=Character.toString((char)ch[i]);
}

for(int i=0; i<8; i++)
ch[i]=0;
start=end;
end=end+64;
counter=0;

}


}

public String getBinDec(){
return binDec;
}
public void DoEncryption(){
DesPanel.StepsText.append("************************CIPHER***********************************"+'\n');
DoEncrpyt_keyword();
int start=0;
int end=8;
PlainText=CheckLenPlain(PlainText);
// System.out.println("PlainTETX: "+ PlainText.substring(start,start+9));

String temp;
for(int f=0; f<PlainText.length()/8; f++){


temp=PlainText.substring(start,end);

DoEncrpyt_Plaintext(temp);


System.out.println("The Orginal Massage code: ");
for(int i=0;i<64;i++) {
if(binCi==null)
binCi=Integer.toString(perm[i]);
else
binCi+=Integer.toString(perm[i]);
System.out.print(perm[i]);
}
int Round=1;

DesPanel.StepsText.append("*********Block Number *********"+(f+1)+'\n');
System.out.println(" ");
Permutation p= new Permutation();
p.FillPermutation();//step1 from 2D to 1D
p.DoIP(perm,perm_out);//step1 -array
DoSegmentation(perm_out);//step2 to Left and Right
// Here u will work on Right (32 Bits ) & Left 32 Bits
////////////////////////////////////////////////////////////////////////
/////////////////////////////key Generation////////////////////////////

key.FillPC_1();//step1 -array
System.out.println();
DesPanel.StepsText.append("*********Key Generation*********" +'\n');
key.DoPC_1(key_in,key_out);//step1

key.DoSegementation(key_out,C,D);//step2

while(Round<=16) {
DesPanel.StepsText.append("*********Round Number********* "+Round+'\n');

int chooser=num_Left[index_chooser];
ESTable Etable = new ESTable();
Etable.FillETable();//step3 array from 2D to 1D
Etable.DoETable(Right,Right_out);//step3

////////////////////////////////////////////////////////////////////////
System.out.println();
Chooser(chooser);//step 3 - shift
System.out.println("Chooser = "+chooser);
key.FillPC_2();//step4 array
FillC_D();//step4 contacnate 56bits
DesPanel.StepsText.append("*********Key For Round Number********* "+Round+'\n');
key.DoPC_2(C_D,final_key);//step4 here the key of Round 1
for(int i=0; i<48;i++) {
if(key_reverse==null)
key_reverse=Integer.toString(final_key[i]);
else
key_reverse+=Integer.toString(final_key[i]);

}


////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
DesPanel.StepsText.append("*********Right Part XORED with Round Key********* "+Round+'\n');
XOR(Right_out,final_key,XOR_Out);//step1
ind=0;
for(int g=0; g<6; g++){
for(int j=0; j<8; j++){
XORS[g][j]=Integer.toString(XOR_Out[ind]);
ind++;

}
}
ind=0;

arrayprinter.printarray(XORS,"XOR Result");
System.out.print("\nXOR :");
for(int j=0;j<48;j++) {
System.out.print(XOR_Out[j]);
}
SBox sbox= new SBox();

sbox.DoSBox(XOR_Out,after_SBox);//step2 32bits - include permitation
System.out.print("\nS-BOX :");
for(int j=0;j<32;j++) {
System.out.print(after_SBox[j]);
}
System.out.print("\n AFter XOR :");
DesPanel.StepsText.append("*********Left Part XORED with Output Function in Round********* "+Round+'\n');
XOR(Left,after_SBox,aft_XOR_fuc);//XOR



for(int g=0; g<4; g++){
for(int j=0; j<8; j++){
aft_XOR_fucS[g][j]=Integer.toString(aft_XOR_fuc[ind]);
ind++;

}
}
ind=0;
arrayprinter.printarray(aft_XOR_fucS,"XOR Result");

for(int j=0;j<32;j++) {
System.out.print(aft_XOR_fuc[j]);
}


Round++;
index++;
index_chooser++;

DesPanel.StepsText.append("*********Left=Right & Right=Left*********"+'\n');
for(int i=0;i<32;i++) {
Left[i]=Right[i];
Right[i]=aft_XOR_fuc[i];
}

ind=0;
for(int g=0; g<4; g++){
for(int j=0; j<8; j++){
LeftS[g][j]=Integer.toString(Right[ind]);
RightS[g][j]=Integer.toString(aft_XOR_fuc[ind]);
ind++;

}
}
arrayprinter.printarray(LeftS,"Left Part");
arrayprinter.printarray(RightS,"Right Part");


}

DesPanel.StepsText.append("********After Swap Operation*********"+'\n');
swap32();

contancate();
System.out.println("/nBit Swap :");
for(int i=0;i<64;i++) {
System.out.print(Block64[i]);
}

p.FillInversePermutation();//step1 from 2D to 1D
DesPanel.StepsText.append("********* Inverse Permutation Operation in Round *********"+(f+1)+'\n');
p.DoInverseIP(Block64,newBlock64);//step1 -array
System.out.println(" ");
System.out.println("/nThe Encyption code: ");
for(int i=0; i<newBlock64.length; i++){
System.out.print(newBlock64[i]);
if(finalEncry==null)
finalEncry=Integer.toString(newBlock64[i]);
else
finalEncry+=Integer.toString(newBlock64[i]);

}
start=end;
end=end+8;
index_chooser=0;

}
System.out.println("");
System.out.println("Final :"+ finalEncry);



}
public String getEncryption(){
return finalEncry;
}

public String getDecryption(){
return finalDecry;
}
public String getBinCi(){
return binCi;
}
private int ind=0;
private String binDec;
private String binCi;
private static Permutation p;
private String keyword;
private int index_chooser=0;
private String PlainText;
private static int index=0;
private String cyphir;
private static int [] reversedkey=new int[48];
private static String key_reverse ;
private static KeyGen key=new KeyGen();
private static int Block64[]=new int[64];
private static int[]num_Left= {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
private static int ch[]=new int[8];
private static int newBlock64[]=new int[64];
private static int [] aft_XOR_fuc=new int[32];
private static String [][] aft_XOR_fucS=new String[4][8];
private static int [] after_SBox=new int[32];
private static int[] C_D=new int[56];
private static int[] XOR_Out=new int[48];
private static int key_in[]=new int[64];
private static int final_key[]=new int[48];
private static int key_out[]=new int[56];
private static int [] Left=new int[32];
private static int[] Right=new int[32];
private static int[] Right_out=new int[48];
private static int[] perm=new int[64];
private static int[] perm_out= new int[64];
private static int newBlock64_[]=new int[64];
private static int C[]=new int[28];
private static int D[]=new int[28];
private byte[][] block=new byte[8][8];
private String [][] blockS= new String[8][8];
private String finalEncry;
private String finalDecry;
private String Plain_Dec;
private String LeftS[][] = new String[4][8];
private String RightS[][] = new String[4][8];
private String XORS[][] = new String[6][8];
private String [][] newBlock64_S = new String[8][8];
}



Code:
DesMain.java

java code
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.DocumentFilter;

public class DesMain {

/** Creates a new instance of DesMain */
public DesMain() {
}

public static void main(String [] rr){
DesPanel mm = new DesPanel();
mm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mm.setVisible(true);
}
}
class DesPanel extends JFrame{
public DesPanel(){
this.setTitle("Des");
this.setSize(800,800);
this.setVisible(true);

GenerateGUI();
}
encrypt en;
public static JTextArea StepsText = new JTextArea(10,40);


private Container c=this.getContentPane();
private JButton btnCihper = new JButton("Cipher");
private JButton btnDeCihper = new JButton("Decipher");

private JTextArea CipherText = new JTextArea(4,40);
private JTextArea OriginalText = new JTextArea(4,40);
private JTextArea DeCipherText = new JTextArea(4,40);


private JScrollPane OriginalScorl=new JScrollPane(OriginalText,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
private JScrollPane CipherScorl=new JScrollPane(CipherText,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
private JScrollPane DecipherScorl=new JScrollPane(DeCipherText,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
private JScrollPane StepScorl=new JScrollPane(StepsText,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
private JTextField KeyWord = new JTextField(16);
private JTextField KeyWordTwo = new JTextField(16);
private JPanel PanelCipher = new JPanel();
private JPanel PanelDecipher = new JPanel();
private JPanel PanelKeyWord = new JPanel();

private JPanel PanelOriginaltxt = new JPanel();
private JPanel jpstep=new JPanel();

private JLabel lblKeyWord= new JLabel("KeyWord : ");


private void GenerateGUI(){
c.setLayout(new GridLayout(4,1));

Document doc = KeyWord.getDocument();
AbstractDocument absDoc = (AbstractDocument )doc;
absDoc.setDocumentFilter(new DocumentSizeFilter(8));
KeyWord.setSize(10,1);


c.setLayout(new FlowLayout());

PanelKeyWord.setLayout(new FlowLayout());
PanelKeyWord.add(lblKeyWord);
PanelKeyWord.add(KeyWord);


PanelOriginaltxt.setBorder(BorderFactory.createTitledBorder("Original Text"));
PanelOriginaltxt.setLayout(new FlowLayout());
PanelOriginaltxt.add(OriginalScorl);

PanelCipher.setBorder(BorderFactory.createTitledBorder("Cipher Text"));
PanelCipher.setLayout(new FlowLayout());
PanelCipher.add(CipherScorl);
PanelCipher.add(btnCihper);

PanelDecipher.setBorder(BorderFactory.createTitledBorder("Decipher Text"));
PanelDecipher.setLayout(new FlowLayout());
PanelDecipher.add(DecipherScorl);
PanelDecipher.add(btnDeCihper);
jpstep.setBorder(BorderFactory.createTitledBorder("Mointor"));
jpstep.setLayout(new FlowLayout());
jpstep.add(StepScorl);


c.add(PanelKeyWord);

c.add(PanelOriginaltxt);
c.add(PanelCipher);
c.add(PanelDecipher);
c.add(jpstep);

DoActions();


}
private void DoActions(){
ActionHandler action = new ActionHandler();

btnCihper.addActionListener(action);
btnDeCihper.addActionListener(action);

}

private class ActionHandler implements ActionListener{


public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnCihper){
String plaintext=OriginalText.getText();
String keyworD=KeyWord.getText();
StepsText.append("keyword : "+keyworD+'\n');
StepsText.append("PlainText : "+plaintext+'\n');
en= new encrypt(plaintext,keyworD);
en.DoEncryption();
//OriginalText.append("\n "+ en.getBinCi());

CipherText.append(en.getEncryption());

}
if(e.getSource()==btnDeCihper){
en.DoDecryption();
DeCipherText.append("\n "+en.getBinDec());
DeCipherText.setCaretPosition(DeCipherText. getDocument().getLength());
DeCipherText.append("\n"+en.getDecryption());
}
}

}
private String fina;

}

class DocumentSizeFilter extends DocumentFilter {
int maxCharacters;

public DocumentSizeFilter(int maxChars) {
maxCharacters = maxChars;
}

public void insertString(FilterBypass fb, int offs, String str, AttributeSet a) throws BadLocationException {

if ((fb.getDocument().getLength() + str.length()) <= maxCharacters)
super.insertString(fb, offs, str, a);
else
Toolkit.getDefaultToolkit().beep();
}

public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a) throws BadLocationException {

if ((fb.getDocument().getLength() + str.length()- length) <= maxCharacters)
super.replace(fb, offs, length, str, a);
else
Toolkit.getDefaultToolkit().beep();
}

}

ESTable.java
java code
public class ESTable {

/** Creates a new instance of ESTable */
public ESTable() {
}

public void FillETable() {
int index=0;
for(int row=0;row<8; row++){
for(int col=0; col<6; col++){
store_num[index]=ETable[row][col];
index++;
}
}

}
public void DoETable(int [] Right_in, int [] Right_out){
int temp=0;
int i=0;
int loop=0;
int check=0;
while(check!=48){
temp=store_num[i];
if(temp==loop){
Right_out[check]=Right_in[loop-1];
loop=0;
check++;
i++;
}
loop++;
}
System.out.println(" ");
System.out.print(" E- table ");
for(int j=0;j<48;j++)
System.out.print(Right_out[j]);
int index=0;
for(int g=0; g<6; g++){
for(int j=0; j<8; j++){
RightS[g][j]=Integer.toString(Right_out[index]);
index++;
}
}
arrayprinter.printarray(RightS,"After E-Table");
index=0;
}


private int[] store_num= new int[48];
private int [][] ETable= {{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 String RightS[][] = new String[6][8];
}

Code:
KeyGen.java

java code
public class KeyGen {

/** Creates a new instance of KeyGen */
public KeyGen() {

}

public void FillPC_1() {
int index=0;
for(int row=0;row<8; row++){
for(int col=0; col<7; col++){
store_num[index]=PC_1[row][col];
index++;
}
}
}

public void FillPC_2() {
int index=0;
for(int row=0;row<8; row++){
for(int col=0; col<6; col++){
store_num1[index]=PC_2[row][col];
index++;
}
}
}
public void DoPC_1(int [] key_in, int [] key_out) {
int temp=0;
int i=0;
int loop=0;
int check=0;
while(check!=56){
temp=store_num[i];
if(temp==loop){
key_out[check]=key_in[loop-1];
loop=0;
check++;
i++;
}
loop++;
}

System.out.println("The Permutted key");
for(int j=0; j<key_out.length; j++){
System.out.print(key_out[j]);
}

int index=0;
for(int g=0; g<7; g++){
for(int j=0; j<8; j++){
keyS[g][j]=Integer.toString(key_out[index]);
index++;
}
}

arrayprinter.printarray(keyS,"After PC-1");
}


public void DoPC_2(int [] key_in, int [] key_out) {
int temp=0;
int i=0;
int loop=0;
int check=0;
while(check!=48){
temp=store_num1[i];
if(temp==loop){
key_out[check]=key_in[loop-1];
loop=0;
check++;
i++;
}
loop++;
}

int index=0;
for(int g=0; g<6; g++){
for(int j=0; j<8; j++){
finalS[g][j]=Integer.toString(key_out[index]);
index++;
}
}

arrayprinter.printarray(finalS,"After PC-2");
index=0;
}

public void DoSegementation(int [] key_out,int [] C, int[] D){
int index=0;
for(int i=0; i<28; i++)
C[i]=key_out[i];

for(int i=28; i<56; i++){
D[index]=key_out[i];

index++;
}


index=0;
for(int g=0; g<4; g++){
for(int j=0; j<7; j++){
cS[g][j]=Integer.toString(key_out[index]);
index++;
}

}

for(int g=0; g<4; g++){
for(int j=0; j<7; j++){
dS[g][j]=Integer.toString(key_out[index]);
index++;
}



}
index=0;

arrayprinter.printarray(cS,"Segment Key to C part");
arrayprinter.printarray(dS,"Segment Key to D part");
}
public void Do_OneLeftShitf(int[] side1, int []side2){

int temp=side1[0];
for(int i=1; i<side1.length; i++){

side1[i-1]=side1[i];


}
side1[side1.length-1]=temp;
temp=side2[0];
for(int i=1; i<side2.length; i++){

side2[i-1]=side2[i];


}
side2[side2.length-1]=temp;

System.out.println("After One left shift");
for(int j=0; j<side1.length; j++){
System.out.print(side1[j]);
}
}






private int[] store_num= new int[56];
private int[] store_num1= new int[48];
private int[][] PC_1={{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[][] PC_2={{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 String key;
private String keyS[][]= new String[7][8];
private String cS[][] = new String [4][7];
private String dS[][] = new String [4][7];
private String finalS[][] = new String [6][8];

}

Permutation.java
java code
class Permutation {


public Permutation() {

}
public void FillPermutation() {
int index=0;
for(int row=0;row<8; row++){
for(int col=0; col<8; col++){
store_num[index]=permutation[row][col];/// from 2D to 1 D
index++;
}
}

}
public void FillInversePermutation() {
int index=0;
for(int row=0;row<8; row++){
for(int col=0; col<8; col++){
store_num[index]=inverperm[row][col];/// from 2D to 1 D
index++;
}
}

}
public void DoIP(int [] perm_in, int [] perm_out) {
int temp=0;
int i=0;
int loop=0;
int check=0;
while(perm_in.length!=check){
temp=store_num[i];
if(temp==loop){
perm_out[check]=perm_in[loop-1];
loop=0;
check++;
i++;
}
loop++;
}
System.out.println("The Permutted output");
for(int j=0; j<perm_out.length; j++){
System.out.print(perm_out[j]);

}
int index=0;
for(int g=0; g<8; g++){
for(int j=0; j<8; j++){
permS[g][j]=Integer.toString(perm_in[index]);
index++;
}
}

arrayprinter.printarray(permS,"After Initial Permutation");

}
public void DoInverseIP(int [] perm_in, int [] perm_out) {
int temp=0;
int i=0;
int loop=0;
int check=0;
while(perm_in.length!=check){
temp=store_num[i];
if(temp==loop){
perm_out[check]=perm_in[loop-1];
loop=0;
check++;
i++;
}
loop++;
}
int index=0;
for(int d=0; d<4;d++){
for(int j=0; j<8; j++){
newBlock64[d][j]=Integer.toString(perm_out[index]);

index++;
}
}
arrayprinter.printarray(newBlock64,"After IP");

}
private int[] store_num= new int[64];
private int permutation[][]={{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 inverperm[][]=
{{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 String permS[][] = new String[8][8];
private String newBlock64 [][]= new String[4][8];


}

Code:
SBox.java

java code
public class SBox {

/** Creates a new instance of SBox */
public SBox() {
}

public void DoDecimal(int [] num){
dec1[0]=num[0];
dec1[1]=num[5];
row=1*dec1[1]+2*dec1[0];

//System.out.print("-------"+row);

}


public void DoFourDecimal(int [] num){
dec2[0]=num[1];
dec2[1]=num[2];
dec2[2]=num[3];
dec2[3]=num[4];
col=1*dec2[3]+2*dec2[2]+dec2[1]*4+dec2[0]*8;

//System.out.print("***********"+col);

}
public void SelectSBox(int choice, int row, int col){

switch(choice){
case 0: SBox_Result[index1]=SBOX1[row][col]; index1++; break;
case 1: SBox_Result[index1]=SBOX2[row][col]; index1++; break;
case 2: SBox_Result[index1]=SBOX3[row][col]; index1++; break;
case 3: SBox_Result[index1]=SBOX4[row][col]; index1++; break;
case 4: SBox_Result[index1]=SBOX5[row][col]; index1++; break;
case 5: SBox_Result[index1]=SBOX6[row][col]; index1++; break;
case 6: SBox_Result[index1]=SBOX7[row][col]; index1++; break;
case 7: SBox_Result[index1]=SBOX8[row][col]; index1++; break;
}

}

public void make32bit(int num){
int num1 = 0,num2,num3;
num1=num;
System.out.println("Code ");
for(int i=0; i<4; i++){

num2=num1%2;
num3=num1/2;
num1=num3;
after_SBox[index2]=num2;

System.out.print(after_SBox[index2]);
index2++;

}

}
public void Reverse(int [] num){
int count=0;
int fix=3;
int temp1,temp2;
while(count!=32){
for(int i=0; i<2; i++){

temp1=num[count+i];

num[count+i]=num[fix-(count+i)];
num[fix-(count+i)]=temp1;

}
fix+=8;
count+=4;
}


}
public void Fill_P() {
int index=0;
for(int row=0;row<8; row++){
for(int col=0; col<4; col++){
store_num[index]=P[row][col];
index++;
}

}


}

public void Do_P(int [] after_SBox, int [] func_out ){
int temp=0;
int i=0;
int loop=0;
int check=0;
while(check!=32){
temp=store_num[i];
if(temp==loop){
func_out[check]=after_SBox[loop-1];
loop=0;
check++;
i++;
}
loop++;
}

}

public void DoSBox(int [] XOR_Out, int [] S_Out){

DesPanel.StepsText.append("*********S-Box Stages********* "+'\n');
int count=0;
int choice=0;
int i;
index=0;
while(count!=48){

for(i=0; i<6; i++) {
temp[i]=XOR_Out[i+count];

}
System.out.println(" ");
DoDecimal(temp);
// System.out.println();
DoFourDecimal(temp);
// System.out.println();
SBox_row[index]=row;
SBox_col[index]=col;
SelectSBox(choice,SBox_row[index],SBox_col[index]);
System.out.println(index+" ="+SBox_Result[index]+"ROW "+row+" COL "+col);

make32bit(SBox_Result[index]);


index++;
choice++;
count+=6;

}
Reverse(after_SBox);
index=0;

for(int d=0 ; d<4; d++){
for(int j=0; j<8; j++){
after_SBoxs[d][j]=Integer.toString(after_SBox[index]);
index++;
}
}
index=0;
arrayprinter.printarray(after_SBoxs,"Final Result After SBoxes");

Fill_P();
Do_P(after_SBox,func_out);


for(int d=0 ; d<4; d++){
for(int j=0; j<8; j++){
func_outS[d][j]=Integer.toString(func_out[index]);
index++;
}
}
index=0;
arrayprinter.printarray(func_outS,"After IP");
for(int j=0; j<32; j++)
S_Out[j]=func_out[j];
// System.out.println();


}

private int index=0;
private int[] func_out=new int[32];
private int [] store_num=new int[32];
private String text48;
private int [] after_SBox=new int[32];
private int index1=0;
private int index2=0;
private int row=0;
private int col=0;
private int [] SBox_Result=new int [8];
private int[] SBox_row=new int[8];
private int[] SBox_col= new int[8];
private int [] dec1=new int[2];
private int [] dec2=new int[4];
private int[] temp =new int[6];
private int [][] SBOX1=
{{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 [][] SBOX2=
{{15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12 ,0, 5, 1},
{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 [][] SBOX3={{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 [][] SBOX4={{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 [][] SBOX5={{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 [][] SBOX6={{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 [][] SBOX7={ {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 [][] SBOX8={ {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 [][] P={{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 String after_SBoxs [][]= new String[4][8];
private String func_outS [][]= new String[4][8];
}


arrayprinter.java
java code
public class arrayprinter {

/** Creates a new instance of arrayprinter */
public arrayprinter() {
}
public static void printarray(String[][] arr,String label) {
DesPanel.StepsText.append("-- "+label+" -- "+'\n');
//System.out.println("-- "+label+" -- ");
for(int i=0;i<arr.length ;i++) {
DesPanel.StepsText.append("| ");
for(int j=0;j<arr[0].length;j++) {

DesPanel.StepsText.append(arr[i][j]+" ");
}
DesPanel.StepsText.append("| "+'\n');
}
}
}





Attachments:
File comment: DES source code
Des.rar [101.68 KiB]
Downloaded 9799 times

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada
Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

thank you



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

private int [][] SBOX2=
{{15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12 ,0, 5, 1},
{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 [][] SBOX2=
{{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}



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

i'm new to Java and am trying to run this code. I have Netbeans open and have the .rar file open - could someone please tell me what to do now?



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

enter the key , and message .


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


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

Thanks for your response ms3_333. My question is how do i run the code? What do I do with the files contained in the .rar file. Do i just double click one, do i paste the contents of another. I'm sorry if this is a basic question but i'm just starting to program.



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

Ok, I have the initial text :

-- PlainText Text in Binary --
| 0 1 0 0 0 0 0 1 |
| 0 1 1 0 0 0 1 1 |
| 0 1 1 0 0 1 0 1 |
| 0 1 1 1 0 0 1 1 |
| 0 1 1 1 0 1 0 0 |
| 0 1 1 0 0 0 0 1 |
| 0 0 1 0 0 0 0 0 |
| 0 1 1 0 0 1 0 1 |

Your algorithm says that after initil permutation it becomes:

| 0 1 0 0 0 0 0 1 |
| 0 1 1 0 0 0 1 1 |
| 0 1 1 0 0 1 0 1 |
| 0 1 1 1 0 0 1 1 |
| 0 1 1 1 0 1 0 0 |
| 0 1 1 0 0 0 0 1 |
| 0 0 1 0 0 0 0 0 |
| 0 1 1 0 0 1 0 1 |

Which is clearly wrong, because the first number should be 1 ( from the 58 th position in the first text in binary) . So, am I missing something or ....? :huh:



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

Thank you for the downloadable link and explanation about the algorithm



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

Are you sure this code works according to the standard of DES?



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

yes sir :) .


_________________
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  [ 16 posts ]  Go to page 1, 2  Next

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



Topic Tags

Java Algorithms, Java Projects







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