BlockChain got a reaction from
wasab in The under 100 line challenge!
Here is my code
A small bifid cipher encoder/decoder
if it doesn't work fr some reason plsss reply or comment or whatever u r supposed to do
[code]
import java.io.*;
public class Bifid_cypher {
public static void main(String[] args) throws IOException {
InputStreamReader red = (new InputStreamReader(System.in));
BufferedReader in = new BufferedReader(red);
System.out.println("Enter 1 to encrypt");
System.out.println("Enter 2 to decrypt");
int opt = Integer.parseInt(in.readLine());
if (opt == 1)
{
char a[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
char c[][] = new char[5][5];
char c1[] = new char[25];
int x = 0;
while (x <25)
{
int r= (int)(Math.random()*(25));//RANDOMISATION CODE
char random=a[r];
boolean b = true;
for (int j = 0; j < x; j++)//FILTERING MATCHING ELEMENTS AND FILLING
{
if(random==c1[j])
{
b = false;
break;
}
}
if (b==true)
{
c1[x]=random;
x += 1;
}
}
for(int j=0;j<25;j++)//1D TO 2D CONVERSION
{
if(j%5==0)
{
System.out.print("\n");
}
c[((j)/5)][((j)%5)]=c1[j];
System.out.print(c1[j]);
}
System.out.println("\n"+"enter the message");//ASK FOR THE INPUT
String s=in.readLine();
int e1[]=new int[s.length()*2];
for(int i=0;i<s.length();i++)//ENCRYPTION STAGE
{
for(int j=0;j<5;j++)
{
for(int k=0;k<5;k++)
{
if(s.charAt(i)=='J')
{
if('I'==c[j][k])
{
e1[i]=j;
e1[i+s.length()]=k;
}
}
else if(s.charAt(i)==c[j][k])
{
e1[i]=j;
e1[i+s.length()]=k;
}
}
}
}
String output="";
for(int i=0;i<s.length();i++)
{
output+=c[e1[i*2]][e1[i*2+1]];
}
System.out.println("\n"+"the encrypted code");
System.out.print(output);
}
if(opt==2)
{
System.out.println("enter the encryption key");
String s2[]=new String[5];
for(int i=0;i<5;i++)
s2[i]=in.readLine();
System.out.println("enter the message");
String msg=in.readLine();
int e2[]=new int[msg.length()*2];
for(int i=0;i<msg.length();i++)
{
for (int j = 0; j < 5; j++)
{
for (int k = 0; k < 5; k++)
{
if(msg.charAt(i)==s2[j].charAt(k))
{
e2[i*2]=j;
e2[i*2+1]=k;
}
}
}
}
System.out.println();
String output="";
for(int i=0;i<msg.length();i++)
{
output+=s2[e2[i]].charAt(e2[i+msg.length()]);
}
System.out.println("\n"+"the decrypted code");
System.out.print(output);
}
}
}
[ /code]