0

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));
    }
4

2 回答 2

2

通常总是使用getBytesandnew String和一个编码参数。否则使用默认平台编码。并用两台电脑...

备注: value = new String(value);可以删除,因为 String 对象是(和)不可变的。

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

应该是(与 兼容getBytes("ISO-8859-1")):

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

这减少到:

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

因此

new String(enc)

应该:

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

一般来说,什么都没有做。

于 2014-01-23T13:52:53.197 回答
0

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

于 2014-01-23T13:41:28.453 回答