001 package com.valhalla.misc;
002
003 public class SimpleXOR {
004 public static String encrypt(String text, String key) {
005 if (text == null)
006 return "";
007 String result = "";
008 while (key.length() < text.length()) {
009 key += key;
010 }
011
012 key = key.substring(0, text.length());
013
014 byte[] t = text.getBytes();
015 byte[] k = key.getBytes();
016
017 for (int i = 0; i < t.length; i++) {
018 int e = (int) (t[i] ^ k[i]);
019 String hex = Integer.toHexString(e);
020 result += " " + hex;
021 }
022
023 return result.substring(1);
024 }
025
026 public static String decrypt(String text, String key) {
027 if (text == null)
028 return "";
029 String[] ar = text.split(" ");
030 while (key.length() < ar.length) {
031 key += key;
032 }
033
034 key = key.substring(0, ar.length);
035 String result = "";
036
037 byte[] t = new byte[ar.length];
038 for (int i = 0; i < ar.length; i++) {
039 try {
040 t[i] = (byte) Integer.parseInt(ar[i], 16);
041 } catch (NumberFormatException ex) {
042 return "";
043 }
044 }
045
046 byte[] k = key.getBytes();
047
048 for (int i = 0; i < t.length; i++) {
049 int e = (int) (t[i] ^ k[i]);
050
051 result += (char) e;
052 }
053
054 return result;
055 }
056 }