Base64编码+两串异或操作

来源:互联网 发布:leslie矩阵预测人口 编辑:程序博客网 时间:2024/06/06 09:12

使用到jar包:commons-codec.jar

/** * base64编码 *  * @param s * @param key * @return */public static String encode(String s, String key) {return base64Encode(xorWithKey(s.getBytes(), key.getBytes()));}/** * base64解码 *  * @param s * @param key * @return */public static String decode(String s, String key) {return new String(xorWithKey(base64Decode(s), key.getBytes()));}private static byte[] base64Decode(String s) {return Base64.decodeBase64(s);}private static String base64Encode(byte[] bytes) {byte[] encodeBase64 = Base64.encodeBase64(bytes);return new String(encodeBase64);}/** * 两个字符串异或 *  * @param a * @param key * @return */public static 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;}

参考:http://www.bluefaq.com/java/79964



0 0
原创粉丝点击