package algorithms;
import javax.swing.JOptionPane;
public class Encryptor {
private static String[] vals = {" ","!", "\"","#","$","%","&","'","(",")","*","+",",","-",".","/","0","1","2","3","4","5","6","7","8","9"
,":",";","<","=",">","?","@","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","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","{","}","|"
,"}","~"};
private static int max = vals.length;
public Encryptor(){}
//takes a String a makes it into an array containing every character
private static String[] toArray(String enc)
{
String[] arr = new String[enc.length()];
for(int i = 0; i < enc.length(); i++)
{
arr[i]=enc.substring(i,i+1);
}
return arr;
}
//assigns number values to each character
private static int[] numValues(String[] enc)
{
int[] numvals = new int[enc.length];
for(int i = 0; i < enc.length;i++)
{
for(int j = 0; j < max; j++)
{
if(enc[i].equals(vals[j]))
{
numvals[i] = j;
System.out.println("numValues(): " + i+": " + j);
break;
}
}
}
System.out.println("numValues(): " + numvals.length);
return numvals;
}
//shifts each character by the specified increment
public static String hardEncrypt(String enc, int inc, int times)
{
String fin = "";//variable for final encrypted String
int chng = enc.length()*enc.length();//pattern breaker and makes the substitution much less obvious
int[] nums = numValues(toArray(enc));//integer array for the characters in the String to be encrypted
for(int amount = 0; amount < times; amount++)//encryption and re-encryption loop
{
fin = "";//resets fin
chng = enc.length()*enc.length();//resets the breaker
for(int i = 0; i < enc.length(); i++)//encryption algorithm loop
{
int ind = (nums[i] + inc)*chng;//the index in vals[] containing the correct character
if(ind < 0)//makes sure the number is positive
{
ind *= -1;
}
if(ind >= max)//makes sure the index is in bounds
{
while(ind >= max)
ind -= max;
}
fin += vals[ind];//adds the character at vals[ind] to fin
chng = Math.round(chng*1/2);//algorithmically changes the breaker integer
if(chng == 1)//resets the breaker if it equals one to enc.length^2*i
{
chng = enc.length()*enc.length() * i;
}
}
nums = numValues(toArray(fin));//resets the nums array
}
return fin;//returns the encrypted String
}
//decryption method based on hardEncrypt()
/*
* Problem Code!! vvvvvvvvv
*/
public static String keyDecrypt(String enc, int key, int times)
{
String fin = "";//String for final decrypted String
int chng = enc.length()*enc.length();//copies the algorithm and attempts to reverse it
System.out.println("keyDecrypt(): " + enc);
System.out.println("keyDecrypt(): " + chng);
int[] nums = numValues(toArray(enc));//array getting the exception
for(int amount = 0; amount < times; amount++)
{
fin = "";
chng = enc.length()*enc.length();
for(int i = 0; i < enc.length(); i++)
{
int ind = (nums[i] - key)*chng;//getting an ArrayIndexOutOfBoundsException and I can't figure out the cause
if(ind < 0)
{
ind += max;
}
if(ind >= max)
{
while(ind >= max)
ind -= max;
}
fin += vals[ind];
nums = numValues(toArray(fin));
chng = Math.round(chng*1/2);
if(chng == 1)
{
chng = enc.length()*enc.length() * i;
}
}
}
return fin;
}
/*
* Problem Code!! ^^^^^^^^^
*/
public static String easyEncrypt(String enc)
{
return hardEncrypt(enc, 3,1);
}
public static String decryptEasyEncrypt(String dec)
{
return hardEncrypt(dec, -3,1);
}
}