Last active
December 1, 2017 14:02
-
-
Save ohad7/211221304b00257ecefd138376bb6e1e to your computer and use it in GitHub Desktop.
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
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