Suppose a text is encrypted using the Vigenere cipher. The given Java program can be used to break the cipher and read the content.
*-----------------------*
import java.io.*;
public class decipher
{
public static void main(String args[])
{
String ctext="cipher_text_here";
ctext=ctext.toUpperCase();
String key="enter_key";
char[] alphabet = {’A’,’B’,’C’,’D’,’E’,’F’,’G’,’H’,’I’,’J’,’K’,’L’,’M’,’N’,’O’,
’P’,’Q’,’R’,’S’,’T’,’U’,’V’,’W’,’X’,’Y’,’Z’};
String plaintext = "";
int k=0;
for(int i=0; i < ctext.length(); i++)
{
int keypos = 0,ciphpos=0;
char c=ctext.charAt(i);
int value = (int) c;
if(value<65 value="">90)65>
{
System.out.print(c);
}
else
{
for(int j = 0; j<26 j="" p=""> {
if(key.charAt(k%key.length())==alphabet[j])
{k++;
keypos=j;
break;
}
}
for(int j = 0; j<26 j="" p=""> {
if(ctext.charAt(i)==alphabet[j])
{
ciphpos=j;
break;
}
}
System.out.print(alphabet[(ciphpos-keypos+26)%26]);
}
}
}
}
26>26>
import java.io.*;
public class decipher
{
public static void main(String args[])
{
String ctext="cipher_text_here";
ctext=ctext.toUpperCase();
String key="enter_key";
char[] alphabet = {’A’,’B’,’C’,’D’,’E’,’F’,’G’,’H’,’I’,’J’,’K’,’L’,’M’,’N’,’O’,
’P’,’Q’,’R’,’S’,’T’,’U’,’V’,’W’,’X’,’Y’,’Z’};
String plaintext = "";
int k=0;
for(int i=0; i < ctext.length(); i++)
{
int keypos = 0,ciphpos=0;
char c=ctext.charAt(i);
int value = (int) c;
if(value<65 value="">90)65>
{
System.out.print(c);
}
else
{
for(int j = 0; j<26 j="" p=""> {
if(key.charAt(k%key.length())==alphabet[j])
{k++;
keypos=j;
break;
}
}
for(int j = 0; j<26 j="" p=""> {
if(ctext.charAt(i)==alphabet[j])
{
ciphpos=j;
break;
}
}
System.out.print(alphabet[(ciphpos-keypos+26)%26]);
}
}
}
}
26>26>
Great Article
ReplyDelete