繁体   English   中英

在不同平台中将字节转换为字符串,反之亦然

[英]Conversion of Byte to String and vice versa in different platforms

我有两个应用程序在不同的服务器上运行,一个使用JDK 1.6,另一个使用JRockit。

我使用RC4算法来屏蔽字符串并发送到托管在不同服务器上的不同应用程序。

下面的程序可用于掩码和取消掩码,两个服务器都有相同的程序运行。

我曾尝试在服务器中加入“ISO-8859-1”编码格式,但它对我没有帮助。 解码值程序失败,并给出垃圾。 以前当我将这两个应用程序托管在同一台服务器中时,它正在运行并且没有任何问题。

以下是该计划......请帮忙......

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

通常总是使用带有编码参数的getBytesnew 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")

一般来说,没有做太多事情。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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