Skip to content

Instantly share code, notes, and snippets.

@premnirmal
Created September 26, 2014 02:03
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save premnirmal/ac6e37ce717982d2aa24 to your computer and use it in GitHub Desktop.
Save premnirmal/ac6e37ce717982d2aa24 to your computer and use it in GitHub Desktop.
Simple yet effective XOR encryption
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;
}
}
@devjva
Copy link

devjva commented Oct 1, 2018

nice

@pp0236
Copy link

pp0236 commented Jan 1, 2022

nice

@Juhnke057
Copy link

nice

@ppolyanskiyy
Copy link

nice

@masecla22
Copy link

nice

@MaximilianoAdaro
Copy link

nice

@Tigressive
Copy link

nice

@fireboy3100
Copy link

nice

@Ascazil
Copy link

Ascazil commented Jan 13, 2023

nice

@Slotharmy
Copy link

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment