Created
September 26, 2014 02:03
-
-
Save premnirmal/ac6e37ce717982d2aa24 to your computer and use it in GitHub Desktop.
Simple yet effective XOR encryption
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
/** | |
* @author Prem Nirmal | |
*/ | |
public class XORCrypt { | |
static String value = "SampleStringToBeEncrypted"; | |
static String keyval = "thisIsAKey"; | |
public static void main(String args[]) { // test | |
int[] encrypted = encrypt(value,keyval); | |
for(int i = 0; i < encrypted.length; i++) | |
System.out.printf("%d,", encrypted[i]); | |
System.out.println(""); | |
System.out.println(decrypt(encrypted,keyval)); | |
} | |
private static int[] encrypt(String str, String key) { | |
int[] output = new int[str.length()]; | |
for(int i = 0; i < str.length(); i++) { | |
int o = (Integer.valueOf(str.charAt(i)) ^ Integer.valueOf(key.charAt(i % (key.length() - 1)))) + '0'; | |
output[i] = o; | |
} | |
return output; | |
} | |
private static int[] string2Arr(String str) { | |
String[] sarr = str.split(","); | |
int[] out = new int[sarr.length]; | |
for (int i = 0; i < out.length; i++) { | |
out[i] = Integer.valueOf(sarr[i]); | |
} | |
return out; | |
} | |
private static String decrypt(int[] input, String key) { | |
String output = ""; | |
for(int i = 0; i < input.length; i++) { | |
output += (char) ((input[i] - 48) ^ (int) key.charAt(i % (key.length() - 1))); | |
} | |
return output; | |
} | |
} |
nice
nice
nice
nice
nice
nice
nice
nice
nice
nice
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice