Vigenere Cipher in Java
/* Dalibor Labudovic */
/* Not 100% stable but work ok*/
package cs3550;
import java.util.Scanner;
public class vigenerecipher {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.println("Encyption...");
System.out.println("Enter Pass Phrase: ");
String phrase = input.nextLine();
System.out.println("Enter plain Text: ");
String plain = input.nextLine();
System.out.print("Here is the encryption: ");
String enc = encrypt(plain, phrase);
System.out.println(enc);
System.out.println("Decrpytion...");
System.out.println("Decoding " + enc);
System.out.println("Enter pass phrase: ");
String dePhrase = input.nextLine();
System.out.println("Decryption : " + decrypt(enc, dePhrase));
}
static String encrypt(String text, final String key)
{
if(text.equalsIgnoreCase(""))
{
System.out.println("Error no text ");
}
String s= text;
String s1= key;
char key1[] = new char[s1.length()];
for(int i=0;i<s1.length();i++)
{
key1[i]=s1.charAt(i);
}
int k[]=new int[key1.length];
char c[]=new char[s.length()];
int cc[]=new int [s.length()];
//int r[]=new int[s.length()];
int ss[]=new int[s.length()];
char result[]=new char[s.length()];
for(int i=0;i<s.length();i++)
{
c[i]=s.charAt(i);
cc[i]=(int)c[i]-97;
}
for(int i=0;i<key1.length;i++)
{
k[i]=(int)key1[i]-97;
}
String g;
for(int i=0;i<s.length();i++)
{
ss[i]=(k[i%s1.length()]+cc[i])%26;
result[i]=(char)(ss[i]+97);
}
g = String.copyValueOf(result);
return g;
}
static String decrypt(String text, final String key)
{
if(text.equalsIgnoreCase(""))
{
System.out.println("There Are No Code To Be Decrypt...!");
}
String s=text;
String s1=key;
char h[]=new char[s.length()];
int hh[]=new int[s.length()];
for(int i=0;i<s.length();i++)
{
h[i]=s.charAt(i);
hh[i]=h[i];
hh[i]=(int)hh[i]-97;
}
char k[]=new char[s1.length()];
int kk[]=new int[s1.length()];
for(int i=0;i<s1.length();i++)
{
k[i]=s1.charAt(i);
kk[i]=k[i];
kk[i]=(int)kk[i]-97;
}
int re1[]=new int[s.length()];
char ref[]=new char[s.length()];
for(int i=0;i<s.length();i++)
{
re1[i]=hh[i]-kk[i%s1.length()];
}
String g;
for(int i=0;i<s.length();i++)
{
if(re1[i]<0)
{
re1[i]=re1[i]+26;
}
ref[i]=(char) (re1[i]+97);
}
g = String.copyValueOf(ref);
return g;
}
}
/* Not 100% stable but work ok*/
package cs3550;
import java.util.Scanner;
public class vigenerecipher {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.println("Encyption...");
System.out.println("Enter Pass Phrase: ");
String phrase = input.nextLine();
System.out.println("Enter plain Text: ");
String plain = input.nextLine();
System.out.print("Here is the encryption: ");
String enc = encrypt(plain, phrase);
System.out.println(enc);
System.out.println("Decrpytion...");
System.out.println("Decoding " + enc);
System.out.println("Enter pass phrase: ");
String dePhrase = input.nextLine();
System.out.println("Decryption : " + decrypt(enc, dePhrase));
}
static String encrypt(String text, final String key)
{
if(text.equalsIgnoreCase(""))
{
System.out.println("Error no text ");
}
String s= text;
String s1= key;
char key1[] = new char[s1.length()];
for(int i=0;i<s1.length();i++)
{
key1[i]=s1.charAt(i);
}
int k[]=new int[key1.length];
char c[]=new char[s.length()];
int cc[]=new int [s.length()];
//int r[]=new int[s.length()];
int ss[]=new int[s.length()];
char result[]=new char[s.length()];
for(int i=0;i<s.length();i++)
{
c[i]=s.charAt(i);
cc[i]=(int)c[i]-97;
}
for(int i=0;i<key1.length;i++)
{
k[i]=(int)key1[i]-97;
}
String g;
for(int i=0;i<s.length();i++)
{
ss[i]=(k[i%s1.length()]+cc[i])%26;
result[i]=(char)(ss[i]+97);
}
g = String.copyValueOf(result);
return g;
}
static String decrypt(String text, final String key)
{
if(text.equalsIgnoreCase(""))
{
System.out.println("There Are No Code To Be Decrypt...!");
}
String s=text;
String s1=key;
char h[]=new char[s.length()];
int hh[]=new int[s.length()];
for(int i=0;i<s.length();i++)
{
h[i]=s.charAt(i);
hh[i]=h[i];
hh[i]=(int)hh[i]-97;
}
char k[]=new char[s1.length()];
int kk[]=new int[s1.length()];
for(int i=0;i<s1.length();i++)
{
k[i]=s1.charAt(i);
kk[i]=k[i];
kk[i]=(int)kk[i]-97;
}
int re1[]=new int[s.length()];
char ref[]=new char[s.length()];
for(int i=0;i<s.length();i++)
{
re1[i]=hh[i]-kk[i%s1.length()];
}
String g;
for(int i=0;i<s.length();i++)
{
if(re1[i]<0)
{
re1[i]=re1[i]+26;
}
ref[i]=(char) (re1[i]+97);
}
g = String.copyValueOf(ref);
return g;
}
}
Comments
Post a Comment