Skip to content

Instantly share code, notes, and snippets.

@Abdillah
Created March 7, 2017 21:48
Show Gist options
  • Save Abdillah/f0604d93a48323bf8ea162aecf47e853 to your computer and use it in GitHub Desktop.
Save Abdillah/f0604d93a48323bf8ea162aecf47e853 to your computer and use it in GitHub Desktop.
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
String input = "aaaaaaaa";
String codes = "slyckies";
StringBuilder result = new StringBuilder();
char[] codeList = codes.toCharArray();
char[] keyList = input.toCharArray();
int maxCount = input.length();
System.out.println("The length is "+maxCount);
int i = 0;
for (Character code : codeList) {
int key = Character.getNumericValue(keyList[i]);
System.out.println("Process input " + keyList[i] + " (" + key + ") with salt " + code + "(" + ((int) code) + ")");
if (key % 2 == 0) {
int res = code+key;
System.out.println("Output " + ((int) code) + " + " + key + " = " + res);
result.append((char)res);
} else {
int res = code-key;
System.out.println("Output " + ((int) code) + " + " + key + " = " + res);
result.append((char)res);
}
i++;
if(i>maxCount) {
i = 0;
}
}
System.out.println("The result is "+result.toString());
// Input is the encrypted result
String encryptedInput = result.toString();
keyList = encryptedInput.toCharArray();
System.out.println("The length is "+keyList.length());
result = new StringBuilder();
i = 0;
for (Character code : codeList) {
int key = Character.getNumericValue(keyList[i]);
// Create temporary value, to determine it's modulus
// Either subtract or add, no problem
// Due to (a + a) % 2 or (a - a) % 2 always 0
int temp = code + key;
if (temp % 2 == 0) {
// Switch to substraction
int res = code-key;
result.append((char)res);
} else {
// Switch to addition
int res = code+key;
result.append((char)res);
}
i++;
if (i>maxCount) {
i = 0;
}
}
System.out.println("The decryp is "+result.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment