simplexor.java

来自「网站即时通讯系统」· Java 代码 · 共 56 行

JAVA
56
字号
package com.valhalla.misc;public class SimpleXOR {    public static String encrypt(String text, String key) {        if (text == null)            return "";        String result = "";        while (key.length() < text.length()) {            key += key;        }        key = key.substring(0, text.length());        byte[] t = text.getBytes();        byte[] k = key.getBytes();        for (int i = 0; i < t.length; i++) {            int e = (int) (t[i] ^ k[i]);            String hex = Integer.toHexString(e);            result += " " + hex;        }        return result.substring(1);    }    public static String decrypt(String text, String key) {        if (text == null)            return "";        String[] ar = text.split(" ");        while (key.length() < ar.length) {            key += key;        }        key = key.substring(0, ar.length);        String result = "";        byte[] t = new byte[ar.length];        for (int i = 0; i < ar.length; i++) {            try {                t[i] = (byte) Integer.parseInt(ar[i], 16);            } catch (NumberFormatException ex) {                return "";            }        }        byte[] k = key.getBytes();        for (int i = 0; i < t.length; i++) {            int e = (int) (t[i] ^ k[i]);            result += (char) e;        }        return result;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?