-
-
Save amayatsky/60cadd851f6abf5fd23100b6d3b3ba16 to your computer and use it in GitHub Desktop.
Simple yet effective XOR encryption
This file contains 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
private static int[] encrypt(String str, String key) { | |
int[] output = new int[str.length()]; | |
for(int i = 0; i < str.length(); i++) { | |
int o = ((int) str.charAt(i) ^ (int) key.charAt(i % (key.length() - 1))) + (char)('0'); | |
output[i] = o; | |
} | |
return output; | |
} |
This file contains 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
/** | |
* @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 anEncrypted : encrypted) System.out.printf("%d,", anEncrypted); | |
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 = ((int) str.charAt(i) ^ (int) 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) { | |
StringBuilder output = new StringBuilder(); | |
for(int i = 0; i < input.length; i++) { | |
output.append((char) ((input[i] - 48) ^ (int) key.charAt(i % (key.length() - 1)))); | |
} | |
return output.toString(); | |
} | |
} |
This file contains 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
package app.cind.android.service.payment | |
/** | |
* @author Prem Nirmal | |
*/ | |
object XORCrypt { | |
private var value = "SampleStringToBeEncrypted" | |
private var keyval = "thisIsAKey" | |
@JvmStatic | |
fun main() { // test | |
val encrypted = encrypt(value, keyval) | |
for (anEncrypted in encrypted) System.out.printf("%d,", anEncrypted) | |
println("") | |
println(decrypt(encrypted, keyval)) | |
} | |
private fun encrypt(str: String, key: String): IntArray { | |
val output = IntArray(str.length) | |
for (i in 0 until str.length) { | |
val o = (str[i].toInt() xor key[i % (key.length - 1)].toInt()) + '0'.toInt() | |
output[i] = o | |
} | |
return output | |
} | |
private fun string2Arr(str: String): IntArray { | |
val sarr = str.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() | |
val out = IntArray(sarr.size) | |
for (i in out.indices) { | |
out[i] = Integer.valueOf(sarr[i]) | |
} | |
return out | |
} | |
private fun decrypt(input: IntArray, key: String): String { | |
val output = StringBuilder() | |
for (i in input.indices) { | |
output.append((input[i] - 48 xor key[i % (key.length - 1)].toInt()).toChar()) | |
} | |
return output.toString() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment