//file name CeaserCipher.java
import java.util.*;
class basic{
String allChar="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int indexOfChar(char c)
{
for(int i=0;i< allChar.length();i++)
{
if(allChar.charAt(i)==c)
return i;
}
return -1;
}
char charAtIndex(int pos)
{
return allChar.charAt(pos);
}
}
class Ceaser{
basic b=new basic();
String Encrypt(String plainText,int key)
{
plainText=plainText.toUpperCase();
String cipherText="";
for(int i=0;i< plainText.length();i++)
{
int index=b.indexOfChar(plainText.charAt(i));
if(index==-1)
{
cipherText+=plainText.charAt(i);
continue;
}
if((index+key)%26==0)
{
cipherText+=b.charAtIndex(index+key);
}
else
{
cipherText+=b.charAtIndex((index+key)%26);
}
}
return cipherText;
}
String Decrypt(String cipherText,int key)
{
cipherText=cipherText.toUpperCase();
String decryptedText="";
for(int i=0;i< cipherText.length();i++)
{
int index=b.indexOfChar(cipherText.charAt(i));
if(index==-1)
{
decryptedText+=cipherText.charAt(i);
continue;
}
if((index-key)>=0)
{
decryptedText+=b.charAtIndex(index-key);
}
else
{
decryptedText+=b.charAtIndex((index-key)+26);
}
}
return decryptedText;
}
void bruteForce(String cipherText)
{
cipherText=cipherText.toUpperCase();
for(int k=0;k< 26;k++)
{
String decryptedText="";
int key=k;
for(int i=0;i< cipherText.length();i++)
{
int index=b.indexOfChar(cipherText.charAt(i));
if(index==-1)
{
decryptedText+=cipherText.charAt(i);
continue;
}
if((index-key)>=0)
{
decryptedText+=b.charAtIndex(index-key);
}
else
{
decryptedText+=b.charAtIndex((index-key)+26);
}
}
System.out.println("Decrypted Text Using key"+key+":"+decryptedText);
}
}
}
class CeaserCipher{
public static void main(String args[])throws Exception
{
Scanner scn=new Scanner(System.in);
String plainText,cipherText;
int key;
System.out.println("Enter Plaintext message:");
plainText=scn.nextLine();
System.out.println("Enter key for Encryption:");
key=scn.nextInt();
Ceaser cipher=new Ceaser();
cipherText=cipher.Encrypt(plainText,key);
System.out.println("Encrypted Ciphertext is:"+cipherText);
String decryptedText=cipher.Decrypt(cipherText,key);
System.out.println("Decrypted Ciphertext is:"+decryptedText);
System.out.println("Do you want to apply brute force on Ciphertext?press 1 otherwise press anykey");
int choice=scn.nextInt();
if(choice==1)
cipher.bruteForce(cipherText);
}
}
OUTPUT
Enter Plaintext message:
hithisisanandpatel
Enter key for Encryption:
4
Encrypted Ciphertext is:LMXLMWMWERERHTEXIP
Decrypted Ciphertext is:HITHISISANANDPATEL
Do you want to apply brute force on Ciphertext?press 1 otherwise press anykey
1
Decrypted Text Using key0:LMXLMWMWERERHTEXIP
Decrypted Text Using key1:KLWKLVLVDQDQGSDWHO
Decrypted Text Using key2:JKVJKUKUCPCPFRCVGN
Decrypted Text Using key3:IJUIJTJTBOBOEQBUFM
Decrypted Text Using key4:HITHISISANANDPATEL
Decrypted Text Using key5:GHSGHRHRZMZMCOZSDK
Decrypted Text Using key6:FGRFGQGQYLYLBNYRCJ
Decrypted Text Using key7:EFQEFPFPXKXKAMXQBI
Decrypted Text Using key8:DEPDEOEOWJWJZLWPAH
Decrypted Text Using key9:CDOCDNDNVIVIYKVOZG
Decrypted Text Using key10:BCNBCMCMUHUHXJUNYF
Decrypted Text Using key11:ABMABLBLTGTGWITMXE
Decrypted Text Using key12:ZALZAKAKSFSFVHSLWD
Decrypted Text Using key13:YZKYZJZJREREUGRKVC
Decrypted Text Using key14:XYJXYIYIQDQDTFQJUB
Decrypted Text Using key15:WXIWXHXHPCPCSEPITA
Decrypted Text Using key16:VWHVWGWGOBOBRDOHSZ
Decrypted Text Using key17:UVGUVFVFNANAQCNGRY
Decrypted Text Using key18:TUFTUEUEMZMZPBMFQX
Decrypted Text Using key19:STESTDTDLYLYOALEPW
Decrypted Text Using key20:RSDRSCSCKXKXNZKDOV
Decrypted Text Using key21:QRCQRBRBJWJWMYJCNU
Decrypted Text Using key22:PQBPQAQAIVIVLXIBMT
Decrypted Text Using key23:OPAOPZPZHUHUKWHALS
Decrypted Text Using key24:NOZNOYOYGTGTJVGZKR
Decrypted Text Using key25:MNYMNXNXFSFSIUFYJQ
import java.util.*;
class basic{
String allChar="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int indexOfChar(char c)
{
for(int i=0;i< allChar.length();i++)
{
if(allChar.charAt(i)==c)
return i;
}
return -1;
}
char charAtIndex(int pos)
{
return allChar.charAt(pos);
}
}
class Ceaser{
basic b=new basic();
String Encrypt(String plainText,int key)
{
plainText=plainText.toUpperCase();
String cipherText="";
for(int i=0;i< plainText.length();i++)
{
int index=b.indexOfChar(plainText.charAt(i));
if(index==-1)
{
cipherText+=plainText.charAt(i);
continue;
}
if((index+key)%26==0)
{
cipherText+=b.charAtIndex(index+key);
}
else
{
cipherText+=b.charAtIndex((index+key)%26);
}
}
return cipherText;
}
String Decrypt(String cipherText,int key)
{
cipherText=cipherText.toUpperCase();
String decryptedText="";
for(int i=0;i< cipherText.length();i++)
{
int index=b.indexOfChar(cipherText.charAt(i));
if(index==-1)
{
decryptedText+=cipherText.charAt(i);
continue;
}
if((index-key)>=0)
{
decryptedText+=b.charAtIndex(index-key);
}
else
{
decryptedText+=b.charAtIndex((index-key)+26);
}
}
return decryptedText;
}
void bruteForce(String cipherText)
{
cipherText=cipherText.toUpperCase();
for(int k=0;k< 26;k++)
{
String decryptedText="";
int key=k;
for(int i=0;i< cipherText.length();i++)
{
int index=b.indexOfChar(cipherText.charAt(i));
if(index==-1)
{
decryptedText+=cipherText.charAt(i);
continue;
}
if((index-key)>=0)
{
decryptedText+=b.charAtIndex(index-key);
}
else
{
decryptedText+=b.charAtIndex((index-key)+26);
}
}
System.out.println("Decrypted Text Using key"+key+":"+decryptedText);
}
}
}
class CeaserCipher{
public static void main(String args[])throws Exception
{
Scanner scn=new Scanner(System.in);
String plainText,cipherText;
int key;
System.out.println("Enter Plaintext message:");
plainText=scn.nextLine();
System.out.println("Enter key for Encryption:");
key=scn.nextInt();
Ceaser cipher=new Ceaser();
cipherText=cipher.Encrypt(plainText,key);
System.out.println("Encrypted Ciphertext is:"+cipherText);
String decryptedText=cipher.Decrypt(cipherText,key);
System.out.println("Decrypted Ciphertext is:"+decryptedText);
System.out.println("Do you want to apply brute force on Ciphertext?press 1 otherwise press anykey");
int choice=scn.nextInt();
if(choice==1)
cipher.bruteForce(cipherText);
}
}
OUTPUT
Enter Plaintext message:
hithisisanandpatel
Enter key for Encryption:
4
Encrypted Ciphertext is:LMXLMWMWERERHTEXIP
Decrypted Ciphertext is:HITHISISANANDPATEL
Do you want to apply brute force on Ciphertext?press 1 otherwise press anykey
1
Decrypted Text Using key0:LMXLMWMWERERHTEXIP
Decrypted Text Using key1:KLWKLVLVDQDQGSDWHO
Decrypted Text Using key2:JKVJKUKUCPCPFRCVGN
Decrypted Text Using key3:IJUIJTJTBOBOEQBUFM
Decrypted Text Using key4:HITHISISANANDPATEL
Decrypted Text Using key5:GHSGHRHRZMZMCOZSDK
Decrypted Text Using key6:FGRFGQGQYLYLBNYRCJ
Decrypted Text Using key7:EFQEFPFPXKXKAMXQBI
Decrypted Text Using key8:DEPDEOEOWJWJZLWPAH
Decrypted Text Using key9:CDOCDNDNVIVIYKVOZG
Decrypted Text Using key10:BCNBCMCMUHUHXJUNYF
Decrypted Text Using key11:ABMABLBLTGTGWITMXE
Decrypted Text Using key12:ZALZAKAKSFSFVHSLWD
Decrypted Text Using key13:YZKYZJZJREREUGRKVC
Decrypted Text Using key14:XYJXYIYIQDQDTFQJUB
Decrypted Text Using key15:WXIWXHXHPCPCSEPITA
Decrypted Text Using key16:VWHVWGWGOBOBRDOHSZ
Decrypted Text Using key17:UVGUVFVFNANAQCNGRY
Decrypted Text Using key18:TUFTUEUEMZMZPBMFQX
Decrypted Text Using key19:STESTDTDLYLYOALEPW
Decrypted Text Using key20:RSDRSCSCKXKXNZKDOV
Decrypted Text Using key21:QRCQRBRBJWJWMYJCNU
Decrypted Text Using key22:PQBPQAQAIVIVLXIBMT
Decrypted Text Using key23:OPAOPZPZHUHUKWHALS
Decrypted Text Using key24:NOZNOYOYGTGTJVGZKR
Decrypted Text Using key25:MNYMNXNXFSFSIUFYJQ