Skip to content

Instantly share code, notes, and snippets.

@kcak11
Last active June 18, 2021 11:02
Show Gist options
  • Save kcak11/e86dc49ad8ca41cb6da21f51a0a6b1ba to your computer and use it in GitHub Desktop.
Save kcak11/e86dc49ad8ca41cb6da21f51a0a6b1ba to your computer and use it in GitHub Desktop.
StringObfuscator

StringObfuscator

package com.string.utils;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Base64;
public class StringObfuscator {
private StringObfuscator() {}
public static String obfuscate(String input) {
String b64str = Base64.getEncoder().encodeToString(encodeURIComponent(input.toLowerCase()).getBytes());
b64str = b64str.replace("+", "").replace("=", "").replace("/", "");
Integer sum = 0;
int index = 0;
for (int i = 0; i < b64str.length() - 4; i += 4) {
sum += Integer.parseInt(b64str.substring(i, i + 4), 36);
index = i + 4;
}
sum += Integer.parseInt(b64str.substring(index), 36);
return Integer.toString(sum, 36);
}
public static String encodeURIComponent(String s) {
String result = null;
try {
result = URLEncoder.encode(s, "UTF-8").replaceAll("\\+", "%20").replaceAll("\\%21", "!").replaceAll("\\%27", "'").replaceAll("\\%28", "(").replaceAll("\\%29", ")").replaceAll("\\%7E", "~");
} catch (UnsupportedEncodingException e) {
result = s;
}
return result;
}
}
function obfuscate(str) {
if (!str) {
throw new Error("Invalid string input specified.");
}
let _str = btoa(encodeURIComponent(str.toLowerCase()));
_str = _str.replace(/\//g, "").replace(/\+/g, "").replace(/\=/g, "");
let _sum = 0;
for (let i = 0; i < _str.length; i += 4) {
_sum += parseInt(_str.substr(i, 4), 36);
}
return _sum.toString(36);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment