简体   繁体   中英

Conversion of Byte to String and vice versa in different platforms

I have two applications running on different servers, one having JDK 1.6 and another having JRockit.

I am using RC4 algorithm to mask a string and send to the different application hosted in different server.

Below program can be used for mask and unmask, And both the servers have same program running.

I have tried putting "ISO-8859-1" encoding format in both the servers, but it didn't help me. While decoding the value program fails, and gives garbage. Previously when I had these two applications hosted in same server, it was working and had no issues.

Below is the program...please help...

    String prefix = "dEncrypt";

    if(null==value||value.length()<1){
        return value;
    }
    else{
        byte[] input = null;
        try {
            value = new String(value);
            //String value1 = new String(value,"UTF-8");
            input = URLDecoder.decode(value).getBytes("ISO-8859-1");
            for(int i =0 ;i<input.length ; i++)
                System.out.println("input=" + input[i]);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        byte[] key = null;
        try {
            key = "123456789123456789123456789".getBytes("ISO-8859-1");
            for(int i =0 ;i<key.length ; i++)
                System.out.println("key=" + key[i]);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        byte[] state = new byte[256];
        int x, y;

        for( int i = 0; i < state.length; i++ ) {
            state[i] = (byte) i;
            }
        x = 0;
        for( int i = 0; i < state.length; i++ ) {
            x = (x + key[i % key.length] + state[i]) & 0xFF;
            //System.out.println("x=" + x);
            byte swap = state[i];
            state[i] = state[x];
            state[x] = swap;
            }
        x = 0;y=0;
        byte[] output = new byte[input.length];
        for( int i = 0; i < input.length; i++ ) {
        x = (x + 1) % 256;
        y = (state[x] + y) & 0xFF;
        byte swap = state[x];
        state[x] = state[y];
        state[y] = swap;
        byte r = state[(state[x] + state[y]) & 0xFF];
        output[i] = (byte) (input[i] ^ r);
        System.out.println("output=" + output[i]);
        }

        try {
            //System.out.println(" New string " +URLEncoder.encode(new String(output,"UTF-16") ));
            byte [] enc = Charset.forName("ISO-8859-1").encode(new String(output)).array();
            System.out.println(Charset.isSupported("base64"));
            //Charset.
            System.out.println(new String(enc));
            System.out.println("URLEncoded1=" + URLEncoder.encode(new String(enc)));
            System.out.println("URLEncoded2=" + URLEncoder.encode(new String(output,"ISO-8859-1")));
            return URLEncoder.encode(new String(output,"ISO-8859-1"));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return URLEncoder.encode(new String(output));
    }

In general always use getBytes and new String with an encoding parameter. As otherwise the default platform encoding is used. And with two computers ...

Remark: value = new String(value); can be removed, as String objects are (as good as) immutable.

byte [] enc = Charset.forName("ISO-8859-1").encode(new String(output)).array();

Should be (to be compatible with getBytes("ISO-8859-1") ):

byte [] enc = Charset.forName("ISO-8859-1").encode(
        new String(output, "ISO-8859-1")).array();

which reduces to:

byte[] enc = output; // Or: Arrays.copyOf(output, output.length);

And hence

new String(enc)

should be:

new String(enc, "ISO-8859-1")

In general nothing much is done.

我不太确定你想要实现什么,但如果你只是想通过文本链接(http)发送字节(二进制数据),你可以使用UUEncode(或Base64,或yEnc)代替。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM