Skip to content

Instantly share code, notes, and snippets.

@ohad7
Last active December 1, 2017 14:02
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 ohad7/211221304b00257ecefd138376bb6e1e to your computer and use it in GitHub Desktop.
Save ohad7/211221304b00257ecefd138376bb6e1e to your computer and use it in GitHub Desktop.
package com.codus.hello;
import java.io.File;
import java.nio.charset.Charset;
import org.apache.commons.codec.binary.Base64;
import com.google.common.io.Files;
public class CodusCipher {
public static String encrypt(final String key, final String text) {
return Base64.encodeBase64String(xor(key, text.getBytes()));
}
public static String decrypt(final String key, final String hash) {
try {
return new String(xor(key, Base64.decodeBase64(hash.getBytes())), "UTF-8");
} catch (java.io.UnsupportedEncodingException ex) {
throw new IllegalStateException(ex);
}
}
private static byte[] xor(String key, final byte[] input) {
final byte[] output = new byte[input.length];
final byte[] secret = key.getBytes();
int spos = 0;
for (int pos = 0; pos < input.length; ++pos) {
output[pos] = (byte) (input[pos] ^ secret[spos]);
spos += 1;
if (spos >= secret.length) {
spos = 0;
}
}
return output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment