You are on page 1of 3

import java.io.

*; class rec { char r[][]=new char[99][99]; char r1[][]=new char[99][99]; static int l,lu,k,g1; static int j=1,e=1; static int m=1,f=1,y=1; int count=1; rec(int d) { l=d; k=d; } int enc(int b,int u) { m=u; if(m<=l) { r[m][j]=(char)b; m++; j++; lu=m; } else if(m>l) { if(k>=1) { k--; if(k>0) { r[k][j]=(char)b; } else { k=2; r[k][j]=(char)b; m=k+1; k=l; lu=m; } } j++; } return lu; } int val() {

return j; } void dec(int b,int len) { r1[y][e]=(char)b; if(e==len) { e=0; y++; } e++; } char[][]ans() { return r; } char[][]ans1() { return r1; } } class rail_fence { static int depth; static int i; static int v; static int va=1; static int va1; static int ro1; public static void main(String a[]) throws java.io.IOException { String source_file=a[1]; String Intermediate_file=a[2]; String destination_file=a[3]; depth=Integer.parseInt(a[0]); char choice; int m1; System.out.println("Enter your choice:"); choice=(char)System.in.read(); switch(choice) { //Case 1 is for enciphering process case '1': File f1=new File(source_file); FileReader f=new FileReader(f1); rec ob=new rec(depth);

while(( i=f.read())!=-1) { va1=ob.enc(i,va); va=va1; } f.close();

char buf[][]=ob.ans(); v=ob.val()-1; FileWriter fw=new FileWriter(Intermediate_file); System.out.println(); for(int p=1;p<=depth;p++) { for(int q=1;q<=v;q++) fw.write(buf[p][q]); } fw.close(); break; //Case2 is for deciphering process case '2': FileReader f2=new FileReader(Intermediate_file); File f3=new File(source_file); long v2=f3.length(); int v3=(int)v2; rec ob1=new rec(depth); while(( i=f2.read())!=-1) { ob1.dec(i,v3); } f2.close(); char buf1[][]=ob1.ans1(); FileWriter fw1=new FileWriter(destination_file); System.out.println(); for(int p=1;p<=v3;p++) { for(int q=1;q<=depth;q++) fw1.write(buf1[q][p]); } fw1.close(); break; default: System.out.println("Your choice is invalid:"); break; } } }

You might also like