Upload
jay-shah
View
16
Download
3
Embed Size (px)
DESCRIPTION
Mono Alpha algorithm implemented in java
Citation preview
VGEC/CE/SEM6/IS/160702/PRA-2 2012
Aim:Write a program to implement Mono-alphabetic Cipher Encryption - Decryption
import java.io.*;
class monoalpha
{
static int m;
static String k;
static String s2="";
static String s="";
public static void main(String a[]) throws java.io.IOException
{
int y=0,flag=0,i,j,len;
char choice;
char k1[]=new char[26];
char al1[]=new char[26];
char al2[]=new char[26];
String source_file=a[1];
String Intermediate_file=a[2];
String destination_file=a[3];
boolean ans=false;
for( i=65;i<=90;i++,y++)
{
al1[y]=(char)i;
}
y=0;
for( i=97;i<=122;i++,y++)
{
ENROLLMENT NO:090170107009 Page
VGEC/CE/SEM6/IS/160702/PRA-2 2012
al2[y]=(char)i;
}
System.out.println("This program implements a letter-substitution cipher");
System.out.println("Your entered key is:");
k=a[0];
System.out.println(k);
len=k.length();
if(len!=26)
{
System.out.println("Your entered key is invalid:");
System.out.println("Re-run your program with valid key");
System.exit(0);
}
k1=k.toCharArray();
for( i=0;i<25;i++)
{
for( j=i+1;j<=25;j++)
{
if(k1[j]==k1[i])
flag=1;
ENROLLMENT NO:090170107009 Page
VGEC/CE/SEM6/IS/160702/PRA-2 2012
if (flag==1)
break;
}
if (flag==1)
break;
}
if(flag==1)
{
System.out.println("Your entered key is invalid:");
System.out.println("Re-run your program with valid key");
System.exit(0);
}
if(flag==0)
{
ans=true;
System.out.println("Your entered key is a valid key:");
}
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);
ENROLLMENT NO:090170107009 Page
VGEC/CE/SEM6/IS/160702/PRA-2 2012
while(( i=f.read())!=-1)
{
if(i>=97&&i<=122)
{
for( j=0;j<26;j++)
{
if(al2[j]==((char)i))
{
m=j;
break;
}
}
s2+=k1[m];
}
else if (i>=65&&i<91)
{
for( j=0;j<26;j++)
{
if(al1[j]==((char)i))
{
m=j;
break;
}
}
ENROLLMENT NO:090170107009 Page
VGEC/CE/SEM6/IS/160702/PRA-2 2012
s2+=Character.toUpperCase(k1[m]);
}
else
s2+=" ";
}
f.close();
char buf[]=s2.toCharArray();
FileWriter fw=new FileWriter(Intermediate_file);
fw.write(buf);
fw.close();
break;
//Case2 is for deciphering process
case '2':
FileReader f2=new FileReader(Intermediate_file);
while(( i=f2.read())!=-1)
{
if(i>=97&&i<=123)
{
for( j=0;j<26;j++)
{
if((char)i==k1[j])
{
m=j;
break;
}
ENROLLMENT NO:090170107009 Page
VGEC/CE/SEM6/IS/160702/PRA-2 2012
}
s+=al2[m];
}
else if(i>=65&&i<=90)
{
for( j=0;j<26;j++)
{
if(Character.toLowerCase((char)i)==k1[j])
{
m=j;
break;
}
}
s+=al1[m];
}
else
s+=" ";
}
f2.close();
char buf1[]=s.toCharArray();
FileWriter fw1=new FileWriter(destination_file);
fw1.write(buf1);
fw1.close();
break;
}
ENROLLMENT NO:090170107009 Page
VGEC/CE/SEM6/IS/160702/PRA-2 2012
}
}
Source File:Plaintext Intermediate File:Ciphertext Destination file:PlainText
ENROLLMENT NO:090170107009 Page
VGEC/CE/SEM6/IS/160702/PRA-2 2012
ENROLLMENT NO:090170107009 Page