of 2 /2
FEISTEL CIPHER import java.io.*; import java.util.*; class feistel { public static void main(String args[]) throws Exception { int a[]=new int[20]; int left[]=new int[10]; int right[]=new int[10]; int key[]=new int[10]; int temp[]=new int[10]; int m[]=new int[10]; int i,k; Scanner sc=new Scanner(System.in); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter length of string:"); int n=Integer.parseInt(br.readLine()); System.out.println("Enter plaintext:"); for(i=0;i<n;i++) a[i]=sc.nextInt(); System.out.println("Enter key:"); for(i=0;i<(n/2);i++) key[i]=sc.nextInt(); for(i=0;i<(n/2);i++) left[i]=a[i]; for(int j=0;i<n;i++,j++ ) right[j]=a[i]; System.out.println("Left string:"); for(i=0;i<(n/2);i++) System.out.print(left[i]); System.out.println(); System.out.println("Right string:"); for(i=0;i<(n/2);i++) System.out.print(right[i]); for(k=0;k<4;k++) { for(i=0;i<(n/2);i++) { if(key[i]==right[i]) temp[i]=0; else temp[i]=1; } for(i=0;i<(n/2);i++) { if(left[i]==temp[i]) m[i]=0; else m[i]=1; }

# Feistel Cipher

Embed Size (px)

DESCRIPTION

Program for Feistel Cipher

### Text of Feistel Cipher

7/15/2019 Feistel Cipher

FEISTEL CIPHER

import java.io.*;

import java.util.*;

class feistel

{

public static void main(String args[]) throws Exception

{int a[]=new int[20];

int left[]=new int[10];

int right[]=new int[10];

int key[]=new int[10];

int temp[]=new int[10];

int m[]=new int[10];

int i,k;

Scanner sc=new Scanner(System.in);

System.out.println("Enter length of string:");

for(i=0;i<n;i++)

a[i]=sc.nextInt();

System.out.println("Enter key:");

for(i=0;i<(n/2);i++)

key[i]=sc.nextInt();

for(i=0;i<(n/2);i++)

left[i]=a[i];

for(int j=0;i<n;i++,j++)

right[j]=a[i];

System.out.println("Left string:");

for(i=0;i<(n/2);i++)

System.out.print(left[i]);

System.out.println();

System.out.println("Right string:");

for(i=0;i<(n/2);i++)

System.out.print(right[i]);

for(k=0;k<4;k++)

{

for(i=0;i<(n/2);i++)

{

if(key[i]==right[i])

temp[i]=0;else

temp[i]=1;

}

for(i=0;i<(n/2);i++)

{

if(left[i]==temp[i])

m[i]=0;

else

m[i]=1;

}

7/15/2019 Feistel Cipher

for(i=0;i<(n/2);i++)

left[i]=right[i];

for(i=0;i<(n/2);i++)

right[i]=m[i];

System.out.println();

System.out.println("Round " + (k+1));

System.out.print("Left:");

for(i=0;i<(n/2);i++)

System.out.print(left[i]);System.out.println();

System.out.print("Right:");

for(i=0;i<(n/2);i++)

System.out.print(right[i]);

}

System.out.println();

System.out.println("Encrypted string is:");

for(i=0;i<(n/2);i++)

{

System.out.print(left[i]);

}

System.out.print(" ");

for(i=0;i<(n/2);i++)

System.out.print(right[i]);

}

}

/*OUTPUT:

C:\ins>java feistel

Enter length of string:

8

Enter plaintext:

0 1 0 1 0 1 0 1Enter key:

1 0 1 0

Left string:

0101

Right string:

0101

Round 1

Left:0101

Right:1010

Round 2

Left:1010Right:0101

Round 3

Left:0101

Right:0101

Round 4

Left:0101

Right:1010

Encrypted string is:

0101 1010

*/

Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Education
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents