0% found this document useful (0 votes)
134 views

RailFence Transformation

The Java code implements the rail fence cipher encryption and decryption algorithm. It takes an input string, encrypts it by alternating characters between two output strings, and concatenates the output strings. It then decrypts the encrypted string by splitting it in half and alternating characters back to the original string.

Uploaded by

Harish Hari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
134 views

RailFence Transformation

The Java code implements the rail fence cipher encryption and decryption algorithm. It takes an input string, encrypts it by alternating characters between two output strings, and concatenates the output strings. It then decrypts the encrypted string by splitting it in half and alternating characters back to the original string.

Uploaded by

Harish Hari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Source Code:

public class railfence {


public static void main(String args[])
{
String input = inputstring;
String output = ;
int len = input.length(),flag = 0;
System.out.println(Input String : + input);
for(int i=0;i<len;i+=2) {
output += input.charAt(i);
}
for(int i=1;i<len;i+=2) {
output += input.charAt(i);
}
System.out.println(Ciphered Text : +output);
}
}

Output:

8 comments
Add yours

1.
Rohit
August 1, 2014 3:34 am

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
//RailFence Technique Done using Swings
class railfence extends JFrame implements
ActionListener
{
JTextField t1,t2;
JButton b1;
JLabel l1,l2;
Container c;
railfence()

{ c=getContentPane();
c.setLayout(new GridLayout(3,2,2,3));
t1=new JTextField(10);
t2=new JTextField(10);
l1=new JLabel(Enter Your Message to Encrypt:);
l2=new JLabel(Encrypted Text:);
t2.setEditable(false);
b1=new JButton(Submit);
b1.addActionListener(this);
c.add(l1); c.add(t1);
c.add(l2); c.add(t2);
c.add(b1);
setTitle(RailFence Technique);
setVisible(true);
pack();
}
public void actionPerformed(ActionEvent ae)
{
encrypt();
}
void encrypt()
{

String s=t1.getText();
int len=s.length();
String s1=;
String s2=;
for(int i=0;i<len;i=i+2)
{
s1=s1+""+s.charAt(i);
}
for(int j=1;j<len;j=j+2)
{
s2=s2+""+s.charAt(j);
}
s1=s1+s2;
t2.setText(s1);
}
public static void main(String args[])
{
new railfence();
}
}
Reply

Sohrab
August 1, 2014 6:50 pm

Good program with swings


Reply

2.
Mahesh Patel
August 1, 2014 3:59 am

RailFence program for encryption and decryption


taking user inputs
import java.io.*;
class RailFence
{
String encrypt(String plain)
{
String s1=,s2=;
for(int i=0;i<plain.length();i++)
{
if(i%2==0)
{s1=s1+plain.charAt(i);
}
else

{s2=s2+plain.charAt(i);
}
}
String cipher=s1+s2;
return cipher;
}
String decrypt(String cipher)
{
String h1,h2,plain="";
int len=cipher.length();
h1=cipher.substring(0,(len/2-1));
h2=cipher.substring(len/2,(len-1));
for(int i=0;i<(len/2-1);i++)
{
plain=plain+h1.charAt(i);
plain=plain+h2.charAt(i);
}
return plain;
}
public static void main(String args[]) throws
IOException
{
RailFence r=new RailFence();

try
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("enter the plain string \n");
String s=br.readLine();
String cipher=r.encrypt(s);
System.out.println("encrypted string is "+cipher);
System.out.println("decrypted string is
"+r.decrypt(cipher));
}
catch(Exception e){e.printStackTrace();}
}
}
Reply

o
raj lalwani
August 12, 2014 4:01 pm

//RailFence program for


encryption and decryption taking
user inputs//

