Created
April 13, 2018 12:28
-
-
Save erfanegtfi/1d99c4ca88962fe23a7820c4a44dc0df to your computer and use it in GitHub Desktop.
base64 Encode
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
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