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(){}
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;
}
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;
break;
}
}
}
return numvals;
}
//shifts each character by the specified increment
public static String hardEncrypt(String enc, int inc, int times)
{
String fin = "";
int[] nums = numValues(toArray(enc));
for(int amount = 0; amount < times; amount++)
{
fin = "";
for(int i = 0; i < enc.length(); i++)
{
int ind = nums[i] + inc;
if(ind < 0)
{
ind *= -1;
}
if(ind >= max)
{
while(ind >= max)
ind -= max;
}
fin += vals[ind];
}
nums = numValues(toArray(fin));
}
return fin;
}
public static String keyDecrypt(String enc, int key, int times)
{
String fin = "";
int[] nums = numValues(toArray(enc));
for(int amount = 0; amount < times; amount++)
{
fin = "";
for(int i = 0; i < enc.length(); i++)
{
int ind = nums[i] - key;
if(ind < 0)
{
ind += max;
}
if(ind >= max)
{
while(ind >= max)
ind -= max;
}
fin += vals[ind];
}
nums = numValues(toArray(fin));
}
return fin;
}
public static String easyEncrypt(String enc)
{
return hardEncrypt(enc, 3,1);
}
public static String decryptEasyEncrypt(String dec)
{
return hardEncrypt(dec, -3,1);
}
public static void main(String[] args)
{
boolean done = false;
while(!done)
{
String toBe = "";
toBe = JOptionPane.showInputDialog("What would you like to be encrypted?");
try{
if(toBe.equals(null)){}
}catch(NullPointerException e)
{
int i = JOptionPane.showConfirmDialog(null, "Are you sure?");
if(i == 0)
System.exit(0);
else{
toBe = JOptionPane.showInputDialog("What would you like to be encrypted?");
}
}
String owMuch = "";
int howMuch = 0;
owMuch = (JOptionPane.showInputDialog("How much would you like it encrypted? (any number)"));
try{
if(owMuch.equals(null)){}
}catch(NullPointerException e)
{
int i = JOptionPane.showConfirmDialog(null, "Are you sure?");
if(i == 0)
System.exit(0);
else{
owMuch = (JOptionPane.showInputDialog("How much would you like it encrypted? (any number)"));
}
}
try{
howMuch = Integer.parseInt(owMuch);
}catch(NumberFormatException e){
JOptionPane.showMessageDialog(null, "Argument must be an integer. Encryption key set to 71.");
howMuch = 71;
}
String owMany = "";
int howMany = 1;
owMany = JOptionPane.showInputDialog("How many times do you want it encrypted? (any number)");
try{
if(owMuch.equals(null)){}
}catch(NullPointerException e)
{
int i = JOptionPane.showConfirmDialog(null, "Are you sure?");
if(i == 0)
System.exit(0);
else{
owMany = (JOptionPane.showInputDialog("How many times do you want it encrypted? (any number)"));
}
}
try{
howMany = Integer.parseInt(owMuch);
}catch(NumberFormatException e){
JOptionPane.showMessageDialog(null, "Argument must be an integer. Encryption amount set to 1.");
}
long start = System.currentTimeMillis();
String enc = hardEncrypt(toBe, howMuch, howMany);
long end = System.currentTimeMillis();
JOptionPane.showMessageDialog(null, "\"" + toBe + "\" encrypted by " + howMuch + ", " + howMany + " times is: \n\n" + enc + "\n\nEncryption took " + (end-start) + "ms.");
int i = JOptionPane.showConfirmDialog(null, "Again?");
if(i != 0)
{
done = true;
}
}
}
}