import java.io.*;
class RailFence
{
String encrypt(String plain)
{
String s1=,s2=;
for(int i=0;i<plain.length();i++)
{
if(i%2==0)
{
s1=s1+plain.charAt(i);
}
else
{
s2=s2+plain.charAt(i);
}
}
String cipher=s1+s2;
return cipher;
}
String decrypt(String cipher)
{
String h1,h2,plain="";

int len=cipher.length();
h1=cipher.substring(0,len/2);
h2=cipher.substring(len/2,len);
for(int i=0;i<len/2;i++)
{
plain=plain+h1.charAt(i);
plain=plain+h2.charAt(i);
}
return plain;
}
public static void main(String
args[]) throws IOException
{
RailFence r=new RailFence();
try
{
BufferedReader br=new
BufferedReader(new
InputStreamReader(System.in));
System.out.println("enter the
plain string ");
String s=br.readLine();
String cipher=r.encrypt(s);

System.out.println("encrypted
string is "+cipher);
System.out.println("decrypted
string is "+r.decrypt(cipher));
}
catch(Exception e)
{e.printStackTrace();}
}
}
//mahesh ur program is right just
few changes in decryption:
Reply

o
lakshita
August 19, 2014 3:37 am

idhar bhi pateli


Reply

3.
Lakshita
August 19, 2014 3:14 pm

import java.util.*;
public class RailFence
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
String text;
int x;
System.out.println(Enter Text :);
text = s.nextLine();
System.out.println(Press :\t1 to Encrypt\n\t2 to
Decrypt);
x = s.nextInt();
if(x==1)
{
encrypt(text);
}
else
{
decrypt(text);
}
}

public static void encrypt(String text)


{
String out1 = , out2 = , output = ;
int l = text.length();
for(int i=0;i<l;i++)
{
if(i%2==0)
out1 += text.charAt(i);
else
out2 += text.charAt(i);
}
output = out1.concat(out2);
System.out.println("Ciphered Text : "+output);
}
public static void decrypt(String text)
{
String output = "";
int l = text.length(), k;
if(l%2!=0)
{
k = l / 2 + 1;
}
else

{
k = l/2;
}
for(int i=0;i<l/2;i++)
{
output += text.charAt(i);
output += text.charAt(k+i);
}
if(l%2!=0)
output += text.charAt(k-1);
System.out.println("Diciphered Text : "+output);
}
}
Reply

4.
Suraj Parmar
October 1, 2015 11:13 am

using same logic i just have added taking input


from user using buffered reader
import java.io.*;
public class rail{

public static void main(String [] args)throws


IOException
{
BufferedReader in=new BufferedReader(new
InputStreamReader(System.in));
System.out.println(Enter The String: );
String input=in.readLine();
String output=;
int l=input.length();
System.out.println(You have Provided : +input);
for(int i=0;i<l;i+=2)
{
output +=input.charAt(i);
}
for(int i=1;i<l;i+=2)
{
output += input.charAt(i);
}
System.out.println("Ciphered Text : "+output);
}
}
Reply

5.
Vidhan Dhagai
July 5, 2016 3:45 am

Same Code With A Different Method


also inputs from user :
import java.io.*;
import java.util.*;
public class railfence {
public static void main(String args[])
{
String input = ;
String output = ;
System.out.println(Enter PlainText : );
Scanner ip = new Scanner(System.in);
input=ip.nextLine();
int len = input.length();
System.out.println(Input String : + input);
for(int i=0;i<len;i+=2)
{
output += input.charAt(i);
}

for(int i=1;i<len;i+=2)
{
output += input.charAt(i);
}
System.out.println("Ciphered Text : "+output);
String dcr1="";
String dcr2="";
int c=len/2;
if(len%2==0)
{
for(int i=0;i<c;i++)
{
dcr1 += output.charAt(i);
dcr2 += output.charAt(c+i);
}
String dcr="";
for(int i=0;i<c;i++)
{
dcr+=dcr1.charAt(i);
dcr+=dcr2.charAt(i);
}
System.out.println(dcr);
}

else
{
dcr1=output.substring(1,(c+1));
dcr2=output.substring((c+1),len);
//System.out.println(dcr1);
//System.out.println(dcr2);
String dcr="";
for(int i=0;i<c;i++)
{
dcr+=dcr2.charAt(i);
dcr+=dcr1.charAt(i);
}
System.out.print(output.charAt(0)+dcr);
//System.out.print(dcr);
}
}
}

You might also like