Skip to content

Instantly share code, notes, and snippets.

@culmat
Last active August 4, 2021 07:19
Show Gist options
  • Save culmat/966df39f9b7e44bf4f42 to your computer and use it in GitHub Desktop.
Save culmat/966df39f9b7e44bf4f42 to your computer and use it in GitHub Desktop.
package common;
import java.util.Base64;
import java.util.Random;
public class Crypto {
public static String base64encode(String text) {
return Base64.getEncoder().encodeToString(text.getBytes());
}
public static String base64decode(String text) {
return new String(Base64.getDecoder().decode(text));
}
public static String encrypt(String message, long seed) {
return base64encode(xorMessage(message, seed));
}
public static String decrypt(String message, long seed) {
return xorMessage(base64decode(message), seed);
}
private static char rndChar(Random r) {
int rnd = (int) (r.nextDouble() * 52);
char base = (rnd < 26) ? 'A' : 'a';
return (char) (base + rnd % 26);
}
public static String xorMessage(String message, long seed) {
try {
if (message == null)
return null;
char[] mesg = message.toCharArray();
Random r = new Random(seed);
for (int i = 0; i < mesg.length; i++) {
mesg[i] = (char) (mesg[i] ^ rndChar(r));
}
return new String(mesg);
} catch (Exception e) {
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment