Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

---504/PRAC3:Simple Columnar Transposition Encryption technique---NetBeans IDE---(01/07/19)---

package p3;

import java.util.Scanner;

public class P3 {
public static void main(String[] args) {
String text;
int key1;
int key[]=new int[4];
Scanner sc=new Scanner(System.in);
System.out.println("Enter a message : ");
text=sc.nextLine();
char a[][]=new char[50][4];
int l = text.length();
int row;
if(l%4==0) {row=l/4;}
else {row=(l/4)+1;}
int k=0;
System.out.println("\nMatrix: ");
for (int i =0;i<row;i++)
{
for(int j=0;j<4;j++)
{
a[i][j]=text.charAt(k);
k++;
System.out.print(a[i][j]+" ");

if(l==k) {break;}
}
System.out.println(" \n");
}
String s="";
System.out.println("Enter a key: ");
for (int i=0;i<4;i++)
{
key[i]=sc.nextInt();
}
for (int i=0;i<4;i++)
{
key1=key[i];
for(int j=0;j<row;j++)
{
String c=a[j][key1]+" ";
if(c!="\0") {s=s+c;}
}
}
System.out.println("Cipher text: " +s);
}
}
-------------OUTPUT--------------
run:
Enter a message :
helloworld

Matrix:
he l l

owor

l d

Enter a key:
3
1
0
2
Cipher text: l r e w d h o l l o
---504/PRAC4:Railfence Encryption technique---NetBeans IDE---(08/07/19)---
package p4;
import java.util.Scanner;
public class P4 {
public static void main(String[] args) {
String rf;
String s="";
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string: ");
rf=sc.nextLine();
int i;

System.out.println("\n---\nFirst half: ");


for(i=0;i<rf.length();i=i+2){
char c=rf.charAt(i);
s=s+c;
System.out.print(c);
}

System.out.println("\nSecond half: ");


for(int j=1;j<rf.length();j=j+2){
char c=rf.charAt(j);
s=s+c;
System.out.print(c);
}
System.out.println("\n---\nCipher text: "+s);
}
}
-------------OUTPUT--------------
run:
Enter a string:
workhard

---
First half:
wrhr
Second half:
okad
---
Cipher text: wrhrokad
BUILD SUCCESSFUL (total time: 5 seconds)

You might also like