java - 在不同平台上字节到字符串的转换,反之亦然

标签 java string arrays type-conversion jrockit

我有两个应用程序在不同的服务器上运行,一个具有 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")

总的来说,没有做太多事情。

关于java - 在不同平台上字节到字符串的转换,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21309707/

相关文章:

java - "Background concurrent copying GC freed"日志消息后空闲时应用程序卡住

java - Java 中的 JSON 模式 validator 库

string - 如何检查字符串是否包含括号 ")("?

ios - 过滤和排序 swift 数组

java - 为什么原始类型的数组不被视为对象

java 重构 if else

java - 使用 Java URLConnection 设置 FTP 主动/被动模式

c++ - 如何返回字符串数组?

javascript - 使用正则表达式和 javascript 获取字符串中的精确匹配

php - 如何在 PHP (Java)Doc 中标记数组值类型?