在Java上解密XOR

时间:2015-03-15 11:52:59

标签: java encryption xor

任何人都可以帮我为我的XOR加密器编写解密方法。那我怎么加密:

我正在制作XOR

public static String idEncrypt(String id, String key)throws Exception
{
    if (id == null)
        return null;
    if (key.length() == 0)
        return id;
    String utfID="", utfKey="";
    try
    {
        utfID = new String(id.getBytes(), "UTF-8");
        utfKey = new String(key.getBytes(), "UTF-8");
    }
    catch (UnsupportedEncodingException e)
    {
        Log.e("utf8", "conversion", e);
    }
    StringBuilder sb = new StringBuilder();
    for(int i = 0; i < utfID.length(); i++)
        sb.append((char)(utfID.charAt(i) ^ utfKey.charAt(i % utfKey.length())));
    String result = sb.toString();

    return toHex(result.getBytes());
}

将字节转换为十六进制:

public static String toHex(byte[] buf) {
    if (buf == null)
        return "";
    StringBuffer result = new StringBuffer(2*buf.length);
    for (int i = 0; i < buf.length; i++) {
        appendHex(result, buf[i]);
    }
    return result.toString();
}

private final static String HEX = "0123456789abcdef";

private static void appendHex(StringBuffer sb, byte b) {
    sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
}

实际上我所有尝试解密此隐藏的结果都失败了=( 可能有些人在解释我如何使用它?

P.S:我尝试的结果是像“X?¾RkÖ_êQ”这样的字符串,但必须是数字和字母的16个符号。

UPD: 我的fromHEX()方法

 public static String fromHex(String hex) {
        return new String(toByte(hex));
    }

    public static byte[] toByte(String hexString) {
        int len = hexString.length()/2;
        byte[] result = new byte[len];
        for (int i = 0; i < len; i++)
            result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
        return result;
    }

0 个答案:

没有答案