Skip to content

Instantly share code, notes, and snippets.

@erfanegtfi
Created April 13, 2018 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erfanegtfi/1d99c4ca88962fe23a7820c4a44dc0df to your computer and use it in GitHub Desktop.
Save erfanegtfi/1d99c4ca88962fe23a7820c4a44dc0df to your computer and use it in GitHub Desktop.
base64 Encode
public String encode(String s, String key) {
if (s != null)
return base64Encode(xorWithKey(s.getBytes(), key.getBytes()));
else
return null;
}
public String decode(String s, String key) {
if (s != null)
return new String(xorWithKey(base64Decode(s), key.getBytes()));
else
return null;
}
private byte[] xorWithKey(byte[] a, byte[] key) {
byte[] out = new byte[a.length];
for (int i = 0; i < a.length; i++) {
out[i] = (byte) (a[i] ^ key[i%key.length]);
}
return out;
}
private byte[] base64Decode(String s) {
try {
BASE64Decoder d = new BASE64Decoder();
return d.decodeBuffer(s);
} catch (IOException e) {throw new RuntimeException(e);}
}
private String base64Encode(byte[] bytes) {
BASE64Encoder enc = new BASE64Encoder();
return enc.encode(bytes).replaceAll("\\s", "");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment