Cryptography : Railfence Cipher
Railfence Cipher
Sample java code for Railfence Cipher
Key = 2
Encryption
Encryption
//key=2
import java.util.Scanner;
public class Railfence_encry{
public static void main(String args[]){
Scanner myscanner =new Scanner(System.in);
System.out.println("Enter the plaintext");
String word=myscanner.nextLine().replace(" ","");
System.out.println("cipher text");
for(int i=0;i<word.length();i++){
char a=word.charAt(i);
if(i%2==0){
System.out.print(a);
}
}
for(int i=0;i<word.length();i++){
char a=word.charAt(i);
if(i%2==1){
System.out.print(a);
}
}
System.out.println();
}
}
import java.util.Scanner;
public class Railfence_encry{
public static void main(String args[]){
Scanner myscanner =new Scanner(System.in);
System.out.println("Enter the plaintext");
String word=myscanner.nextLine().replace(" ","");
System.out.println("cipher text");
for(int i=0;i<word.length();i++){
char a=word.charAt(i);
if(i%2==0){
System.out.print(a);
}
}
for(int i=0;i<word.length();i++){
char a=word.charAt(i);
if(i%2==1){
System.out.print(a);
}
}
System.out.println();
}
}
output of the above code |
Decryption
//key=2
import java.util.*;
public class Railfence_decry{
public static void main(String args[]){
Scanner myscanner =new Scanner(System.in);
System.out.println("Enter the cipher text");
String word=myscanner.nextLine().replace(" ","");
System.out.println("original message");
char[] array1;
char[] array2;
array1=new char[word.length()];
array2=new char[word.length()];
if(word.length()%2==1){
for(int i=0;i<=(word.length()-1)/2;i++){
array1[i]=word.charAt(i);
}
int j=0;
for(int i=(word.length()-1)/2+1;i<=word.length()-1;i++){
array2[j]=word.charAt(i);
j++;
}
for(int i=0;i<word.length();i++){
if(i<=(word.length()+1)/2){
System.out.print(array1[i]);
}
if(i<=(word.length()-1)/2){
System.out.print(array2[i]);
}
}
}
else{
for(int i=0;i<=(word.length()/2)-1;i++){
array1[i]=word.charAt(i);
}
int j=0;
for(int i=word.length()/2;i<=word.length()-1;i++){
array2[j]=word.charAt(i);
j++;
}
for(int i=0;i<word.length();i++){
if(i<=word.length()/2){
System.out.print(array1[i]);
}
if(i<=word.length()/2){
System.out.print(array2[i]);
}
}
}
System.out.println();
}
}
import java.util.*;
public class Railfence_decry{
public static void main(String args[]){
Scanner myscanner =new Scanner(System.in);
System.out.println("Enter the cipher text");
String word=myscanner.nextLine().replace(" ","");
System.out.println("original message");
char[] array1;
char[] array2;
array1=new char[word.length()];
array2=new char[word.length()];
if(word.length()%2==1){
for(int i=0;i<=(word.length()-1)/2;i++){
array1[i]=word.charAt(i);
}
int j=0;
for(int i=(word.length()-1)/2+1;i<=word.length()-1;i++){
array2[j]=word.charAt(i);
j++;
}
for(int i=0;i<word.length();i++){
if(i<=(word.length()+1)/2){
System.out.print(array1[i]);
}
if(i<=(word.length()-1)/2){
System.out.print(array2[i]);
}
}
}
else{
for(int i=0;i<=(word.length()/2)-1;i++){
array1[i]=word.charAt(i);
}
int j=0;
for(int i=word.length()/2;i<=word.length()-1;i++){
array2[j]=word.charAt(i);
j++;
}
for(int i=0;i<word.length();i++){
if(i<=word.length()/2){
System.out.print(array1[i]);
}
if(i<=word.length()/2){
System.out.print(array2[i]);
}
}
}
System.out.println();
}
}
0 comments:
Post a Comment