You are on page 1of 25

TUGAS KRIPTOGRAFI

SOURCE CODE DES (DATA ENCRYPTION STANDARD)


DALAM BAHASA JAVA

DISUSUN OLEH :
KELOMPOK IV
GILBERT CHRISTOPHER (09021181320007)
YAUMIL FADHILAH (09021181320009)
ALVIN TAMAARSA (09021181320016)
IRWAN JERY SIHITE (09021181320031)
RUDI PURNIAWAN (09021181320054)
DOSEN PENGAMPU MATA KULIAH :
DRS. MEGAH MULYA, M.T.
TEKNIK INFORMATIKA
FAKULTAS ILMU KOMPUTER
UNIVERSITAS SRIWIJAYA
2015

SOURCE CODE DES


Source code ini dibuat dalam bahasa Java dengan Netbeans. Source code ini terdiri dari beberapa modul,
antara lain :
1.
2.
3.
4.
5.
6.
7.

DesMain.java
ESTable.java
KeyGen.java
Permutation.java
Sbox.java
arrayprinter.java
encrypt.java

DesMain.ja
va

import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import

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

public class 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_AL
WAYS);
private
JScrollPane CipherScorl=new JScrollPane(CipherText,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AL
WAYS);
private JScrollPane DecipherScorl=new JScrollPane(DeCipherText,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AL
WAYS);
private
JScrollPane StepScorl=new JScrollPane(StepsText,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AL
WAYS);
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();
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

ESTable.jav
a

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();
}
}
public class 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];

KeyGen.jav
a

}
public class 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
private
private
private
private

Permutatio
n.java

String
String
String
String
String

key;
keyS[][]= new String[7][8];
cS[][] = new String [4][7];
dS[][] = new String [4][7];
finalS[][] = new String [6][8];

}
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];

SBox.java

}
public class SBox {
public SBox()
{
}
public void DoDecimal(int [] num){
dec1[0]=num[0];
dec1[1]=num[5];
row=1*dec1[1]+2*dec1[0];
}
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;
}
public void SelectSBox(int choice, int row, int col){
switch(choice){
case 0: SBox_Result[index1]=SBOX1[row][col]; index1++;
case 1: SBox_Result[index1]=SBOX2[row][col]; index1++;
case 2: SBox_Result[index1]=SBOX3[row][col]; index1++;
case 3: SBox_Result[index1]=SBOX4[row][col]; index1++;
case 4: SBox_Result[index1]=SBOX5[row][col]; index1++;

break;
break;
break;
break;
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);
DoFourDecimal(temp);
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];
}
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 ,15 ,7 ,4 ,14 ,2 ,13 ,1 ,10 ,6 ,12 ,11 ,9 ,5
{4 ,1 ,14 ,8 ,13 ,6 ,2 ,11 ,15 ,12 ,9 ,7 ,3 ,10
{15, 12, 8, 2, 4, 9, 1, 7 ,5, 11, 3, 14, 10, 0,
};
private int
{{15, 1, 8,
{3, 13, 4,
{0, 14, 7,
{13 ,8 ,10
};
private
{{10 ,0
{13, 7,
{13, 6,
{1, 10,
};

,0
,3
,5
6,

,7},
,8},
,0},
13}

[][] SBOX2=
14, 6, 11, 3, 4, 9, 7, 2, 13, 12 ,0, 5, 1},
7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9 ,11, 5},
11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15},
,1 ,3 ,15 ,4 ,2 ,11 ,6 ,7 ,12 ,0 ,5 ,14 ,9}

int [][] SBOX3=


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


{{12 ,1 ,10 ,15,
{10, 15, 4 ,2 ,7
{9, 14, 15, 5, 2
{4, 3, 2, 12, 9,
};

SBOX6=
9 ,2, 6 ,8 ,0 ,13 ,3 ,4 ,14 ,7 ,5
,12 ,9, 5, 6, 1, 13 ,14 ,0, 11, 3
,8 ,12, 3, 7 ,0 ,4 ,10, 1, 13 ,11
5, 15 ,10 ,11, 14, 1, 7, 6, 0, 8,

,11},
,8},
,6},
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

Encrypt.jav
a

}
ublic class arrayprinter
{
public arrayprinter()
{
}
public static void printarray(String[][] arr,String label){
DesPanel.StepsText.append("-- "+label+" -- "+'\n');
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');
}
}
}
import java.util.Scanner;

public class 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));
}
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]);
}
}
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 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);
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
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
private
private
private
private
private
private
private
}

String
String
String
String
String
String
String
String

[][] blockS= new String[8][8];


finalEncry;
finalDecry;
Plain_Dec;
LeftS[][] = new String[4][8];
RightS[][] = new String[4][8];
XORS[][] = new String[6][8];
[][] newBlock64_S = new String[8][8];

You might also like