import java.nio.charset.Charset;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
/**
* This is a class which can be used to encrypt any sort of string,
* the class provides a simple encryption program which uses a scrambler/adder/replacer/swapper.
* The encrypted message is sent as an object which can then be casted
* into a String LinkedList or a String array which can then be formated and decrypted.
* If the message is infiltrated while traveling through a socket the thief
* wont be able to see the content of the message without the key or this class.
*
* @author Eudy Contreras
*
*/
public class Encrypter {
private static final char[] plain =
{'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','Ö','Ä','Å','0','1','2',
'3','4','5','6','7','8','9'};
private static final char[] key =
{'D','E','F','G','H','Ä','U','J',
'A','L','Z','Å','N','C','P','Q',
'W','S','T','Ö','I','V','R','X',
'Y','M','K','B','O','4','6','1',
'8','3','2','9','0','5','7'};
private static final char[] added =
{'L','E','Z','G','H','Ä','U','J',
'A','D','F','Å','N','C','M','Y',
'W','S','B','Ö','I','V','R','1',
'Q','P','K','T','O','4','6','X',
'8','3','2','9','0','5','7'};
private static final String[] bytePlaim =
{"1","2","3","4","5","6","7","8","9","0"};
private static final String[] byteKey =
{"8","0","5","4","9","2","1","3","7","6"};
private static final Map<Character, Character> plainMap;
static
{
plainMap = new HashMap<Character, Character>();
for(int i = 0; i<plain.length; i++){
plainMap.put(plain[i],key[i]);
}
}
private static final Map<Character, Character> keyMap;
static
{
keyMap = new HashMap<Character, Character>();
for(int i = 0; i<key.length; i++){
keyMap.put(key[i],plain[i]);
}
}
private static final Map<String, String> plainByteMap;
static
{
plainByteMap = new HashMap<String, String>();
for(int i = 0; i<bytePlaim.length; i++){
plainByteMap.put(bytePlaim[i],byteKey[i]);
}
}
private static final Map<String, String> keyByteMap;
static
{
keyByteMap = new HashMap<String, String>();
for(int i = 0; i<byteKey.length; i++){
keyByteMap.put(byteKey[i],bytePlaim[i]);
}
}
private static final int operationCount = 16;
/**
* This method will will used a simple key
* and encrypt a message
* @param message
* @return
*/
private static String obfuscate(String message) {
String caseCode = convertCase(message);
char[] caseBinary = new char[message.length()];
for(int i = 0; i < messagei<caseCode.length(); i++) {
if (Character.isUpperCase(messagecaseCode.charAt(i)) == 'A') {
caseBinary[i] = '1';
} else if (Character.isLowerCase(messagecaseCode.charAt(i)) == 'a') {
caseBinary[i] = '0';
}
}
char[] upperCaseMessage = new char[message.length()];
for(int i = 0; i<message.length(); i++) {
upperCaseMessage[i] = Character.toUpperCase(message.charAt(i));
}
char[] code = upperCaseMessage;
for (int i = 0; i < code.length; i++) {
if(plainMap.containsKey(code[i])){
code[i] = plainMap.get(code[i]);
}
else {code[i] = code[i];}
}
return String.valueOf(code)+"¤¤¤¤"+String.valueOf(caseBinary);
}
/**
* this method will decipherdecypher and encrypted string message
* @param message
* @return
*/
private static String DecypherEncryption(String message) {
String[] code_and_case = message.split("¤¤¤¤");
String codeMessage;
String caseMessage;
if(code_and_case.length == 2) {
codeMessage = code_and_case[0];
caseMessage = code_and_case[1];
}
else {
codeMessage = message;
caseMessage = null;
}
char[] code = codeMessage.toCharArray();
for (int i = 0; i < code.length; i++) {
if(keyMap.containsKey(code[i])){
code[i] = keyMap.get(code[i]);
}
else {code[i] = code[i];}
}
if (code_and_case.length == 2) {
for (int i = 0; i < caseMessage.length()-1; i++) {
if (caseMessage.charAt(i) == '1') {
code[i] = Character.toUpperCase(code[i]);
} else if (caseMessage.charAt(i) == '0') {
code[i] = Character.toLowerCase(code[i]);
}
}
}
return String.valueOf(code);
}
private static int getRandom(int value){
SecureRandom rand = new SecureRandom();
return rand.nextInt(value);
}
/**
* Further increases the encryption level of the message by adding random numbers and characters
* to the already encrypted message the process can then be reversed with an additional key.
* @param code: the encrypted message
* @return
*/
private static LinkedList<String> scrambler(String code) {
Random rand1 = new Random();
Random rand2 = new Random();
Random rand3 = new Random();
Random rand4 = new Random();
String encryptedCode = Encrypter.obfuscate(code);
String[] case_and_code = encryptedCode.split("¤¤¤¤");
String cypher = case_and_code[0];
String codeCase = case_and_code[1];
LinkedList<String> cypherList = new LinkedList<>();
char[] cypherCharArr = cypher.toCharArray();
for (int index = 0; index < cypherCharArr.length; index++){
cypherList.add(added[getRandomadded[rand3.nextInt(added.length)] + "" + added[getRandom]+""+added[rand2.nextInt(added.length)]
+ String]+String.valueOf(cypherCharArr[index]) + added[getRandomadded[rand4.nextInt(added.length)]
+ ]+(getRandomrand1.nextInt(99 - 10 + 110+1) + 10+10));
}
cypherList.addFirst("" + ""+(getRandomrand1.nextInt(999 - 100 ++1 1) + 100+100));
cypherList.add(codeCase + getRandomcodeCase+rand1.nextInt(10));
return swapper(cypherList);
}
/**
* This method will unscramble and rebuild a scrambled string
* @param cypherList
* @return
*/
private static String unScrambler(List<String>LinkedList<String> cypherList) {
StringBuilder stringBuilder = new StringBuilder();
List<String>LinkedList<String> cypherListOutput = new LinkedList<>();
String caseCode = cypherList.get(cypherList.size()-1);
for (int index = 0; index < cypherList.size()-1; index++) {
cypherListOutput.add(String.valueOf(cypherList.get(index).toCharArray()[2]));
if(index >= 1index>=1)
stringBuilder.append(cypherListOutput.get(index));
continue;
}
char[] rawCode = (stringBuilder.toString() + "¤¤¤¤" + caseCode).toCharArray();
char[] unscrambled = new char[rawCode.length - 1];
for (int i = 0; i < unscrambled.length; i++) {
unscrambled[i] = rawCode[i];
}
return String.valueOf(unscrambled);
}
char[] rawCode = (stringBuilder.toString()+"¤¤¤¤"+caseCode).toCharArray();
char[] unscrambled = new char[rawCode.length-1];
for(int i = 0; i<unscrambled.length; i++) {
unscrambled[i] = rawCode[i];
}
return String.valueOf(unscrambled);
}
/**
* Swaps the content of the array the as many times as
* the operation count.
* @param code
* @return
*/
private static LinkedList<String> swapper(LinkedList<String> code) {
LinkedList<String> output = new LinkedList<>();
LinkedList<char[]> chars = new LinkedList<>();
String[] swapArray = new String[code.size()];
for (int index = 0; index < code.size(); index++) {
swapArray[index] = code.get(index);
chars.add(swapArray[index].toCharArray());
}
for (int iteration = 0; iteration < operationCount; iteration++) {
for (int i = 1; i < chars.size() - 2; i++) {
for (int r = 0; r < chars.get(i).length - 1; r++) {
char tempChar = chars.get(i)[r];
chars.get(i)[r] = chars.get(i + 1)[r];
chars.get(i + 1)[r] = tempChar;
}
}
}
for (int i = 0; i < chars.size(); i++) {
output.add(String.valueOf(chars.get(i)));
}
return output;
}
/**
* unswapsunswapps the content of the array in order to make it
* ready to be deciphered
* @param code
* @return
*/
private static List<String>LinkedList<String> unswapper(List<String>LinkedList<String> code) {
List<String>LinkedList<String> output = new LinkedList<>();
List<char[]>LinkedList<char[]> chars = new LinkedList<>();
String[] swapArray = new String[code.size()];
for (int index = 0; index < code.size(); index++) {
swapArray[index] = code.get(index);
chars.add(swapArray[index].toCharArray());
}
for (int iteration = 0; iteration < operationCount; iteration++) {
for (int i = chars.size() - 3; i >= 1; i--) {
for (int r = chars.get(i).length - 2; r >= 0; r--) {
char tempChar = chars.get(i)[r];
chars.get(i)[r] = chars.get(i + 1)[r];
chars.get(i + 1)[r] = tempChar;
}
}
}
for (int i = 0; i < chars.size(); i++) {
output.add(String.valueOf(chars.get(i)));
}
return output;
}
private static String printBytes(byte[] binaryEncription){
return new String(binaryEncription, Charset.forName("UTF-8"));
}
private static List<String> fromStringToList(String list) {
return Arrays.asList(list.split(", "));
}
private static byte[] toPrimitives(Byte[] byteObject){
byte[] bytes = new byte[byteObject.length];
int i = 0;
for(Byte byteObjects : byteObject)bytes[i++] = byteObjects;
return bytes;
}
private static Byte[] toByteObject(byte[] bytesPrim) {
Byte[] bytes = new Byte[bytesPrim.length];
int i = 0;
for (byte bytePrimes : bytesPrim) bytes[i++] = bytePrimes;
return bytes;
}
/**
* ReplaceThis themethod firstwill digitsconvert ofa thestring byteinto tocase aholder
predefined digit! * which hold which character is upper case and which
* isn't.
* @param bytesmessage
* @return
*/
private static Byte[]String byteReplacerconvertCase(Byte[]String bytesmessage) {
Byte[]char[] tempBytescaseHolder = Arrays.copyOf(bytes,new byteschar[message.length();];
for(int i = 0; i < tempBytescaseHolder.length; i++) {
if (IntegerCharacter.toStringisUpperCase(tempBytes[i])message.charAt(0i) != '-')) {
String tempStringcaseHolder[i] = String.valueOf(tempBytes[i]);'A';
String} newValueelse =if plainByteMap.get(IntegerCharacter.toStringisLowerCase(tempBytes[i])message.substringcharAt(0, 1i))
+ tempString.substring(1); {
tempBytes[i]caseHolder[i] = Byte.valueOf(newValue);'a';
}
}
return tempBytes;String.valueOf(caseHolder);
}
/**
* PlacesThis backmethod will take a case code holder
* and apply the originalcase digitto whichthe wasgiven previouslymessage
replaced * @param caseBank
* @param bytesmessage
* @return
*/
private static Byte[]String bytePlaceBackcaseConverter(Byte[]String bytes)caseBank,String message){
Byte[]StringBuilder tempBytesinput = Arrays.copyOfnew StringBuilder(bytes,caseBank);
bytes.length StringBuilder output = new StringBuilder(message);
for (int iindex = 0; iindex < tempBytesinput.length;length(); i++index++) {
ifchar (Integer.toString(tempBytes[i]).charAt(0)current != '-'input.charAt(index) {;
Stringchar tempStringformatedString = Stringoutput.valueOfcharAt(tempBytes[i]index);
if (Character.isLowerCase(current)) {
String newValue = keyByteMap.get(Integer.toString(tempBytes[i]).substring(0, 1))
output.setCharAt(index, Character.toLowerCase(formatedString));
} +else tempString.substring(1);{
tempBytes[i] =output.setCharAt(index, ByteCharacter.valueOftoUpperCase(newValueformatedString));
}
}
return tempBytes;output.toString();
}
private static String printBytes(byte[] binaryEncription){
return new String(binaryEncription, Charset.forName("UTF-8"));
}
private static LinkedList<String> fromStringToList(String list){
String[] newList = list.split(", ");
LinkedList<String> code = new LinkedList<String>();
for(int i = 0; i<newList.length; i++){
code.add(newList[i]);
}
return code;
}
/**
* ReversesCan be used to further increase the bytelevel arrayof
for further obfuscation * encryption;
* @param encryption
* @return
*/
private static byte[] byteReverserbyteSwapper(Byte[]byte[] encryption){
Byte[] byteArray = byteReplacer(Arrays.copyOf(encryption, encryption.length));
// List<Byte>TODO: byteListwrite =code Arrays.asList(byteArray);here!!!
// Further swapping may not Collections.reverse(byteList);be needed!
return toPrimitives(byteArray);encryption;
}
/**
* ReversesTo unswapp the byte array to itsback originalto statenormal
* @param encryption
* @return
*/
private static byte[] byteSortterbyteUnswapper(Byte[]byte[] encryption){
Byte[] byteArray = encryption;
List<Byte> byteList = Arrays.asList(byteArray);
//TODO: write Collections.reverse(byteList);code here!
return toPrimitives(bytePlaceBack(byteArray));encryption;
}
public static byte[] encrypt(String message){
String cypher = scrambler(message).toString();
byte[] encryption = cypher.getBytes(Charset.forName("UTF-8"));
byte[] swappedBytes = byteReverser(toByteObjectbyteSwapper(encryption));
return swappedBytes;
}
public static String decrypt(byte[] encryption){
byte[] unswappedBytes = byteSortter(toByteObjectbyteUnswapper(encryption));
List<String>LinkedList<String> list = fromStringToList(new String(unswappedBytes, Charset.forName("UTF-8")));
List<String>LinkedList<String> unSwapped = Encrypter.unswapper(list);
String unScrambled = Encrypter.unScrambler(unSwapped);
String decyphered = Encrypter.DecypherEncryption(unScrambled);
return decyphered;
}
public static void main(String[] args) {
System.out.println(Encrypter.caseConverter("AaaaAA", "eudycr"));
System.out.println(Encrypter.convertCase("Whats up man, how are you doing today?"));
System.out.println(Encrypter.obfuscate("Whats up man, how are you doing today?"));
System.out.println(Encrypter.scrambler("Whats up man, how are you doing today?"));
System.out.println(Encrypter.unswapper(Encrypter.scrambler("Whats up man, how are you doing today?")));
System.out.println(Encrypter.unScrambler(Encrypter.unswapper(Encrypter.scrambler("Whats up man, how are you doing today?"))));
System.out.println("");
System.out.println(Encrypter.DecypherEncryption(Encrypter.unScrambler(Encrypter.unswapper(Encrypter.scrambler("Whats up man, how are you doing today?")))));
System.out.println(Encrypter.DecypherEncryption("HIGYFS"));
System.out.println("");
System.out.println("");
byte[] encryption = Encrypter.encrypt("Whats up man, how are you doing today?");
System.out.println("Actual encrypted message:");
System.out.println("");
byte[] encryption = Encrypter.encrypt("Whats up man, how are you doing today?");
System.out.println(printBytes(encryption));
System.out.println("");
System.out.println(Arrays.toString(encryption));
System.out.println(Encrypter.decrypt(encryption));
}
}
import java.nio.charset.Charset;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* This is a class which can be used to encrypt any sort of string,
* the class provides a simple encryption program which uses a scrambler/adder/replacer/swapper.
* The encrypted message is sent as an object which can then be casted
* into a String LinkedList or a String array which can then be formated and decrypted.
* If the message is infiltrated while traveling through a socket the thief
* wont be able to see the content of the message without the key or this class.
*
* @author Eudy Contreras
*
*/
public class Encrypter {
private static final char[] plain =
{'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','Ö','Ä','Å','0','1','2',
'3','4','5','6','7','8','9'};
private static final char[] key =
{'D','E','F','G','H','Ä','U','J',
'A','L','Z','Å','N','C','P','Q',
'W','S','T','Ö','I','V','R','X',
'Y','M','K','B','O','4','6','1',
'8','3','2','9','0','5','7'};
private static final char[] added =
{'L','E','Z','G','H','Ä','U','J',
'A','D','F','Å','N','C','M','Y',
'W','S','B','Ö','I','V','R','1',
'Q','P','K','T','O','4','6','X',
'8','3','2','9','0','5','7'};
private static final String[] bytePlaim =
{"1","2","3","4","5","6","7","8","9","0"};
private static final String[] byteKey =
{"8","0","5","4","9","2","1","3","7","6"};
private static final Map<Character, Character> plainMap;
static
{
plainMap = new HashMap<Character, Character>();
for(int i = 0; i<plain.length; i++){
plainMap.put(plain[i],key[i]);
}
}
private static final Map<Character, Character> keyMap;
static
{
keyMap = new HashMap<Character, Character>();
for(int i = 0; i<key.length; i++){
keyMap.put(key[i],plain[i]);
}
}
private static final Map<String, String> plainByteMap;
static
{
plainByteMap = new HashMap<String, String>();
for(int i = 0; i<bytePlaim.length; i++){
plainByteMap.put(bytePlaim[i],byteKey[i]);
}
}
private static final Map<String, String> keyByteMap;
static
{
keyByteMap = new HashMap<String, String>();
for(int i = 0; i<byteKey.length; i++){
keyByteMap.put(byteKey[i],bytePlaim[i]);
}
}
private static final int operationCount = 16;
/**
* This method will will used a simple key
* and encrypt a message
* @param message
* @return
*/
private static String obfuscate(String message) {
char[] caseBinary = new char[message.length()];
for(int i = 0; i < message.length(); i++) {
if (Character.isUpperCase(message.charAt(i))) {
caseBinary[i] = '1';
} else if (Character.isLowerCase(message.charAt(i))) {
caseBinary[i] = '0';
}
}
char[] upperCaseMessage = new char[message.length()];
for(int i = 0; i<message.length(); i++) {
upperCaseMessage[i] = Character.toUpperCase(message.charAt(i));
}
char[] code = upperCaseMessage;
for (int i = 0; i < code.length; i++) {
if(plainMap.containsKey(code[i])){
code[i] = plainMap.get(code[i]);
}
}
return String.valueOf(code)+"¤¤¤¤"+String.valueOf(caseBinary);
}
/**
* this method will decipher and encrypted string message
* @param message
* @return
*/
private static String DecypherEncryption(String message) {
String[] code_and_case = message.split("¤¤¤¤");
String codeMessage;
String caseMessage;
if(code_and_case.length == 2) {
codeMessage = code_and_case[0];
caseMessage = code_and_case[1];
}
else {
codeMessage = message;
caseMessage = null;
}
char[] code = codeMessage.toCharArray();
for (int i = 0; i < code.length; i++) {
if(keyMap.containsKey(code[i])){
code[i] = keyMap.get(code[i]);
}
}
if (code_and_case.length == 2) {
for (int i = 0; i < caseMessage.length()-1; i++) {
if (caseMessage.charAt(i) == '1') {
code[i] = Character.toUpperCase(code[i]);
} else if (caseMessage.charAt(i) == '0') {
code[i] = Character.toLowerCase(code[i]);
}
}
}
return String.valueOf(code);
}
private static int getRandom(int value){
SecureRandom rand = new SecureRandom();
return rand.nextInt(value);
}
/**
* Further increases the encryption level of the message by adding random numbers and characters
* to the already encrypted message the process can then be reversed with an additional key.
* @param code: the encrypted message
* @return
*/
private static LinkedList<String> scrambler(String code) {
String encryptedCode = Encrypter.obfuscate(code);
String[] case_and_code = encryptedCode.split("¤¤¤¤");
String cypher = case_and_code[0];
String codeCase = case_and_code[1];
LinkedList<String> cypherList = new LinkedList<>();
char[] cypherCharArr = cypher.toCharArray();
for (int index = 0; index < cypherCharArr.length; index++){
cypherList.add(added[getRandom(added.length)] + "" + added[getRandom(added.length)]
+ String.valueOf(cypherCharArr[index]) + added[getRandom(added.length)]
+ (getRandom(99 - 10 + 1) + 10));
}
cypherList.addFirst("" + (getRandom(999 - 100 + 1) + 100));
cypherList.add(codeCase + getRandom(10));
return swapper(cypherList);
}
/**
* This method will unscramble and rebuild a scrambled string
* @param cypherList
* @return
*/
private static String unScrambler(List<String> cypherList) {
StringBuilder stringBuilder = new StringBuilder();
List<String> cypherListOutput = new LinkedList<>();
String caseCode = cypherList.get(cypherList.size()-1);
for (int index = 0; index < cypherList.size()-1; index++) {
cypherListOutput.add(String.valueOf(cypherList.get(index).toCharArray()[2]));
if(index >= 1)
stringBuilder.append(cypherListOutput.get(index));
continue;
}
char[] rawCode = (stringBuilder.toString() + "¤¤¤¤" + caseCode).toCharArray();
char[] unscrambled = new char[rawCode.length - 1];
for (int i = 0; i < unscrambled.length; i++) {
unscrambled[i] = rawCode[i];
}
return String.valueOf(unscrambled);
}
/**
* Swaps the content of the array the as many times as
* the operation count.
* @param code
* @return
*/
private static LinkedList<String> swapper(LinkedList<String> code) {
LinkedList<String> output = new LinkedList<>();
LinkedList<char[]> chars = new LinkedList<>();
String[] swapArray = new String[code.size()];
for (int index = 0; index < code.size(); index++) {
swapArray[index] = code.get(index);
chars.add(swapArray[index].toCharArray());
}
for (int iteration = 0; iteration < operationCount; iteration++) {
for (int i = 1; i < chars.size() - 2; i++) {
for (int r = 0; r < chars.get(i).length - 1; r++) {
char tempChar = chars.get(i)[r];
chars.get(i)[r] = chars.get(i + 1)[r];
chars.get(i + 1)[r] = tempChar;
}
}
}
for (int i = 0; i < chars.size(); i++) {
output.add(String.valueOf(chars.get(i)));
}
return output;
}
/**
* unswaps the content of the array in order to make it
* ready to be deciphered
* @param code
* @return
*/
private static List<String> unswapper(List<String> code) {
List<String> output = new LinkedList<>();
List<char[]> chars = new LinkedList<>();
String[] swapArray = new String[code.size()];
for (int index = 0; index < code.size(); index++) {
swapArray[index] = code.get(index);
chars.add(swapArray[index].toCharArray());
}
for (int iteration = 0; iteration < operationCount; iteration++) {
for (int i = chars.size() - 3; i >= 1; i--) {
for (int r = chars.get(i).length - 2; r >= 0; r--) {
char tempChar = chars.get(i)[r];
chars.get(i)[r] = chars.get(i + 1)[r];
chars.get(i + 1)[r] = tempChar;
}
}
}
for (int i = 0; i < chars.size(); i++) {
output.add(String.valueOf(chars.get(i)));
}
return output;
}
private static String printBytes(byte[] binaryEncription){
return new String(binaryEncription, Charset.forName("UTF-8"));
}
private static List<String> fromStringToList(String list) {
return Arrays.asList(list.split(", "));
}
private static byte[] toPrimitives(Byte[] byteObject){
byte[] bytes = new byte[byteObject.length];
int i = 0;
for(Byte byteObjects : byteObject)bytes[i++] = byteObjects;
return bytes;
}
private static Byte[] toByteObject(byte[] bytesPrim) {
Byte[] bytes = new Byte[bytesPrim.length];
int i = 0;
for (byte bytePrimes : bytesPrim) bytes[i++] = bytePrimes;
return bytes;
}
/**
* Replace the first digits of the byte to a predefined digit!
* @param bytes
* @return
*/
private static Byte[] byteReplacer(Byte[] bytes) {
Byte[] tempBytes = Arrays.copyOf(bytes, bytes.length);
for(int i = 0; i < tempBytes.length; i++) {
if (Integer.toString(tempBytes[i]).charAt(0) != '-') {
String tempString = String.valueOf(tempBytes[i]);
String newValue = plainByteMap.get(Integer.toString(tempBytes[i]).substring(0, 1))
+ tempString.substring(1);
tempBytes[i] = Byte.valueOf(newValue);
}
}
return tempBytes;
}
/**
* Places back the original digit which was previously replaced
* @param bytes
* @return
*/
private static Byte[] bytePlaceBack(Byte[] bytes) {
Byte[] tempBytes = Arrays.copyOf(bytes, bytes.length);
for (int i = 0; i < tempBytes.length; i++) {
if (Integer.toString(tempBytes[i]).charAt(0) != '-') {
String tempString = String.valueOf(tempBytes[i]);
String newValue = keyByteMap.get(Integer.toString(tempBytes[i]).substring(0, 1))
+ tempString.substring(1);
tempBytes[i] = Byte.valueOf(newValue);
}
}
return tempBytes;
}
/**
* Reverses the byte array for further obfuscation
* @param encryption
* @return
*/
private static byte[] byteReverser(Byte[] encryption){
Byte[] byteArray = byteReplacer(Arrays.copyOf(encryption, encryption.length));
List<Byte> byteList = Arrays.asList(byteArray);
Collections.reverse(byteList);
return toPrimitives(byteArray);
}
/**
* Reverses the byte array to its original state
* @param encryption
* @return
*/
private static byte[] byteSortter(Byte[] encryption){
Byte[] byteArray = encryption;
List<Byte> byteList = Arrays.asList(byteArray);
Collections.reverse(byteList);
return toPrimitives(bytePlaceBack(byteArray));
}
public static byte[] encrypt(String message){
String cypher = scrambler(message).toString();
byte[] encryption = cypher.getBytes(Charset.forName("UTF-8"));
byte[] swappedBytes = byteReverser(toByteObject(encryption));
return swappedBytes;
}
public static String decrypt(byte[] encryption){
byte[] unswappedBytes = byteSortter(toByteObject(encryption));
List<String> list = fromStringToList(new String(unswappedBytes, Charset.forName("UTF-8")));
List<String> unSwapped = Encrypter.unswapper(list);
String unScrambled = Encrypter.unScrambler(unSwapped);
String decyphered = Encrypter.DecypherEncryption(unScrambled);
return decyphered;
}
public static void main(String[] args) {
System.out.println(Encrypter.obfuscate("Whats up man, how are you doing today?"));
System.out.println(Encrypter.scrambler("Whats up man, how are you doing today?"));
System.out.println(Encrypter.unswapper(Encrypter.scrambler("Whats up man, how are you doing today?")));
System.out.println(Encrypter.unScrambler(Encrypter.unswapper(Encrypter.scrambler("Whats up man, how are you doing today?"))));
System.out.println("");
System.out.println(Encrypter.DecypherEncryption(Encrypter.unScrambler(Encrypter.unswapper(Encrypter.scrambler("Whats up man, how are you doing today?")))));
System.out.println(Encrypter.DecypherEncryption("HIGYFS"));
System.out.println("");
System.out.println("");
byte[] encryption = Encrypter.encrypt("Whats up man, how are you doing today?");
System.out.println("Actual encrypted message:");
System.out.println("");
System.out.println(printBytes(encryption));
System.out.println("");
System.out.println(Arrays.toString(encryption));
System.out.println(Encrypter.decrypt(encryption));
}
}
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Random;
/**
* This is a class which can be used to encrypt any sort of string,
* the class provides a simple encryption program which uses a scrambler/adder/replacer/swapper.
* The encrypted message is sent as an object which can then be casted
* into a String LinkedList or a String array which can then be formated and decrypted.
* If the message is infiltrated while traveling through a socket the thief
* wont be able to see the content of the message without the key or this class.
*
* @author Eudy Contreras
*
*/
public class Encrypter {
private static final char[] plain =
{'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','Ö','Ä','Å','0','1','2',
'3','4','5','6','7','8','9'};
private static final char[] key =
{'D','E','F','G','H','Ä','U','J',
'A','L','Z','Å','N','C','P','Q',
'W','S','T','Ö','I','V','R','X',
'Y','M','K','B','O','4','6','1',
'8','3','2','9','0','5','7'};
private static final char[] added =
{'L','E','Z','G','H','Ä','U','J',
'A','D','F','Å','N','C','M','Y',
'W','S','B','Ö','I','V','R','1',
'Q','P','K','T','O','4','6','X',
'8','3','2','9','0','5','7'};
private static final Map<Character, Character> plainMap;
static
{
plainMap = new HashMap<Character, Character>();
for(int i = 0; i<plain.length; i++){
plainMap.put(plain[i],key[i]);
}
}
private static final Map<Character, Character> keyMap;
static
{
keyMap = new HashMap<Character, Character>();
for(int i = 0; i<key.length; i++){
keyMap.put(key[i],plain[i]);
}
}
private static final int operationCount = 16;
/**
* This method will will used a simple key
* and encrypt a message
* @param message
* @return
*/
private static String obfuscate(String message) {
String caseCode = convertCase(message);
char[] caseBinary = new char[message.length()];
for(int i = 0; i<caseCode.length(); i++) {
if(caseCode.charAt(i) == 'A') {
caseBinary[i] = '1';
} else if(caseCode.charAt(i) == 'a') {
caseBinary[i] = '0';
}
}
char[] upperCaseMessage = new char[message.length()];
for(int i = 0; i<message.length(); i++) {
upperCaseMessage[i] = Character.toUpperCase(message.charAt(i));
}
char[] code = upperCaseMessage;
for (int i = 0; i < code.length; i++) {
if(plainMap.containsKey(code[i])){
code[i] = plainMap.get(code[i]);
}
else {code[i] = code[i];}
}
return String.valueOf(code)+"¤¤¤¤"+String.valueOf(caseBinary);
}
/**
* this method will decypher and encrypted string message
* @param message
* @return
*/
private static String DecypherEncryption(String message) {
String[] code_and_case = message.split("¤¤¤¤");
String codeMessage;
String caseMessage;
if(code_and_case.length == 2) {
codeMessage = code_and_case[0];
caseMessage = code_and_case[1];
}
else {
codeMessage = message;
caseMessage = null;
}
char[] code = codeMessage.toCharArray();
for (int i = 0; i < code.length; i++) {
if(keyMap.containsKey(code[i])){
code[i] = keyMap.get(code[i]);
}
else {code[i] = code[i];}
}
if (code_and_case.length == 2) {
for (int i = 0; i < caseMessage.length()-1; i++) {
if (caseMessage.charAt(i) == '1') {
code[i] = Character.toUpperCase(code[i]);
} else if (caseMessage.charAt(i) == '0') {
code[i] = Character.toLowerCase(code[i]);
}
}
}
return String.valueOf(code);
}
/**
* Further increases the encryption level of the message by adding random numbers and characters
* to the already encrypted message the process can then be reversed with an additional key.
* @param code: the encrypted message
* @return
*/
private static LinkedList<String> scrambler(String code) {
Random rand1 = new Random();
Random rand2 = new Random();
Random rand3 = new Random();
Random rand4 = new Random();
String encryptedCode = Encrypter.obfuscate(code);
String[] case_and_code = encryptedCode.split("¤¤¤¤");
String cypher = case_and_code[0];
String codeCase = case_and_code[1];
LinkedList<String> cypherList = new LinkedList<>();
char[] cypherCharArr = cypher.toCharArray();
for (int index = 0; index < cypherCharArr.length; index++){
cypherList.add(added[rand3.nextInt(added.length)]+""+added[rand2.nextInt(added.length)]+String.valueOf(cypherCharArr[index]) + added[rand4.nextInt(added.length)]+(rand1.nextInt(99 - 10+1)+10));
}
cypherList.addFirst(""+(rand1.nextInt(999 - 100 +1 )+100));
cypherList.add(codeCase+rand1.nextInt(10));
return swapper(cypherList);
}
/**
* This method will unscramble and rebuild a scrambled string
* @param cypherList
* @return
*/
private static String unScrambler(LinkedList<String> cypherList) {
StringBuilder stringBuilder = new StringBuilder();
LinkedList<String> cypherListOutput = new LinkedList<>();
String caseCode = cypherList.get(cypherList.size()-1);
for (int index = 0; index < cypherList.size()-1; index++) {
cypherListOutput.add(String.valueOf(cypherList.get(index).toCharArray()[2]));
if(index>=1)
stringBuilder.append(cypherListOutput.get(index));
continue;
}
char[] rawCode = (stringBuilder.toString()+"¤¤¤¤"+caseCode).toCharArray();
char[] unscrambled = new char[rawCode.length-1];
for(int i = 0; i<unscrambled.length; i++) {
unscrambled[i] = rawCode[i];
}
return String.valueOf(unscrambled);
}
/**
* Swaps the content of the array the as many times as
* the operation count.
* @param code
* @return
*/
private static LinkedList<String> swapper(LinkedList<String> code) {
LinkedList<String> output = new LinkedList<>();
LinkedList<char[]> chars = new LinkedList<>();
String[] swapArray = new String[code.size()];
for (int index = 0; index < code.size(); index++) {
swapArray[index] = code.get(index);
chars.add(swapArray[index].toCharArray());
}
for (int iteration = 0; iteration < operationCount; iteration++) {
for (int i = 1; i < chars.size() - 2; i++) {
for (int r = 0; r < chars.get(i).length - 1; r++) {
char tempChar = chars.get(i)[r];
chars.get(i)[r] = chars.get(i + 1)[r];
chars.get(i + 1)[r] = tempChar;
}
}
}
for (int i = 0; i < chars.size(); i++) {
output.add(String.valueOf(chars.get(i)));
}
return output;
}
/**
* unswapps the content of the array in order to make it
* ready to be deciphered
* @param code
* @return
*/
private static LinkedList<String> unswapper(LinkedList<String> code) {
LinkedList<String> output = new LinkedList<>();
LinkedList<char[]> chars = new LinkedList<>();
String[] swapArray = new String[code.size()];
for (int index = 0; index < code.size(); index++) {
swapArray[index] = code.get(index);
chars.add(swapArray[index].toCharArray());
}
for (int iteration = 0; iteration < operationCount; iteration++) {
for (int i = chars.size() - 3; i >= 1; i--) {
for (int r = chars.get(i).length - 2; r >= 0; r--) {
char tempChar = chars.get(i)[r];
chars.get(i)[r] = chars.get(i + 1)[r];
chars.get(i + 1)[r] = tempChar;
}
}
}
for (int i = 0; i < chars.size(); i++) {
output.add(String.valueOf(chars.get(i)));
}
return output;
}
/**
* This method will convert a string into case holder
* which hold which character is upper case and which
* isn't.
* @param message
* @return
*/
private static String convertCase(String message) {
char[] caseHolder = new char[message.length()];
for(int i = 0; i < caseHolder.length; i++) {
if (Character.isUpperCase(message.charAt(i))) {
caseHolder[i] = 'A';
} else if (Character.isLowerCase(message.charAt(i))) {
caseHolder[i] = 'a';
}
}
return String.valueOf(caseHolder);
}
/**
* This method will take a case code holder
* and apply the case to the given message
* @param caseBank
* @param message
* @return
*/
private static String caseConverter(String caseBank,String message){
StringBuilder input = new StringBuilder(caseBank);
StringBuilder output = new StringBuilder(message);
for (int index = 0; index < input.length(); index++) {
char current = input.charAt(index);
char formatedString = output.charAt(index);
if (Character.isLowerCase(current)) {
output.setCharAt(index, Character.toLowerCase(formatedString));
} else {
output.setCharAt(index, Character.toUpperCase(formatedString));
}
}
return output.toString();
}
private static String printBytes(byte[] binaryEncription){
return new String(binaryEncription, Charset.forName("UTF-8"));
}
private static LinkedList<String> fromStringToList(String list){
String[] newList = list.split(", ");
LinkedList<String> code = new LinkedList<String>();
for(int i = 0; i<newList.length; i++){
code.add(newList[i]);
}
return code;
}
/**
* Can be used to further increase the level of
* encryption;
* @param encryption
* @return
*/
private static byte[] byteSwapper(byte[] encryption){
// TODO: write code here!!!
// Further swapping may not be needed!
return encryption;
}
/**
* To unswapp the byte array back to normal
* @param encryption
* @return
*/
private static byte[] byteUnswapper(byte[] encryption){
//TODO: write code here!
return encryption;
}
public static byte[] encrypt(String message){
String cypher = scrambler(message).toString();
byte[] encryption = cypher.getBytes(Charset.forName("UTF-8"));
byte[] swappedBytes = byteSwapper(encryption);
return swappedBytes;
}
public static String decrypt(byte[] encryption){
byte[] unswappedBytes = byteUnswapper(encryption);
LinkedList<String> list = fromStringToList(new String(unswappedBytes, Charset.forName("UTF-8")));
LinkedList<String> unSwapped = Encrypter.unswapper(list);
String unScrambled = Encrypter.unScrambler(unSwapped);
String decyphered = Encrypter.DecypherEncryption(unScrambled);
return decyphered;
}
public static void main(String[] args) {
System.out.println(Encrypter.caseConverter("AaaaAA", "eudycr"));
System.out.println(Encrypter.convertCase("Whats up man, how are you doing today?"));
System.out.println(Encrypter.obfuscate("Whats up man, how are you doing today?"));
System.out.println(Encrypter.scrambler("Whats up man, how are you doing today?"));
System.out.println(Encrypter.unswapper(Encrypter.scrambler("Whats up man, how are you doing today?")));
System.out.println(Encrypter.unScrambler(Encrypter.unswapper(Encrypter.scrambler("Whats up man, how are you doing today?"))));
System.out.println("");
System.out.println(Encrypter.DecypherEncryption(Encrypter.unScrambler(Encrypter.unswapper(Encrypter.scrambler("Whats up man, how are you doing today?")))));
System.out.println(Encrypter.DecypherEncryption("HIGYFS"));
System.out.println("");
System.out.println("");
System.out.println("");
byte[] encryption = Encrypter.encrypt("Whats up man, how are you doing today?");
System.out.println(printBytes(encryption));
System.out.println(Arrays.toString(encryption));
System.out.println(Encrypter.decrypt(encryption));
}
}
import java.nio.charset.Charset;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
/**
* This is a class which can be used to encrypt any sort of string,
* the class provides a simple encryption program which uses a scrambler/adder/replacer/swapper.
* The encrypted message is sent as an object which can then be casted
* into a String LinkedList or a String array which can then be formated and decrypted.
* If the message is infiltrated while traveling through a socket the thief
* wont be able to see the content of the message without the key or this class.
*
* @author Eudy Contreras
*
*/
public class Encrypter {
private static final char[] plain =
{'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','Ö','Ä','Å','0','1','2',
'3','4','5','6','7','8','9'};
private static final char[] key =
{'D','E','F','G','H','Ä','U','J',
'A','L','Z','Å','N','C','P','Q',
'W','S','T','Ö','I','V','R','X',
'Y','M','K','B','O','4','6','1',
'8','3','2','9','0','5','7'};
private static final char[] added =
{'L','E','Z','G','H','Ä','U','J',
'A','D','F','Å','N','C','M','Y',
'W','S','B','Ö','I','V','R','1',
'Q','P','K','T','O','4','6','X',
'8','3','2','9','0','5','7'};
private static final String[] bytePlaim =
{"1","2","3","4","5","6","7","8","9","0"};
private static final String[] byteKey =
{"8","0","5","4","9","2","1","3","7","6"};
private static final Map<Character, Character> plainMap;
static
{
plainMap = new HashMap<Character, Character>();
for(int i = 0; i<plain.length; i++){
plainMap.put(plain[i],key[i]);
}
}
private static final Map<Character, Character> keyMap;
static
{
keyMap = new HashMap<Character, Character>();
for(int i = 0; i<key.length; i++){
keyMap.put(key[i],plain[i]);
}
}
private static final Map<String, String> plainByteMap;
static
{
plainByteMap = new HashMap<String, String>();
for(int i = 0; i<bytePlaim.length; i++){
plainByteMap.put(bytePlaim[i],byteKey[i]);
}
}
private static final Map<String, String> keyByteMap;
static
{
keyByteMap = new HashMap<String, String>();
for(int i = 0; i<byteKey.length; i++){
keyByteMap.put(byteKey[i],bytePlaim[i]);
}
}
private static final int operationCount = 16;
/**
* This method will will used a simple key
* and encrypt a message
* @param message
* @return
*/
private static String obfuscate(String message) {
String caseCode = convertCase(message);
char[] caseBinary = new char[message.length()];
for(int i = 0; i<caseCodei < message.length(); i++) {
if(caseCodeCharacter.isUpperCase(message.charAt(i) == 'A')) {
caseBinary[i] = '1';
} else if(caseCodeCharacter.isLowerCase(message.charAt(i) == 'a')) {
caseBinary[i] = '0';
}
}
char[] upperCaseMessage = new char[message.length()];
for(int i = 0; i<message.length(); i++) {
upperCaseMessage[i] = Character.toUpperCase(message.charAt(i));
}
char[] code = upperCaseMessage;
for (int i = 0; i < code.length; i++) {
if(plainMap.containsKey(code[i])){
code[i] = plainMap.get(code[i]);
}
else {code[i] = code[i];}
}
return String.valueOf(code)+"¤¤¤¤"+String.valueOf(caseBinary);
}
/**
* this method will decypherdecipher and encrypted string message
* @param message
* @return
*/
private static String DecypherEncryption(String message) {
String[] code_and_case = message.split("¤¤¤¤");
String codeMessage;
String caseMessage;
if(code_and_case.length == 2) {
codeMessage = code_and_case[0];
caseMessage = code_and_case[1];
}
else {
codeMessage = message;
caseMessage = null;
}
char[] code = codeMessage.toCharArray();
for (int i = 0; i < code.length; i++) {
if(keyMap.containsKey(code[i])){
code[i] = keyMap.get(code[i]);
}
else {code[i] = code[i];}
}
if (code_and_case.length == 2) {
for (int i = 0; i < caseMessage.length()-1; i++) {
if (caseMessage.charAt(i) == '1') {
code[i] = Character.toUpperCase(code[i]);
} else if (caseMessage.charAt(i) == '0') {
code[i] = Character.toLowerCase(code[i]);
}
}
}
return String.valueOf(code);
}
private static int getRandom(int value){
SecureRandom rand = new SecureRandom();
return rand.nextInt(value);
}
/**
* Further increases the encryption level of the message by adding random numbers and characters
* to the already encrypted message the process can then be reversed with an additional key.
* @param code: the encrypted message
* @return
*/
private static LinkedList<String> scrambler(String code) {
Random rand1 = new Random();
Random rand2 = new Random();
Random rand3 = new Random();
Random rand4 = new Random();
String encryptedCode = Encrypter.obfuscate(code);
String[] case_and_code = encryptedCode.split("¤¤¤¤");
String cypher = case_and_code[0];
String codeCase = case_and_code[1];
LinkedList<String> cypherList = new LinkedList<>();
char[] cypherCharArr = cypher.toCharArray();
for (int index = 0; index < cypherCharArr.length; index++){
cypherList.add(added[rand3.nextIntadded[getRandom(added.length)]+""+added[rand2.nextInt] + "" + added[getRandom(added.length)]+String]
+ String.valueOf(cypherCharArr[index]) + added[rand4.nextIntadded[getRandom(added.length)]+]
+ (rand1.nextIntgetRandom(99 - 10+110 + 1)+10 + 10));
}
cypherList.addFirst(""+"" + (rand1.nextIntgetRandom(999 - 100 +1+ 1)+100 + 100));
cypherList.add(codeCase+rand1.nextIntcodeCase + getRandom(10));
return swapper(cypherList);
}
/**
* This method will unscramble and rebuild a scrambled string
* @param cypherList
* @return
*/
private static String unScrambler(LinkedList<String>List<String> cypherList) {
StringBuilder stringBuilder = new StringBuilder();
LinkedList<String>List<String> cypherListOutput = new LinkedList<>();
String caseCode = cypherList.get(cypherList.size()-1);
for (int index = 0; index < cypherList.size()-1; index++) {
cypherListOutput.add(String.valueOf(cypherList.get(index).toCharArray()[2]));
if(index>=1index >= 1)
stringBuilder.append(cypherListOutput.get(index));
continue;
}
char[] rawCode = (stringBuilder.toString()+"¤¤¤¤"+caseCode + "¤¤¤¤" + caseCode).toCharArray();
char[] unscrambled = new char[rawCode.length-1];
for(int i = 0; i<unscrambledi < unscrambled.length; i++) {
unscrambled[i] = rawCode[i];
}
return String.valueOf(unscrambled);
}
/**
* Swaps the content of the array the as many times as
* the operation count.
* @param code
* @return
*/
private static LinkedList<String> swapper(LinkedList<String> code) {
LinkedList<String> output = new LinkedList<>();
LinkedList<char[]> chars = new LinkedList<>();
String[] swapArray = new String[code.size()];
for (int index = 0; index < code.size(); index++) {
swapArray[index] = code.get(index);
chars.add(swapArray[index].toCharArray());
}
for (int iteration = 0; iteration < operationCount; iteration++) {
for (int i = 1; i < chars.size() - 2; i++) {
for (int r = 0; r < chars.get(i).length - 1; r++) {
char tempChar = chars.get(i)[r];
chars.get(i)[r] = chars.get(i + 1)[r];
chars.get(i + 1)[r] = tempChar;
}
}
}
for (int i = 0; i < chars.size(); i++) {
output.add(String.valueOf(chars.get(i)));
}
return output;
}
/**
* unswappsunswaps the content of the array in order to make it
* ready to be deciphered
* @param code
* @return
*/
private static LinkedList<String>List<String> unswapper(LinkedList<String>List<String> code) {
LinkedList<String>List<String> output = new LinkedList<>();
LinkedList<char[]>List<char[]> chars = new LinkedList<>();
String[] swapArray = new String[code.size()];
for (int index = 0; index < code.size(); index++) {
swapArray[index] = code.get(index);
chars.add(swapArray[index].toCharArray());
}
for (int iteration = 0; iteration < operationCount; iteration++) {
for (int i = chars.size() - 3; i >= 1; i--) {
for (int r = chars.get(i).length - 2; r >= 0; r--) {
char tempChar = chars.get(i)[r];
chars.get(i)[r] = chars.get(i + 1)[r];
chars.get(i + 1)[r] = tempChar;
}
}
}
for (int i = 0; i < chars.size(); i++) {
output.add(String.valueOf(chars.get(i)));
}
return output;
}
private static String printBytes(byte[] binaryEncription){
return new String(binaryEncription, Charset.forName("UTF-8"));
}
private static List<String> fromStringToList(String list) {
return Arrays.asList(list.split(", "));
}
private static byte[] toPrimitives(Byte[] byteObject){
byte[] bytes = new byte[byteObject.length];
int i = 0;
for(Byte byteObjects : byteObject)bytes[i++] = byteObjects;
return bytes;
}
private static Byte[] toByteObject(byte[] bytesPrim) {
Byte[] bytes = new Byte[bytesPrim.length];
int i = 0;
for (byte bytePrimes : bytesPrim) bytes[i++] = bytePrimes;
return bytes;
}
/**
* This method will convert a string into case holder
* which hold which character isReplace upperthe casefirst anddigits which
of the byte to a *predefined isn't.digit!
* @param messagebytes
* @return
*/
private static StringByte[] convertCasebyteReplacer(StringByte[] messagebytes) {
char[]Byte[] caseHoldertempBytes = newArrays.copyOf(bytes, char[messagebytes.length()];;
for(int i = 0; i < caseHoldertempBytes.length; i++) {
if (CharacterInteger.isUpperCasetoString(messagetempBytes[i]).charAt(i)0) != '-') {
caseHolder[i]String tempString = 'A';String.valueOf(tempBytes[i]);
} else if (Character String newValue = plainByteMap.isLowerCaseget(messageInteger.charAttoString(itempBytes[i]).substring(0, 1)) { + tempString.substring(1);
caseHolder[i]tempBytes[i] = 'a';Byte.valueOf(newValue);
}
}
return String.valueOf(caseHolder);tempBytes;
}
/**
* This method will take a case code holder
* and apply the casePlaces toback the given message
original digit which *was @parampreviously caseBankreplaced
* @param messagebytes
* @return
*/
private static StringByte[] caseConverterbytePlaceBack(String caseBank,StringByte[] messagebytes){
StringBuilderByte[] inputtempBytes = new StringBuilderArrays.copyOf(caseBank);
StringBuilder output = newbytes, StringBuilder(messagebytes.length);
for (int indexi = 0; indexi < inputtempBytes.length();length; index++i++) {
char current =if input(Integer.toString(tempBytes[i]).charAt(index0); != '-') {
char formatedString String tempString = outputString.charAtvalueOf(indextempBytes[i]);
if (Character.isLowerCase(current)) {
String newValue = keyByteMap.get(Integer.toString(tempBytes[i]).substring(0, 1))
output.setCharAt(index, Character.toLowerCase(formatedString));
} else+ {tempString.substring(1);
output.setCharAt(index,tempBytes[i] Character= Byte.toUpperCasevalueOf(formatedString)newValue);
}
}
return output.toString();
}
private static String printBytes(byte[] binaryEncription){
return new String(binaryEncription, Charset.forName("UTF-8"));
}
private static LinkedList<String> fromStringToList(String list){
String[] newList = list.split(", ");
LinkedList<String> code = new LinkedList<String>();
for(int i = 0; i<newList.length; i++){
code.add(newList[i]);
}
return code;tempBytes;
}
/**
* Can be used to further increaseReverses the level of
byte array for *further encryption;obfuscation
* @param encryption
* @return
*/
private static byte[] byteSwapperbyteReverser(byte[]Byte[] encryption){
//Byte[] TODO:byteArray write= codebyteReplacer(Arrays.copyOf(encryption, here!!!encryption.length));
// Further swapping may not be needed! List<Byte> byteList = Arrays.asList(byteArray);
Collections.reverse(byteList);
return encryption;toPrimitives(byteArray);
}
/**
* To unswappReverses the byte array back to normalits original state
* @param encryption
* @return
*/
private static byte[] byteUnswapperbyteSortter(byte[]Byte[] encryption){
//TODO:Byte[] writebyteArray code= here!encryption;
List<Byte> byteList = Arrays.asList(byteArray);
Collections.reverse(byteList);
return encryption;toPrimitives(bytePlaceBack(byteArray));
}
public static byte[] encrypt(String message){
String cypher = scrambler(message).toString();
byte[] encryption = cypher.getBytes(Charset.forName("UTF-8"));
byte[] swappedBytes = byteSwapperbyteReverser(toByteObject(encryption));
return swappedBytes;
}
public static String decrypt(byte[] encryption){
byte[] unswappedBytes = byteUnswapperbyteSortter(toByteObject(encryption));
LinkedList<String>List<String> list = fromStringToList(new String(unswappedBytes, Charset.forName("UTF-8")));
LinkedList<String>List<String> unSwapped = Encrypter.unswapper(list);
String unScrambled = Encrypter.unScrambler(unSwapped);
String decyphered = Encrypter.DecypherEncryption(unScrambled);
return decyphered;
}
public static void main(String[] args) {
System.out.println(Encrypter.caseConverter("AaaaAA", "eudycr"));
System.out.println(Encrypter.convertCase("Whats up man, how are you doing today?"));
System.out.println(Encrypter.obfuscate("Whats up man, how are you doing today?"));
System.out.println(Encrypter.scrambler("Whats up man, how are you doing today?"));
System.out.println(Encrypter.unswapper(Encrypter.scrambler("Whats up man, how are you doing today?")));
System.out.println(Encrypter.unScrambler(Encrypter.unswapper(Encrypter.scrambler("Whats up man, how are you doing today?"))));
System.out.println("");
System.out.println(Encrypter.DecypherEncryption(Encrypter.unScrambler(Encrypter.unswapper(Encrypter.scrambler("Whats up man, how are you doing today?")))));
System.out.println(Encrypter.DecypherEncryption("HIGYFS"));
System.out.println("");
System.out.println("");
System.out.println("");
byte[] encryption = Encrypter.encrypt("Whats up man, how are you doing today?");
System.out.println("Actual encrypted message:");
System.out.println("");
System.out.println(printBytes(encryption));
System.out.println("");
System.out.println(Arrays.toString(encryption));
System.out.println(Encrypter.decrypt(encryption));
}
}
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Random;
/**
* This is a class which can be used to encrypt any sort of string,
* the class provides a simple encryption program which uses a scrambler/adder/replacer/swapper.
* The encrypted message is sent as an object which can then be casted
* into a String LinkedList or a String array which can then be formated and decrypted.
* If the message is infiltrated while traveling through a socket the thief
* wont be able to see the content of the message without the key or this class.
*
* @author Eudy Contreras
*
*/
public class Encrypter {
private static final char[] plain =
{'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','Ö','Ä','Å','0','1','2',
'3','4','5','6','7','8','9'};
private static final char[] key =
{'D','E','F','G','H','Ä','U','J',
'A','L','Z','Å','N','C','P','Q',
'W','S','T','Ö','I','V','R','X',
'Y','M','K','B','O','4','6','1',
'8','3','2','9','0','5','7'};
private static final char[] added =
{'L','E','Z','G','H','Ä','U','J',
'A','D','F','Å','N','C','M','Y',
'W','S','B','Ö','I','V','R','1',
'Q','P','K','T','O','4','6','X',
'8','3','2','9','0','5','7'};
private static final Map<Character, Character> plainMap;
static
{
plainMap = new HashMap<Character, Character>();
for(int i = 0; i<plain.length; i++){
plainMap.put(plain[i],key[i]);
}
}
private static final Map<Character, Character> keyMap;
static
{
keyMap = new HashMap<Character, Character>();
for(int i = 0; i<key.length; i++){
keyMap.put(key[i],plain[i]);
}
}
private static final int operationCount = 16;
/**
* This method will will used a simple key
* and encrypt a message
* @param message
* @return
*/
private static String obfuscate(String message) {
String caseCode = convertCase(message);
char[] caseBinary = new char[message.length()];
for(int i = 0; i<caseCode.length(); i++) {
if(caseCode.charAt(i) == 'A') {
caseBinary[i] = '1';
} else if(caseCode.charAt(i) == 'a') {
caseBinary[i] = '0';
}
}
char[] upperCaseMessage = new char[message.length()];
for(int i = 0; i<message.length(); i++) {
upperCaseMessage[i] = Character.toUpperCase(message.charAt(i));
}
char[] code = upperCaseMessage;
for (int i = 0; i < code.length; i++) {
if(plainMap.containsKey(code[i])){
code[i] = plainMap.get(code[i]);
}
else {code[i] = code[i];}
}
return String.valueOf(code)+"¤¤¤¤"+String.valueOf(caseBinary);
}
/**
* this method will decypher and encrypted string message
* @param message
* @return
*/
private static String DecypherEncryption(String message) {
String[] code_and_case = message.split("¤¤¤¤");
String codeMessage;
String caseMessage;
if(code_and_case.length == 2) {
codeMessage = code_and_case[0];
caseMessage = code_and_case[1];
}
else {
codeMessage = message;
caseMessage = null;
}
char[] code = codeMessage.toCharArray();
for (int i = 0; i < code.length; i++) {
if(keyMap.containsKey(code[i])){
code[i] = keyMap.get(code[i]);
}
else {code[i] = code[i];}
}
if (code_and_case.length == 2) {
for (int i = 0; i < caseMessage.length()-1; i++) {
if (caseMessage.charAt(i) == '1') {
code[i] = Character.toUpperCase(code[i]);
} else if (caseMessage.charAt(i) == '0') {
code[i] = Character.toLowerCase(code[i]);
}
}
}
return String.valueOf(code);
}
/**
* Further increases the encryption level of the message by adding random numbers and characters
* to the already encrypted message the process can then be reversed with an additional key.
* @param code: the encrypted message
* @return
*/
private static LinkedList<String> scrambler(String code) {
Random rand1 = new Random();
Random rand2 = new Random();
Random rand3 = new Random();
Random rand4 = new Random();
String encryptedCode = Encrypter.obfuscate(code);
String[] case_and_code = encryptedCode.split("¤¤¤¤");
String cypher = case_and_code[0];
String codeCase = case_and_code[1];
LinkedList<String> cypherList = new LinkedList<>();
char[] cypherCharArr = cypher.toCharArray();
for (int index = 0; index < cypherCharArr.length; index++){
cypherList.add(added[rand3.nextInt(added.length)]+""+added[rand2.nextInt(added.length)]+String.valueOf(cypherCharArr[index]) + added[rand4.nextInt(added.length)]+(rand1.nextInt(99 - 10+1)+10));
}
cypherList.addFirst(""+(rand1.nextInt(999 - 100 +1 )+100));
cypherList.add(codeCase+rand1.nextInt(10));
return swapper(cypherList);
}
/**
* This method will unscramble and rebuild a scrambled string
* @param cypherList
* @return
*/
private static String unScrambler(LinkedList<String> cypherList) {
StringBuilder stringBuilder = new StringBuilder();
LinkedList<String> cypherListOutput = new LinkedList<>();
String caseCode = cypherList.get(cypherList.size()-1);
for (int index = 0; index < cypherList.size()-1; index++) {
cypherListOutput.add(String.valueOf(cypherList.get(index).toCharArray()[2]));
if(index>=1)
stringBuilder.append(cypherListOutput.get(index));
continue;
}
char[] rawCode = (stringBuilder.toString()+"¤¤¤¤"+caseCode).toCharArray();
char[] unscrambled = new char[rawCode.length-1];
for(int i = 0; i<unscrambled.length; i++) {
unscrambled[i] = rawCode[i];
}
return String.valueOf(unscrambled);
}
/**
* Swaps the content of the array the as many times as
* the operation count.
* @param code
* @return
*/
private static LinkedList<String> swapper(LinkedList<String> code) {
LinkedList<String> output = new LinkedList<>();
LinkedList<char[]> chars = new LinkedList<>();
String[] swapArray = new String[code.size()];
for (int index = 0; index < code.size(); index++) {
swapArray[index] = code.get(index);
chars.add(swapArray[index].toCharArray());
}
for (int iteration = 0; iteration < operationCount; iteration++) {
for (int i = 1; i < chars.size() - 2; i++) {
for (int r = 0; r < chars.get(i).length - 1; r++) {
char tempChar = chars.get(i)[r];
chars.get(i)[r] = chars.get(i + 1)[r];
chars.get(i + 1)[r] = tempChar;
}
}
}
for (int i = 0; i < chars.size(); i++) {
output.add(String.valueOf(chars.get(i)));
}
return output;
}
/**
* unswapps the content of the array in order to make it
* ready to be deciphered
* @param code
* @return
*/
private static LinkedList<String> unswapper(LinkedList<String> code) {
LinkedList<String> output = new LinkedList<>();
LinkedList<char[]> chars = new LinkedList<>();
String[] swapArray = new String[code.size()];
for (int index = 0; index < code.size(); index++) {
swapArray[index] = code.get(index);
chars.add(swapArray[index].toCharArray());
}
for (int iteration = 0; iteration < operationCount; iteration++) {
for (int i = chars.size() - 3; i >= 1; i--) {
for (int r = chars.get(i).length - 2; r >= 0; r--) {
char tempChar = chars.get(i)[r];
chars.get(i)[r] = chars.get(i + 1)[r];
chars.get(i + 1)[r] = tempChar;
}
}
}
for (int i = 0; i < chars.size(); i++) {
output.add(String.valueOf(chars.get(i)));
}
return output;
}
/**
* This method will convert a string into case holder
* which hold which character is upper case and which
* isn't.
* @param message
* @return
*/
private static String convertCase(String message) {
char[] caseHolder = new char[message.length()];
for(int i = 0; i < caseHolder.length; i++) {
if (Character.isUpperCase(message.charAt(i))) {
caseHolder[i] = 'A';
} else if (Character.isLowerCase(message.charAt(i))) {
caseHolder[i] = 'a';
}
}
return String.valueOf(caseHolder);
}
/**
* This method will take a case code holder
* and apply the case to the given message
* @param caseBank
* @param message
* @return
*/
private static String caseConverter(String caseBank,String message){
StringBuilder input = new StringBuilder(caseBank);
StringBuilder output = new StringBuilder(message);
for (int index = 0; index < input.length(); index++) {
char current = input.charAt(index);
char formatedString = output.charAt(index);
if (Character.isLowerCase(current)) {
output.setCharAt(index, Character.toLowerCase(formatedString));
} else {
output.setCharAt(index, Character.toUpperCase(formatedString));
}
}
return output.toString();
}
private static String printBytes(byte[] binaryEncription){
return new String(binaryEncription, Charset.forName("UTF-8"));
}
private static LinkedList<String> fromStringToList(String list){
String[] newList = list.split(", ");
LinkedList<String> code = new LinkedList<String>();
for(int i = 0; i<newList.length; i++){
code.add(newList[i]);
}
return code;
}
/**
* Can be used to further increase the level of
* encryption;
* @param encryption
* @return
*/
private static byte[] byteSwapper(byte[] encryption){
// TODO: write code here!!!
// Further swapping may not be needed!
return encryption;
}
/**
* To unswapp the byte array back to normal
* @param encryption
* @return
*/
private static byte[] byteUnswapper(byte[] encryption){
//TODO: write code here!
return encryption;
}
public static byte[] encrypt(String message){
String cypher = scrambler(message).toString();
byte[] encryption = cypher.getBytes(Charset.forName("UTF-8"));
byte[] swappedBytes = byteSwapper(encryption);
return swappedBytes;
}
public static String decrypt(byte[] encryption){
byte[] unswappedBytes = byteUnswapper(encryption);
LinkedList<String> list = fromStringToList(new String(unswappedBytes, Charset.forName("UTF-8")));
LinkedList<String> unSwapped = Encrypter.unswapper(list);
String unScrambled = Encrypter.unScrambler(unSwapped);
String decyphered = Encrypter.DecypherEncryption(unScrambled);
return decyphered;
}
public static void main(String[] args) {
System.out.println(Encrypter.caseConverter("AaaaAA", "eudycr"));
System.out.println(Encrypter.convertCase("Whats up man, how are you doing today?"));
System.out.println(Encrypter.obfuscate("Whats up man, how are you doing today?"));
System.out.println(Encrypter.scrambler("Whats up man, how are you doing today?"));
System.out.println(Encrypter.unswapper(Encrypter.scrambler("Whats up man, how are you doing today?")));
System.out.println(Encrypter.unScrambler(Encrypter.unswapper(Encrypter.scrambler("Whats up man, how are you doing today?"))));
System.out.println("");
System.out.println(Encrypter.DecypherEncryption(Encrypter.unScrambler(Encrypter.unswapper(Encrypter.scrambler("Whats up man, how are you doing today?")))));
System.out.println(Encrypter.DecypherEncryption("HIGYFS"));
System.out.println("");
System.out.println("");
System.out.println("");
byte[] encryption = Encrypter.encrypt("Whats up man, how are you doing today?");
System.out.println(printBytes(encryption));
System.out.println(Arrays.toString(encryption));
System.out.println(Encrypter.decrypt(encryption));
}
}
import java.nio.charset.Charset;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* This is a class which can be used to encrypt any sort of string,
* the class provides a simple encryption program which uses a scrambler/adder/replacer/swapper.
* The encrypted message is sent as an object which can then be casted
* into a String LinkedList or a String array which can then be formated and decrypted.
* If the message is infiltrated while traveling through a socket the thief
* wont be able to see the content of the message without the key or this class.
*
* @author Eudy Contreras
*
*/
public class Encrypter {
private static final char[] plain =
{'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','Ö','Ä','Å','0','1','2',
'3','4','5','6','7','8','9'};
private static final char[] key =
{'D','E','F','G','H','Ä','U','J',
'A','L','Z','Å','N','C','P','Q',
'W','S','T','Ö','I','V','R','X',
'Y','M','K','B','O','4','6','1',
'8','3','2','9','0','5','7'};
private static final char[] added =
{'L','E','Z','G','H','Ä','U','J',
'A','D','F','Å','N','C','M','Y',
'W','S','B','Ö','I','V','R','1',
'Q','P','K','T','O','4','6','X',
'8','3','2','9','0','5','7'};
private static final String[] bytePlaim =
{"1","2","3","4","5","6","7","8","9","0"};
private static final String[] byteKey =
{"8","0","5","4","9","2","1","3","7","6"};
private static final Map<Character, Character> plainMap;
static
{
plainMap = new HashMap<Character, Character>();
for(int i = 0; i<plain.length; i++){
plainMap.put(plain[i],key[i]);
}
}
private static final Map<Character, Character> keyMap;
static
{
keyMap = new HashMap<Character, Character>();
for(int i = 0; i<key.length; i++){
keyMap.put(key[i],plain[i]);
}
}
private static final Map<String, String> plainByteMap;
static
{
plainByteMap = new HashMap<String, String>();
for(int i = 0; i<bytePlaim.length; i++){
plainByteMap.put(bytePlaim[i],byteKey[i]);
}
}
private static final Map<String, String> keyByteMap;
static
{
keyByteMap = new HashMap<String, String>();
for(int i = 0; i<byteKey.length; i++){
keyByteMap.put(byteKey[i],bytePlaim[i]);
}
}
private static final int operationCount = 16;
/**
* This method will will used a simple key
* and encrypt a message
* @param message
* @return
*/
private static String obfuscate(String message) {
char[] caseBinary = new char[message.length()];
for(int i = 0; i < message.length(); i++) {
if(Character.isUpperCase(message.charAt(i))) {
caseBinary[i] = '1';
} else if(Character.isLowerCase(message.charAt(i))) {
caseBinary[i] = '0';
}
}
char[] upperCaseMessage = new char[message.length()];
for(int i = 0; i<message.length(); i++) {
upperCaseMessage[i] = Character.toUpperCase(message.charAt(i));
}
char[] code = upperCaseMessage;
for (int i = 0; i < code.length; i++) {
if(plainMap.containsKey(code[i])){
code[i] = plainMap.get(code[i]);
}
}
return String.valueOf(code)+"¤¤¤¤"+String.valueOf(caseBinary);
}
/**
* this method will decipher and encrypted string message
* @param message
* @return
*/
private static String DecypherEncryption(String message) {
String[] code_and_case = message.split("¤¤¤¤");
String codeMessage;
String caseMessage;
if(code_and_case.length == 2) {
codeMessage = code_and_case[0];
caseMessage = code_and_case[1];
}
else {
codeMessage = message;
caseMessage = null;
}
char[] code = codeMessage.toCharArray();
for (int i = 0; i < code.length; i++) {
if(keyMap.containsKey(code[i])){
code[i] = keyMap.get(code[i]);
}
}
if (code_and_case.length == 2) {
for (int i = 0; i < caseMessage.length()-1; i++) {
if (caseMessage.charAt(i) == '1') {
code[i] = Character.toUpperCase(code[i]);
} else if (caseMessage.charAt(i) == '0') {
code[i] = Character.toLowerCase(code[i]);
}
}
}
return String.valueOf(code);
}
private static int getRandom(int value){
SecureRandom rand = new SecureRandom();
return rand.nextInt(value);
}
/**
* Further increases the encryption level of the message by adding random numbers and characters
* to the already encrypted message the process can then be reversed with an additional key.
* @param code: the encrypted message
* @return
*/
private static LinkedList<String> scrambler(String code) {
String encryptedCode = Encrypter.obfuscate(code);
String[] case_and_code = encryptedCode.split("¤¤¤¤");
String cypher = case_and_code[0];
String codeCase = case_and_code[1];
LinkedList<String> cypherList = new LinkedList<>();
char[] cypherCharArr = cypher.toCharArray();
for (int index = 0; index < cypherCharArr.length; index++){
cypherList.add(added[getRandom(added.length)] + "" + added[getRandom(added.length)]
+ String.valueOf(cypherCharArr[index]) + added[getRandom(added.length)]
+ (getRandom(99 - 10 + 1) + 10));
}
cypherList.addFirst("" + (getRandom(999 - 100 + 1) + 100));
cypherList.add(codeCase + getRandom(10));
return swapper(cypherList);
}
/**
* This method will unscramble and rebuild a scrambled string
* @param cypherList
* @return
*/
private static String unScrambler(List<String> cypherList) {
StringBuilder stringBuilder = new StringBuilder();
List<String> cypherListOutput = new LinkedList<>();
String caseCode = cypherList.get(cypherList.size()-1);
for (int index = 0; index < cypherList.size()-1; index++) {
cypherListOutput.add(String.valueOf(cypherList.get(index).toCharArray()[2]));
if(index >= 1)
stringBuilder.append(cypherListOutput.get(index));
continue;
}
char[] rawCode = (stringBuilder.toString() + "¤¤¤¤" + caseCode).toCharArray();
char[] unscrambled = new char[rawCode.length-1];
for(int i = 0; i < unscrambled.length; i++) {
unscrambled[i] = rawCode[i];
}
return String.valueOf(unscrambled);
}
/**
* Swaps the content of the array the as many times as
* the operation count.
* @param code
* @return
*/
private static LinkedList<String> swapper(LinkedList<String> code) {
LinkedList<String> output = new LinkedList<>();
LinkedList<char[]> chars = new LinkedList<>();
String[] swapArray = new String[code.size()];
for (int index = 0; index < code.size(); index++) {
swapArray[index] = code.get(index);
chars.add(swapArray[index].toCharArray());
}
for (int iteration = 0; iteration < operationCount; iteration++) {
for (int i = 1; i < chars.size() - 2; i++) {
for (int r = 0; r < chars.get(i).length - 1; r++) {
char tempChar = chars.get(i)[r];
chars.get(i)[r] = chars.get(i + 1)[r];
chars.get(i + 1)[r] = tempChar;
}
}
}
for (int i = 0; i < chars.size(); i++) {
output.add(String.valueOf(chars.get(i)));
}
return output;
}
/**
* unswaps the content of the array in order to make it
* ready to be deciphered
* @param code
* @return
*/
private static List<String> unswapper(List<String> code) {
List<String> output = new LinkedList<>();
List<char[]> chars = new LinkedList<>();
String[] swapArray = new String[code.size()];
for (int index = 0; index < code.size(); index++) {
swapArray[index] = code.get(index);
chars.add(swapArray[index].toCharArray());
}
for (int iteration = 0; iteration < operationCount; iteration++) {
for (int i = chars.size() - 3; i >= 1; i--) {
for (int r = chars.get(i).length - 2; r >= 0; r--) {
char tempChar = chars.get(i)[r];
chars.get(i)[r] = chars.get(i + 1)[r];
chars.get(i + 1)[r] = tempChar;
}
}
}
for (int i = 0; i < chars.size(); i++) {
output.add(String.valueOf(chars.get(i)));
}
return output;
}
private static String printBytes(byte[] binaryEncription){
return new String(binaryEncription, Charset.forName("UTF-8"));
}
private static List<String> fromStringToList(String list) {
return Arrays.asList(list.split(", "));
}
private static byte[] toPrimitives(Byte[] byteObject){
byte[] bytes = new byte[byteObject.length];
int i = 0;
for(Byte byteObjects : byteObject)bytes[i++] = byteObjects;
return bytes;
}
private static Byte[] toByteObject(byte[] bytesPrim) {
Byte[] bytes = new Byte[bytesPrim.length];
int i = 0;
for (byte bytePrimes : bytesPrim) bytes[i++] = bytePrimes;
return bytes;
}
/**
* Replace the first digits of the byte to a predefined digit!
* @param bytes
* @return
*/
private static Byte[] byteReplacer(Byte[] bytes) {
Byte[] tempBytes = Arrays.copyOf(bytes, bytes.length);
for(int i = 0; i < tempBytes.length; i++) {
if (Integer.toString(tempBytes[i]).charAt(0) != '-') {
String tempString = String.valueOf(tempBytes[i]);
String newValue = plainByteMap.get(Integer.toString(tempBytes[i]).substring(0, 1)) + tempString.substring(1);
tempBytes[i] = Byte.valueOf(newValue);
}
}
return tempBytes;
}
/**
* Places back the original digit which was previously replaced
* @param bytes
* @return
*/
private static Byte[] bytePlaceBack(Byte[] bytes){
Byte[] tempBytes = Arrays.copyOf(bytes, bytes.length);
for (int i = 0; i < tempBytes.length; i++) {
if (Integer.toString(tempBytes[i]).charAt(0) != '-') {
String tempString = String.valueOf(tempBytes[i]);
String newValue = keyByteMap.get(Integer.toString(tempBytes[i]).substring(0, 1))
+ tempString.substring(1);
tempBytes[i] = Byte.valueOf(newValue);
}
}
return tempBytes;
}
/**
* Reverses the byte array for further obfuscation
* @param encryption
* @return
*/
private static byte[] byteReverser(Byte[] encryption){
Byte[] byteArray = byteReplacer(Arrays.copyOf(encryption, encryption.length));
List<Byte> byteList = Arrays.asList(byteArray);
Collections.reverse(byteList);
return toPrimitives(byteArray);
}
/**
* Reverses the byte array to its original state
* @param encryption
* @return
*/
private static byte[] byteSortter(Byte[] encryption){
Byte[] byteArray = encryption;
List<Byte> byteList = Arrays.asList(byteArray);
Collections.reverse(byteList);
return toPrimitives(bytePlaceBack(byteArray));
}
public static byte[] encrypt(String message){
String cypher = scrambler(message).toString();
byte[] encryption = cypher.getBytes(Charset.forName("UTF-8"));
byte[] swappedBytes = byteReverser(toByteObject(encryption));
return swappedBytes;
}
public static String decrypt(byte[] encryption){
byte[] unswappedBytes = byteSortter(toByteObject(encryption));
List<String> list = fromStringToList(new String(unswappedBytes, Charset.forName("UTF-8")));
List<String> unSwapped = Encrypter.unswapper(list);
String unScrambled = Encrypter.unScrambler(unSwapped);
String decyphered = Encrypter.DecypherEncryption(unScrambled);
return decyphered;
}
public static void main(String[] args) {
System.out.println(Encrypter.obfuscate("Whats up man, how are you doing today?"));
System.out.println(Encrypter.scrambler("Whats up man, how are you doing today?"));
System.out.println(Encrypter.unswapper(Encrypter.scrambler("Whats up man, how are you doing today?")));
System.out.println(Encrypter.unScrambler(Encrypter.unswapper(Encrypter.scrambler("Whats up man, how are you doing today?"))));
System.out.println("");
System.out.println(Encrypter.DecypherEncryption(Encrypter.unScrambler(Encrypter.unswapper(Encrypter.scrambler("Whats up man, how are you doing today?")))));
System.out.println(Encrypter.DecypherEncryption("HIGYFS"));
System.out.println("");
System.out.println("");
byte[] encryption = Encrypter.encrypt("Whats up man, how are you doing today?");
System.out.println("Actual encrypted message:");
System.out.println("");
System.out.println(printBytes(encryption));
System.out.println("");
System.out.println(Arrays.toString(encryption));
System.out.println(Encrypter.decrypt(encryption));
}
}
Eudy Contreras
- 195
- 7
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Random;
/**
* This is a class which can be used to encrypt any sort of string,
* the class provides a simple encryption program which uses a scrambler/adder/replacer/swapper.
* The encrypted message is sent as an object which can then be casted
* into a byteString LinkedList or a String array which can then be formated and decrypted.
* If the message is infiltrated while traveling through a socket the thief
* wont be able to see the content of the message without the key or this class.
*
* @author Eudy Contreras
*
*/
/*
private static final char[] plain = {'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','Ö','Ä','Å','0','1','2',
'3','4','5','6','7','8','9'};
private static final char[] key = {'D','E','F','G','H','Ä','U','J',
'A','L','Z','Å','N','C','P','Q',
'W','S','T','Ö','I','V','R','X',
'Y','M','K','B','O','4','6','1',
'8','3','2','9','0','5','7'};
*/
public class Encrypter {
private static final char[] added plain ={'L''A','E''B','Z''C','G''D','H''E','Ä''F','U''G','J''H',
'I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X',
'A''Y','D''Z','F''Ö','Ä','Å','N''0','C''1','M''2','Y'
'3','4','5','6','7','8','9'};
private static final char[] key =
{'D','E','F','G','H','Ä','U','J',
'A','L','Z','Å','N','C','P','Q',
'W','S','B''T','Ö','I','V','R','1''X',
'Y','M','K','B','O','4','6','1',
'8','3','2','9','0','5','7'};
private static final char[] added =
'Q' {'L','P''E','K''Z','T''G','O''H','4''Ä','6''U','X''J',
'A','D','F','Å','N','C','M','Y',
'W','S','B','Ö','I','V','R','1',
'Q','P','K','T','O','4','6','X',
'8','3','2','9','0','5','7'};
private static final Map<Character, Character> plainMap;
static
{
plainMap = new HashMap<Character, Character>();
for(int i = 0; i<plain.length; i++){
plainMap.put(plain[i],key[i]);
}
}
private static final Map<Character, Character> keyMap;
static
{
keyMap = new HashMap<Character, Character>();
for(int i = 0; i<key.length; i++){
keyMap.put(key[i],plain[i]);
}
}
private static final int operationCount = 16;
/**
* This method will will used a simple key
* and encrypt a message
* @param message
* @return
*/
private static String obfuscate(String message) {
String caseCode = convertCase(message);
char[] caseBinary = new char[message.length()];
for(int i = 0; i<caseCode.length(); i++) {
if(caseCode.charAt(i) == 'A') {
caseBinary[i] = '1';
}
else if(caseCode.charAt(i) == 'a') {
caseBinary[i] = '0';
}
}
char[] upperCaseMessage = new char[message.length()];
for(int i = 0; i<message.length(); i++) {
upperCaseMessage[i] = Character.toUpperCase(message.charAt(i));
}
char[] code = upperCaseMessage;
for (int i = 0; i < code.length; i++) {
if(code[i] == 'A') {code[i] = 'D';}
else if(code[i] == 'B') {code[i] = 'E';}
else if(code[i] == 'C') {code[i] = 'F';}
else if(code[i] == 'D') {code[i] = 'G';}
else if(code[i] == 'E') {code[i] = 'H';}
else if(code[i] == 'F') {code[i] = 'Ä';}
else if(code[i] == 'G') {code[i] = 'U';}
else if(code[i] == 'H') {code[i] = 'J';}
else if(code[i] == 'I') {code[i] = 'A';}
else if(code[i] == 'J') {code[i] = 'L';}
else if(code[i] == 'K') {code[i] = 'Z';}
else if(code[i] == 'L') {code[i] = 'Å';}
else if(code[i] == 'M') {code[i] = 'N';}
else if(code[i] == 'N') {code[i] = 'C';}
else if(code[i] == 'O') {code[i] = 'P';}
else if(code[i] == 'P') {code[i] = 'Q';}
else if(code[i] == 'Q') {code[i] = 'W';}
else if(code[i] == 'R') {code[i] = 'S';}
else if(code[i] == 'S') {code[i] = 'T';}
else if(code[i] == 'T') {code[i] = 'Ö';}
else if(code[i] == 'U') {code[i] = 'I';}
else if(code[i] == 'V') {code[i] = 'V';}
else if(code[i] == 'W') {code[i] = 'R';}
else if(code[i] == 'X') {code[i] = 'S';}
else if(code[i] == 'Y') {code[i] = 'Y';}
else if(code[i] == 'Z') {code[i] = 'M';}
else if(code[i] == 'Ö') {code[i] = 'K';}
else if(code[i] == 'Ä') {code[i] = 'B';}
else if(code[i] == 'Å') {code[i] = 'O';}
else if(code[i] == '0') {code[i] = '4';}
else if(code[i] == '1') {code[i] = '6';}
else if(code[i] == '2') {code[i] = '1';}
else if(code[i] == '3') {code[i] = '8';}
else ifplainMap.containsKey(code[i] == '4') {code[i] = '3';}
else if(code[i] == '5'){code[i] = '2';}
else if(code[i] == '6') {code[i] = '9';}
else if(code[i] == '7') {code[i] = '0';}
else ifplainMap.get(code[i] == '8') {code[i] = '5';};
else if(code[i] == '9') {code[i] = '7';}
else {code[i] = code[i];}
}
return String.valueOf(code)+"¤¤¤¤"+String.valueOf(caseBinary);
}
/**
* this method will decypher and encrypted string message
* @param message
* @return
*/
private static String DecypherEncryption(String message) {
String[] code_and_case = message.split("¤¤¤¤");
String codeMessage;
String caseMessage;
if(code_and_case.length == 2) {
codeMessage = code_and_case[0];
caseMessage = code_and_case[1];
}
else {
codeMessage = message;
caseMessage = null;
}
char[] code = codeMessage.toCharArray();
for (int i = 0; i < code.length; i++) {
if (code[i] == 'D') {code[i] = 'A';}
else if(code[i] == 'E') {code[i] = 'B';}
else if(code[i] == 'F') {code[i] = 'C';}
else if(code[i] == 'G') {code[i] = 'D';}
else if(code[i] == 'H') {code[i] = 'E';}
else if(code[i] == 'Ä') {code[i] = 'F';}
else if(code[i] == 'U') {code[i] = 'G';}
else if(code[i] == 'J') {code[i] = 'H';}
else if(code[i] == 'A') {code[i] = 'I';}
else if(code[i] == 'L') {code[i] = 'J';}
else if(code[i] == 'Z') {code[i] = 'K';}
else if(code[i] == 'Å') {code[i] = 'L';}
else if(code[i] == 'N') {code[i] = 'M';}
else if(code[i] == 'C') {code[i] = 'N';}
else if(code[i] == 'P') {code[i] = 'O';}
else if(code[i] == 'Q') {code[i] = 'P';}
else if(code[i] == 'W') {code[i] = 'Q';}
else if(code[i] == 'S') {code[i] = 'R';}
else if(code[i] == 'T') {code[i] = 'S';}
else if(code[i] == 'Ö') {code[i] = 'T';}
else if(code[i] == 'I') {code[i] = 'U';}
else if(code[i] == 'V') {code[i] = 'V';}
else if(code[i] == 'R') {code[i] = 'W';}
else if(code[i] == 'S') {code[i] = 'X';}
else if(code[i] == 'Y') {code[i] = 'Y';}
else if(code[i] == 'M') {code[i] = 'Z';}
else if(code[i] == 'K') {code[i] = 'Ö';}
else if(code[i] == 'B') {code[i] = 'Ä';}
else if(code[i] == 'O') {code[i] = 'Å';}
else if(code[i] == '4') {code[i] = '0';}
else if(code[i] == '6') {code[i] = '1';}
else if(code[i] == '1') {code[i] = '2';}
else if(code[i] == '8') {code[i] = '3';}
else ifkeyMap.containsKey(code[i] == '3') {code[i] = '4';}
else if(code[i] == '2'){code[i] = '5';}
else if(code[i] == '9') {code[i] = '6';}
else if(code[i] == '0') {code[i] = '7';}
else ifkeyMap.get(code[i] == '5') {code[i] = '8';};
else if(code[i] == '7') {code[i] = '9';}
else {code[i] = code[i];}
}
if (code_and_case.length == 2) {
for (int i = 0; i < caseMessage.length()-1; i++) {
if (caseMessage.charAt(i) == '1') {
code[i] = Character.toUpperCase(code[i]);
} else if (caseMessage.charAt(i) == '0') {
code[i] = Character.toLowerCase(code[i]);
}
}
}
return String.valueOf(code);
}
/**
* Further increases the encryption level of the message by adding random numbers and characters
* to the already encrypted message the process can then be reversed with an additional key.
* @param code: the encrypted message
* @return
*/
private static LinkedList<String> scrambler(String code) {
Random rand1 = new Random();
Random rand2 = new Random();
Random rand3 = new Random();
Random rand4 = new Random();
String encryptedCode = Encrypter.obfuscate(code);
String[] case_and_code = encryptedCode.split("¤¤¤¤");
String cypher = case_and_code[0];
String codeCase = case_and_code[1];
LinkedList<String> cypherList = new LinkedList<>();
char[] cypherCharArr = cypher.toCharArray();
for (int index = 0; index < cypherCharArr.length; index++){
cypherList.add(added[rand3.nextInt(added.length)]+""+added[rand2.nextInt(added.length)]+String.valueOf(cypherCharArr[index]) + added[rand4.nextInt(added.length)]+(rand1.nextInt(99 - 10+1)+10));
}
cypherList.addFirst(""+(rand1.nextInt(999 - 100 +1 )+100));
cypherList.add(codeCase+rand1.nextInt(10));
return swapper(cypherList);
}
/**
* This method will unscramble and rebuild a scrambled string
* @param cypherList
* @return
*/
private static String unScrambler(LinkedList<String> cypherList) {
StringBuilder stringBuilder = new StringBuilder();
LinkedList<String> cypherListOutput = new LinkedList<>();
String caseCode = cypherList.get(cypherList.size()-1);
for (int index = 0; index < cypherList.size()-1; index++) {
cypherListOutput.add(String.valueOf(cypherList.get(index).toCharArray()[2]));
if(index>=1)
stringBuilder.append(cypherListOutput.get(index));
continue;
}
char[] rawCode = (stringBuilder.toString()+"¤¤¤¤"+caseCode).toCharArray();
char[] unscrambled = new char[rawCode.length-1];
for(int i = 0; i<unscrambled.length; i++) {
unscrambled[i] = rawCode[i];
}
return String.valueOf(unscrambled);
}
/**
* Swaps the content of the array the as many times as
* the operation count.
* @param code
* @return
*/
private static LinkedList<String> swapper(LinkedList<String> code) {
LinkedList<String> output = new LinkedList<>();
LinkedList<char[]> chars = new LinkedList<>();
String[] swapArray = new String[code.size()];
for (int index = 0; index < code.size(); index++) {
swapArray[index] = code.get(index);
chars.add(swapArray[index].toCharArray());
}
for (int iteration = 0; iteration < operationCount; iteration++) {
for (int i = 1; i < chars.size() - 2; i++) {
for (int r = 0; r < chars.get(i).length - 1; r++) {
char tempChar = chars.get(i)[r];
chars.get(i)[r] = chars.get(i + 1)[r];
chars.get(i + 1)[r] = tempChar;
}
}
}
for (int i = 0; i < chars.size(); i++) {
output.add(String.valueOf(chars.get(i)));
}
return output;
}
/**
* unswapps the content of the array in order to make it
* ready to be deciphered
* @param code
* @return
*/
private static LinkedList<String> unswapper(LinkedList<String> code) {
LinkedList<String> output = new LinkedList<>();
LinkedList<char[]> chars = new LinkedList<>();
String[] swapArray = new String[code.size()];
for (int index = 0; index < code.size(); index++) {
swapArray[index] = code.get(index);
chars.add(swapArray[index].toCharArray());
}
for (int iteration = 0; iteration < operationCount; iteration++) {
for (int i = chars.size() - 3; i >= 1; i--) {
for (int r = chars.get(i).length - 2; r >= 0; r--) {
char tempChar = chars.get(i)[r];
chars.get(i)[r] = chars.get(i + 1)[r];
chars.get(i + 1)[r] = tempChar;
}
}
}
for (int i = 0; i < chars.size(); i++) {
output.add(String.valueOf(chars.get(i)));
}
return output;
}
/**
* This method will convert a string into case holder
* which hold which character is upper case and which
* isn't.
* @param message
* @return
*/
private static String convertCase(String message) {
char[] caseHolder = new char[message.length()];
for(int i = 0; i < caseHolder.length; i++) {
if (Character.isUpperCase(message.charAt(i))) {
caseHolder[i] = 'A';
} else if (Character.isLowerCase(message.charAt(i))) {
caseHolder[i] = 'a';
}
}
return String.valueOf(caseHolder);
}
/**
* This method will take a case code holder
* and apply the case to the given message
* @param caseBank
* @param message
* @return
*/
private static String caseConverter(String caseBank,String message){
StringBuilder input = new StringBuilder(caseBank);
StringBuilder output = new StringBuilder(message);
for (int index = 0; index < input.length(); index++) {
char current = input.charAt(index);
char formatedString = output.charAt(index);
if (Character.isLowerCase(current)) {
output.setCharAt(index, Character.toLowerCase(formatedString));
} else {
output.setCharAt(index, Character.toUpperCase(formatedString));
}
}
return output.toString();
}
private static String printBytes(byte[] binaryEncription){
return new String(binaryEncription, Charset.forName("UTF-8"));
}
private static LinkedList<String> fromStringToList(String list){
String[] newList = list.split(", ");
LinkedList<String> code = new LinkedList<String>();
for(int i = 0; i<newList.length; i++){
code.add(newList[i]);
}
return code;
}
/**
* Can be used to further increase the level of
* encryption;
* @param encryption
* @return
*/
private static byte[] byteSwapper(byte[] encryption){
// TODO: write code here!!!
// Further swapping may not be needed!
return encryption;
}
/**
* To unswapp the byte array back to normal
* @param encryption
* @return
*/
private static byte[] byteUnswapper(byte[] encryption){
//TODO: write code here!
return encryption;
}
public static byte[] encrypt(String message){
String cypher = scrambler(message).toString();
byte[] encryption = cypher.getBytes(Charset.forName("UTF-8"));
System.out.println(printBytes(encryption));
byte[] swappedBytes = System.out.println(Arrays.toStringbyteSwapper(encryption));
return encryption;swappedBytes;
}
public static String decrypt(byte[] encryption){
byte[] unswappedBytes = byteUnswapper(encryption);
LinkedList<String> list = fromStringToList(new String(encryptionunswappedBytes, Charset.forName("UTF-8")));
LinkedList<String> unSwapped = Encrypter.unswapper(list);
String unScrambled = Encrypter.unScrambler(unSwapped);
String decyphered = Encrypter.DecypherEncryption(unScrambled);
return decyphered;
}
private static LinkedList<String> fromStringToList(String list){
String[] newList = list.split(", ");
LinkedList<String> code = new LinkedList<String>();
for(int i = 0; i<newList.length; i++){
code.add(newList[i]);
}
return code;
}
public static void main(String[] args) {
System.out.println(Encrypter.caseConverter("AaaaAA", "eudycr"));
System.out.println(Encrypter.convertCase("Whats up man, how are you doing today?"));
System.out.println(Encrypter.obfuscate("Whats up man, how are you doing today?"));
System.out.println(Encrypter.scrambler("Whats up man, how are you doing today?"));
System.out.println(Encrypter.unswapper(Encrypter.scrambler("Whats up man, how are you doing today?")));
System.out.println(Encrypter.unScrambler(Encrypter.unswapper(Encrypter.scrambler("Whats up man, how are you doing today?"))));
System.out.println("");
System.out.println(Encrypter.DecypherEncryption(Encrypter.unScrambler(Encrypter.unswapper(Encrypter.scrambler("Whats up man, how are you doing today?")))));
System.out.println(Encrypter.DecypherEncryption("HIGYFS"));
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println(Encrypter.decrypt(byte[] encryption = Encrypter.encrypt("Whats up man, how are you doing today?");
System.out.println(printBytes(encryption));
System.out.println(Arrays.toString(encryption));
System.out.println(Encrypter.decrypt(encryption));
}
}
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Random;
/**
* This is a class which can be used to encrypt any sort of string,
* the class provides a simple encryption program which uses a scrambler/adder/replacer/swapper.
* The encrypted message is sent as a byte array which can then be formated and decrypted.
* If the message is infiltrated while traveling through a socket the thief
* wont be able to see the content of the message without the key or this class.
*
* @author Eudy Contreras
*
*/
/*
private static final char[] plain = {'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','Ö','Ä','Å','0','1','2',
'3','4','5','6','7','8','9'};
private static final char[] key = {'D','E','F','G','H','Ä','U','J',
'A','L','Z','Å','N','C','P','Q',
'W','S','T','Ö','I','V','R','X',
'Y','M','K','B','O','4','6','1',
'8','3','2','9','0','5','7'};
*/
public class Encrypter {
private static final char[] added ={'L','E','Z','G','H','Ä','U','J',
'A','D','F','Å','N','C','M','Y',
'W','S','B','Ö','I','V','R','1',
'Q','P','K','T','O','4','6','X',
'8','3','2','9','0','5','7'};
private static final int operationCount = 16;
/**
* This method will will used a simple key
* and encrypt a message
* @param message
* @return
*/
private static String obfuscate(String message) {
String caseCode = convertCase(message);
char[] caseBinary = new char[message.length()];
for(int i = 0; i<caseCode.length(); i++) {
if(caseCode.charAt(i) == 'A') {
caseBinary[i] = '1';
}
else if(caseCode.charAt(i) == 'a') {
caseBinary[i] = '0';
}
}
char[] upperCaseMessage = new char[message.length()];
for(int i = 0; i<message.length(); i++) {
upperCaseMessage[i] = Character.toUpperCase(message.charAt(i));
}
char[] code = upperCaseMessage;
for (int i = 0; i < code.length; i++) {
if(code[i] == 'A') {code[i] = 'D';}
else if(code[i] == 'B') {code[i] = 'E';}
else if(code[i] == 'C') {code[i] = 'F';}
else if(code[i] == 'D') {code[i] = 'G';}
else if(code[i] == 'E') {code[i] = 'H';}
else if(code[i] == 'F') {code[i] = 'Ä';}
else if(code[i] == 'G') {code[i] = 'U';}
else if(code[i] == 'H') {code[i] = 'J';}
else if(code[i] == 'I') {code[i] = 'A';}
else if(code[i] == 'J') {code[i] = 'L';}
else if(code[i] == 'K') {code[i] = 'Z';}
else if(code[i] == 'L') {code[i] = 'Å';}
else if(code[i] == 'M') {code[i] = 'N';}
else if(code[i] == 'N') {code[i] = 'C';}
else if(code[i] == 'O') {code[i] = 'P';}
else if(code[i] == 'P') {code[i] = 'Q';}
else if(code[i] == 'Q') {code[i] = 'W';}
else if(code[i] == 'R') {code[i] = 'S';}
else if(code[i] == 'S') {code[i] = 'T';}
else if(code[i] == 'T') {code[i] = 'Ö';}
else if(code[i] == 'U') {code[i] = 'I';}
else if(code[i] == 'V') {code[i] = 'V';}
else if(code[i] == 'W') {code[i] = 'R';}
else if(code[i] == 'X') {code[i] = 'S';}
else if(code[i] == 'Y') {code[i] = 'Y';}
else if(code[i] == 'Z') {code[i] = 'M';}
else if(code[i] == 'Ö') {code[i] = 'K';}
else if(code[i] == 'Ä') {code[i] = 'B';}
else if(code[i] == 'Å') {code[i] = 'O';}
else if(code[i] == '0') {code[i] = '4';}
else if(code[i] == '1') {code[i] = '6';}
else if(code[i] == '2') {code[i] = '1';}
else if(code[i] == '3') {code[i] = '8';}
else if(code[i] == '4') {code[i] = '3';}
else if(code[i] == '5'){code[i] = '2';}
else if(code[i] == '6') {code[i] = '9';}
else if(code[i] == '7') {code[i] = '0';}
else if(code[i] == '8') {code[i] = '5';}
else if(code[i] == '9') {code[i] = '7';}
else {code[i] = code[i];}
}
return String.valueOf(code)+"¤¤¤¤"+String.valueOf(caseBinary);
}
/**
* this method will decypher and encrypted string message
* @param message
* @return
*/
private static String DecypherEncryption(String message) {
String[] code_and_case = message.split("¤¤¤¤");
String codeMessage;
String caseMessage;
if(code_and_case.length == 2) {
codeMessage = code_and_case[0];
caseMessage = code_and_case[1];
}
else {
codeMessage = message;
caseMessage = null;
}
char[] code = codeMessage.toCharArray();
for (int i = 0; i < code.length; i++) {
if (code[i] == 'D') {code[i] = 'A';}
else if(code[i] == 'E') {code[i] = 'B';}
else if(code[i] == 'F') {code[i] = 'C';}
else if(code[i] == 'G') {code[i] = 'D';}
else if(code[i] == 'H') {code[i] = 'E';}
else if(code[i] == 'Ä') {code[i] = 'F';}
else if(code[i] == 'U') {code[i] = 'G';}
else if(code[i] == 'J') {code[i] = 'H';}
else if(code[i] == 'A') {code[i] = 'I';}
else if(code[i] == 'L') {code[i] = 'J';}
else if(code[i] == 'Z') {code[i] = 'K';}
else if(code[i] == 'Å') {code[i] = 'L';}
else if(code[i] == 'N') {code[i] = 'M';}
else if(code[i] == 'C') {code[i] = 'N';}
else if(code[i] == 'P') {code[i] = 'O';}
else if(code[i] == 'Q') {code[i] = 'P';}
else if(code[i] == 'W') {code[i] = 'Q';}
else if(code[i] == 'S') {code[i] = 'R';}
else if(code[i] == 'T') {code[i] = 'S';}
else if(code[i] == 'Ö') {code[i] = 'T';}
else if(code[i] == 'I') {code[i] = 'U';}
else if(code[i] == 'V') {code[i] = 'V';}
else if(code[i] == 'R') {code[i] = 'W';}
else if(code[i] == 'S') {code[i] = 'X';}
else if(code[i] == 'Y') {code[i] = 'Y';}
else if(code[i] == 'M') {code[i] = 'Z';}
else if(code[i] == 'K') {code[i] = 'Ö';}
else if(code[i] == 'B') {code[i] = 'Ä';}
else if(code[i] == 'O') {code[i] = 'Å';}
else if(code[i] == '4') {code[i] = '0';}
else if(code[i] == '6') {code[i] = '1';}
else if(code[i] == '1') {code[i] = '2';}
else if(code[i] == '8') {code[i] = '3';}
else if(code[i] == '3') {code[i] = '4';}
else if(code[i] == '2'){code[i] = '5';}
else if(code[i] == '9') {code[i] = '6';}
else if(code[i] == '0') {code[i] = '7';}
else if(code[i] == '5') {code[i] = '8';}
else if(code[i] == '7') {code[i] = '9';}
else {code[i] = code[i];}
}
if (code_and_case.length == 2) {
for (int i = 0; i < caseMessage.length()-1; i++) {
if (caseMessage.charAt(i) == '1') {
code[i] = Character.toUpperCase(code[i]);
} else if (caseMessage.charAt(i) == '0') {
code[i] = Character.toLowerCase(code[i]);
}
}
}
return String.valueOf(code);
}
/**
* Further increases the encryption level of the message by adding random numbers and characters
* to the already encrypted message the process can then be reversed with an additional key.
* @param code: the encrypted message
* @return
*/
private static LinkedList<String> scrambler(String code) {
Random rand1 = new Random();
Random rand2 = new Random();
Random rand3 = new Random();
Random rand4 = new Random();
String encryptedCode = Encrypter.obfuscate(code);
String[] case_and_code = encryptedCode.split("¤¤¤¤");
String cypher = case_and_code[0];
String codeCase = case_and_code[1];
LinkedList<String> cypherList = new LinkedList<>();
char[] cypherCharArr = cypher.toCharArray();
for (int index = 0; index < cypherCharArr.length; index++){
cypherList.add(added[rand3.nextInt(added.length)]+""+added[rand2.nextInt(added.length)]+String.valueOf(cypherCharArr[index]) + added[rand4.nextInt(added.length)]+(rand1.nextInt(99 - 10+1)+10));
}
cypherList.addFirst(""+(rand1.nextInt(999 - 100 +1 )+100));
cypherList.add(codeCase+rand1.nextInt(10));
return swapper(cypherList);
}
/**
* This method will unscramble and rebuild a scrambled string
* @param cypherList
* @return
*/
private static String unScrambler(LinkedList<String> cypherList) {
StringBuilder stringBuilder = new StringBuilder();
LinkedList<String> cypherListOutput = new LinkedList<>();
String caseCode = cypherList.get(cypherList.size()-1);
for (int index = 0; index < cypherList.size()-1; index++) {
cypherListOutput.add(String.valueOf(cypherList.get(index).toCharArray()[2]));
if(index>=1)
stringBuilder.append(cypherListOutput.get(index));
continue;
}
char[] rawCode = (stringBuilder.toString()+"¤¤¤¤"+caseCode).toCharArray();
char[] unscrambled = new char[rawCode.length-1];
for(int i = 0; i<unscrambled.length; i++) {
unscrambled[i] = rawCode[i];
}
return String.valueOf(unscrambled);
}
/**
* Swaps the content of the array the as many times as
* the operation count.
* @param code
* @return
*/
private static LinkedList<String> swapper(LinkedList<String> code) {
LinkedList<String> output = new LinkedList<>();
LinkedList<char[]> chars = new LinkedList<>();
String[] swapArray = new String[code.size()];
for (int index = 0; index < code.size(); index++) {
swapArray[index] = code.get(index);
chars.add(swapArray[index].toCharArray());
}
for (int iteration = 0; iteration < operationCount; iteration++) {
for (int i = 1; i < chars.size() - 2; i++) {
for (int r = 0; r < chars.get(i).length - 1; r++) {
char tempChar = chars.get(i)[r];
chars.get(i)[r] = chars.get(i + 1)[r];
chars.get(i + 1)[r] = tempChar;
}
}
}
for (int i = 0; i < chars.size(); i++) {
output.add(String.valueOf(chars.get(i)));
}
return output;
}
/**
* unswapps the content of the array in order to make it
* ready to be deciphered
* @param code
* @return
*/
private static LinkedList<String> unswapper(LinkedList<String> code) {
LinkedList<String> output = new LinkedList<>();
LinkedList<char[]> chars = new LinkedList<>();
String[] swapArray = new String[code.size()];
for (int index = 0; index < code.size(); index++) {
swapArray[index] = code.get(index);
chars.add(swapArray[index].toCharArray());
}
for (int iteration = 0; iteration < operationCount; iteration++) {
for (int i = chars.size() - 3; i >= 1; i--) {
for (int r = chars.get(i).length - 2; r >= 0; r--) {
char tempChar = chars.get(i)[r];
chars.get(i)[r] = chars.get(i + 1)[r];
chars.get(i + 1)[r] = tempChar;
}
}
}
for (int i = 0; i < chars.size(); i++) {
output.add(String.valueOf(chars.get(i)));
}
return output;
}
/**
* This method will convert a string into case holder
* which hold which character is upper case and which
* isn't.
* @param message
* @return
*/
private static String convertCase(String message) {
char[] caseHolder = new char[message.length()];
for(int i = 0; i < caseHolder.length; i++) {
if (Character.isUpperCase(message.charAt(i))) {
caseHolder[i] = 'A';
} else if (Character.isLowerCase(message.charAt(i))) {
caseHolder[i] = 'a';
}
}
return String.valueOf(caseHolder);
}
/**
* This method will take a case code holder
* and apply the case to the given message
* @param caseBank
* @param message
* @return
*/
private static String caseConverter(String caseBank,String message){
StringBuilder input = new StringBuilder(caseBank);
StringBuilder output = new StringBuilder(message);
for (int index = 0; index < input.length(); index++) {
char current = input.charAt(index);
char formatedString = output.charAt(index);
if (Character.isLowerCase(current)) {
output.setCharAt(index, Character.toLowerCase(formatedString));
} else {
output.setCharAt(index, Character.toUpperCase(formatedString));
}
}
return output.toString();
}
private static String printBytes(byte[] binaryEncription){
return new String(binaryEncription, Charset.forName("UTF-8"));
}
public static byte[] encrypt(String message){
String cypher = scrambler(message).toString();
byte[] encryption = cypher.getBytes(Charset.forName("UTF-8"));
System.out.println(printBytes(encryption));
System.out.println(Arrays.toString(encryption));
return encryption;
}
public static String decrypt(byte[] encryption){
LinkedList<String> list = fromStringToList(new String(encryption, Charset.forName("UTF-8")));
LinkedList<String> unSwapped = Encrypter.unswapper(list);
String unScrambled = Encrypter.unScrambler(unSwapped);
String decyphered = Encrypter.DecypherEncryption(unScrambled);
return decyphered;
}
private static LinkedList<String> fromStringToList(String list){
String[] newList = list.split(", ");
LinkedList<String> code = new LinkedList<String>();
for(int i = 0; i<newList.length; i++){
code.add(newList[i]);
}
return code;
}
public static void main(String[] args) {
System.out.println(Encrypter.caseConverter("AaaaAA", "eudycr"));
System.out.println(Encrypter.convertCase("Whats up man, how are you doing today?"));
System.out.println(Encrypter.obfuscate("Whats up man, how are you doing today?"));
System.out.println(Encrypter.scrambler("Whats up man, how are you doing today?"));
System.out.println(Encrypter.unswapper(Encrypter.scrambler("Whats up man, how are you doing today?")));
System.out.println(Encrypter.unScrambler(Encrypter.unswapper(Encrypter.scrambler("Whats up man, how are you doing today?"))));
System.out.println("");
System.out.println(Encrypter.DecypherEncryption(Encrypter.unScrambler(Encrypter.unswapper(Encrypter.scrambler("Whats up man, how are you doing today?")))));
System.out.println(Encrypter.DecypherEncryption("HIGYFS"));
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println(Encrypter.decrypt(Encrypter.encrypt("Whats up man, how are you doing today?")));
}
}
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Random;
/**
* This is a class which can be used to encrypt any sort of string,
* the class provides a simple encryption program which uses a scrambler/adder/replacer/swapper.
* The encrypted message is sent as an object which can then be casted
* into a String LinkedList or a String array which can then be formated and decrypted.
* If the message is infiltrated while traveling through a socket the thief
* wont be able to see the content of the message without the key or this class.
*
* @author Eudy Contreras
*
*/
public class Encrypter {
private static final char[] plain ={'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','Ö','Ä','Å','0','1','2',
'3','4','5','6','7','8','9'};
private static final char[] key =
{'D','E','F','G','H','Ä','U','J',
'A','L','Z','Å','N','C','P','Q',
'W','S','T','Ö','I','V','R','X',
'Y','M','K','B','O','4','6','1',
'8','3','2','9','0','5','7'};
private static final char[] added =
{'L','E','Z','G','H','Ä','U','J',
'A','D','F','Å','N','C','M','Y',
'W','S','B','Ö','I','V','R','1',
'Q','P','K','T','O','4','6','X',
'8','3','2','9','0','5','7'};
private static final Map<Character, Character> plainMap;
static
{
plainMap = new HashMap<Character, Character>();
for(int i = 0; i<plain.length; i++){
plainMap.put(plain[i],key[i]);
}
}
private static final Map<Character, Character> keyMap;
static
{
keyMap = new HashMap<Character, Character>();
for(int i = 0; i<key.length; i++){
keyMap.put(key[i],plain[i]);
}
}
private static final int operationCount = 16;
/**
* This method will will used a simple key
* and encrypt a message
* @param message
* @return
*/
private static String obfuscate(String message) {
String caseCode = convertCase(message);
char[] caseBinary = new char[message.length()];
for(int i = 0; i<caseCode.length(); i++) {
if(caseCode.charAt(i) == 'A') {
caseBinary[i] = '1';
}
else if(caseCode.charAt(i) == 'a') {
caseBinary[i] = '0';
}
}
char[] upperCaseMessage = new char[message.length()];
for(int i = 0; i<message.length(); i++) {
upperCaseMessage[i] = Character.toUpperCase(message.charAt(i));
}
char[] code = upperCaseMessage;
for (int i = 0; i < code.length; i++) {
if(plainMap.containsKey(code[i])){
code[i] = plainMap.get(code[i]);
}
else {code[i] = code[i];}
}
return String.valueOf(code)+"¤¤¤¤"+String.valueOf(caseBinary);
}
/**
* this method will decypher and encrypted string message
* @param message
* @return
*/
private static String DecypherEncryption(String message) {
String[] code_and_case = message.split("¤¤¤¤");
String codeMessage;
String caseMessage;
if(code_and_case.length == 2) {
codeMessage = code_and_case[0];
caseMessage = code_and_case[1];
}
else {
codeMessage = message;
caseMessage = null;
}
char[] code = codeMessage.toCharArray();
for (int i = 0; i < code.length; i++) {
if(keyMap.containsKey(code[i])){
code[i] = keyMap.get(code[i]);
}
else {code[i] = code[i];}
}
if (code_and_case.length == 2) {
for (int i = 0; i < caseMessage.length()-1; i++) {
if (caseMessage.charAt(i) == '1') {
code[i] = Character.toUpperCase(code[i]);
} else if (caseMessage.charAt(i) == '0') {
code[i] = Character.toLowerCase(code[i]);
}
}
}
return String.valueOf(code);
}
/**
* Further increases the encryption level of the message by adding random numbers and characters
* to the already encrypted message the process can then be reversed with an additional key.
* @param code: the encrypted message
* @return
*/
private static LinkedList<String> scrambler(String code) {
Random rand1 = new Random();
Random rand2 = new Random();
Random rand3 = new Random();
Random rand4 = new Random();
String encryptedCode = Encrypter.obfuscate(code);
String[] case_and_code = encryptedCode.split("¤¤¤¤");
String cypher = case_and_code[0];
String codeCase = case_and_code[1];
LinkedList<String> cypherList = new LinkedList<>();
char[] cypherCharArr = cypher.toCharArray();
for (int index = 0; index < cypherCharArr.length; index++){
cypherList.add(added[rand3.nextInt(added.length)]+""+added[rand2.nextInt(added.length)]+String.valueOf(cypherCharArr[index]) + added[rand4.nextInt(added.length)]+(rand1.nextInt(99 - 10+1)+10));
}
cypherList.addFirst(""+(rand1.nextInt(999 - 100 +1 )+100));
cypherList.add(codeCase+rand1.nextInt(10));
return swapper(cypherList);
}
/**
* This method will unscramble and rebuild a scrambled string
* @param cypherList
* @return
*/
private static String unScrambler(LinkedList<String> cypherList) {
StringBuilder stringBuilder = new StringBuilder();
LinkedList<String> cypherListOutput = new LinkedList<>();
String caseCode = cypherList.get(cypherList.size()-1);
for (int index = 0; index < cypherList.size()-1; index++) {
cypherListOutput.add(String.valueOf(cypherList.get(index).toCharArray()[2]));
if(index>=1)
stringBuilder.append(cypherListOutput.get(index));
continue;
}
char[] rawCode = (stringBuilder.toString()+"¤¤¤¤"+caseCode).toCharArray();
char[] unscrambled = new char[rawCode.length-1];
for(int i = 0; i<unscrambled.length; i++) {
unscrambled[i] = rawCode[i];
}
return String.valueOf(unscrambled);
}
/**
* Swaps the content of the array the as many times as
* the operation count.
* @param code
* @return
*/
private static LinkedList<String> swapper(LinkedList<String> code) {
LinkedList<String> output = new LinkedList<>();
LinkedList<char[]> chars = new LinkedList<>();
String[] swapArray = new String[code.size()];
for (int index = 0; index < code.size(); index++) {
swapArray[index] = code.get(index);
chars.add(swapArray[index].toCharArray());
}
for (int iteration = 0; iteration < operationCount; iteration++) {
for (int i = 1; i < chars.size() - 2; i++) {
for (int r = 0; r < chars.get(i).length - 1; r++) {
char tempChar = chars.get(i)[r];
chars.get(i)[r] = chars.get(i + 1)[r];
chars.get(i + 1)[r] = tempChar;
}
}
}
for (int i = 0; i < chars.size(); i++) {
output.add(String.valueOf(chars.get(i)));
}
return output;
}
/**
* unswapps the content of the array in order to make it
* ready to be deciphered
* @param code
* @return
*/
private static LinkedList<String> unswapper(LinkedList<String> code) {
LinkedList<String> output = new LinkedList<>();
LinkedList<char[]> chars = new LinkedList<>();
String[] swapArray = new String[code.size()];
for (int index = 0; index < code.size(); index++) {
swapArray[index] = code.get(index);
chars.add(swapArray[index].toCharArray());
}
for (int iteration = 0; iteration < operationCount; iteration++) {
for (int i = chars.size() - 3; i >= 1; i--) {
for (int r = chars.get(i).length - 2; r >= 0; r--) {
char tempChar = chars.get(i)[r];
chars.get(i)[r] = chars.get(i + 1)[r];
chars.get(i + 1)[r] = tempChar;
}
}
}
for (int i = 0; i < chars.size(); i++) {
output.add(String.valueOf(chars.get(i)));
}
return output;
}
/**
* This method will convert a string into case holder
* which hold which character is upper case and which
* isn't.
* @param message
* @return
*/
private static String convertCase(String message) {
char[] caseHolder = new char[message.length()];
for(int i = 0; i < caseHolder.length; i++) {
if (Character.isUpperCase(message.charAt(i))) {
caseHolder[i] = 'A';
} else if (Character.isLowerCase(message.charAt(i))) {
caseHolder[i] = 'a';
}
}
return String.valueOf(caseHolder);
}
/**
* This method will take a case code holder
* and apply the case to the given message
* @param caseBank
* @param message
* @return
*/
private static String caseConverter(String caseBank,String message){
StringBuilder input = new StringBuilder(caseBank);
StringBuilder output = new StringBuilder(message);
for (int index = 0; index < input.length(); index++) {
char current = input.charAt(index);
char formatedString = output.charAt(index);
if (Character.isLowerCase(current)) {
output.setCharAt(index, Character.toLowerCase(formatedString));
} else {
output.setCharAt(index, Character.toUpperCase(formatedString));
}
}
return output.toString();
}
private static String printBytes(byte[] binaryEncription){
return new String(binaryEncription, Charset.forName("UTF-8"));
}
private static LinkedList<String> fromStringToList(String list){
String[] newList = list.split(", ");
LinkedList<String> code = new LinkedList<String>();
for(int i = 0; i<newList.length; i++){
code.add(newList[i]);
}
return code;
}
/**
* Can be used to further increase the level of
* encryption;
* @param encryption
* @return
*/
private static byte[] byteSwapper(byte[] encryption){
// TODO: write code here!!!
// Further swapping may not be needed!
return encryption;
}
/**
* To unswapp the byte array back to normal
* @param encryption
* @return
*/
private static byte[] byteUnswapper(byte[] encryption){
//TODO: write code here!
return encryption;
}
public static byte[] encrypt(String message){
String cypher = scrambler(message).toString();
byte[] encryption = cypher.getBytes(Charset.forName("UTF-8"));
byte[] swappedBytes = byteSwapper(encryption);
return swappedBytes;
}
public static String decrypt(byte[] encryption){
byte[] unswappedBytes = byteUnswapper(encryption);
LinkedList<String> list = fromStringToList(new String(unswappedBytes, Charset.forName("UTF-8")));
LinkedList<String> unSwapped = Encrypter.unswapper(list);
String unScrambled = Encrypter.unScrambler(unSwapped);
String decyphered = Encrypter.DecypherEncryption(unScrambled);
return decyphered;
}
public static void main(String[] args) {
System.out.println(Encrypter.caseConverter("AaaaAA", "eudycr"));
System.out.println(Encrypter.convertCase("Whats up man, how are you doing today?"));
System.out.println(Encrypter.obfuscate("Whats up man, how are you doing today?"));
System.out.println(Encrypter.scrambler("Whats up man, how are you doing today?"));
System.out.println(Encrypter.unswapper(Encrypter.scrambler("Whats up man, how are you doing today?")));
System.out.println(Encrypter.unScrambler(Encrypter.unswapper(Encrypter.scrambler("Whats up man, how are you doing today?"))));
System.out.println("");
System.out.println(Encrypter.DecypherEncryption(Encrypter.unScrambler(Encrypter.unswapper(Encrypter.scrambler("Whats up man, how are you doing today?")))));
System.out.println(Encrypter.DecypherEncryption("HIGYFS"));
System.out.println("");
System.out.println("");
System.out.println("");
byte[] encryption = Encrypter.encrypt("Whats up man, how are you doing today?");
System.out.println(printBytes(encryption));
System.out.println(Arrays.toString(encryption));
System.out.println(Encrypter.decrypt(encryption));
}
}
Loading
Loading
lang-java