开发者

Using XOR on Shift-JIS characters in Java

So I'm trying to write a little decryption program but I'm running into a little trouble. I'm applying XOR to the characters with 'FF' (reversing all the bits) and I'm doing that by converting the string to a byte array then applying the XOR to it. But the characters are in Shift-JIS encoding and something's not working. When I try the method with normal letters, it seems to work but when it gets to the Japanese characters something goes wrong.

public void sampleMethod(String a)
    {
       try {
        String b = "FF";
        byte[] c = a.getBytes("Shift_JIS");
        byte[] d = b.getBytes("Shift_JIS");
        byte[] e = new byte[50];
        for (int i=0; i<c.length; i++)
        {
            e[i] =(byte)(c[i]^d[i%2]);
        }
        String t = new String(e, "Shift_JIS");
        System.out.println(t);
    }
       catch (UnsupportedEncodingException e)
       {
        }

    }

But when I stick in Japanese characters, it converts every single one of them into just 'yyyyyy'. I tried printing out the byte array to see the problem, and it showed that each character was being stored as '63'. How would I get the characters to be stored correctly? Actually, how woul开发者_开发知识库d I use XOR on the Shift-JIS characters?

I'm using XOR because I basically just want to reverse the bits from say 0010 to 1101 then change it back to characters. Is that possible?

Thanks

For example, this was my input: '始めまして" and what I get out is: "yyyyy" And when I do something like "hello there" I get ".#**)f2.#4#"


You simply can't do this kind of byte wise manipulation on multi-byte characters.

Japanese characters (and other extended characters) are typically represented by a series of bytes. Changing these around is likely going to produce invalid sequences which can't be decoded properly (and I guess this is the results that you are seeing).

From the Wikipedia article, Shift JIS

only guarantees that the first byte will be high bit set (0x80–0xFF); the value of the second byte can be either high or low

I would imagine by XOR'ing you are breaking this guarantee.

If you want to reverse the bits and do it back again work with a byte[] data type internally and only turn it back to a string when you're sure it's a Shift JIS structured byte array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