将字节数组转换为String显示垃圾

时间:2014-11-02 12:31:17

标签: java string bytearray type-conversion

我在使用新的String()方法将字节数组转换为String时遇到问题。 即使使用编码,输出仍然是输出垃圾。

我正在开发UDP套接字编程,能够在主机/客户端之间发送加密和解密的消息。我想要做的是使用SHA-1将解密的byte in byte转换为String for hex函数。

    //received encrypted data from client
    byte[] cipherKB = new byte[8];
    cipherKB = receivePacket.getData(); 
    System.out.println(Arrays.toString(cipherKB));       //this output correct data from byte array         

    //contents of cipherKB [-36, 120, 90, 1, -51, 99, 27, 97]

    //decrypt the above message using "decrypt" method
    byte[] KB = new byte[8];
    KB = r.decrypt(cipherKB);                 // r is an object
    System.out.println(Arrays.toString(KB));         //this output correct data from byte array         

    //contents of KB [82, -127, 11, -40, -60, 81, 12, 65]

    String KBString = new String (KB,"UTF-8");   
    System.out.println(KBString);        //this is giving me garbage message when I output in console           

     System.out.println("KB.toString(): output " + KB.toString());

    //KB.toString(): output [B@578088c0
        .....    
}

//Decrypt function



    private final static byte[] S = new byte[256];
    private final byte[] T = new byte[256];
    private final int keylen;

    public static byte[] encrypt(final byte[] plaintext) {
            final byte[] ciphertext = new byte[plaintext.length];
            int i = 0, j = 0, k, t;
            byte tmp;
            for (int counter = 0; counter < plaintext.length; counter++) {
                i = (i + 1) & 0xFF;
                j = (j + S[i]) & 0xFF;
                tmp = S[j];
                S[j] = S[i];
                S[i] = tmp;
                t = (S[i] + S[j]) & 0xFF;
                k = S[t];
                ciphertext[counter] = (byte) (plaintext[counter] ^ k);
            }
            return ciphertext;
        }

        public static byte[] decrypt(final byte[] ciphertext) {
            return encrypt(ciphertext);
        }
    }

显示加密的输出数据正在运行:

//HOST - Alice

Alice Random KA:[45, 58, -4, 93, -1, -127, 127, 20]

Alice Random KA in string:[B@6791d8c1     //output of String KAString = new String (KA);

Alice encrypted KA sent to Client: [-63, 81, -91, 119, 124, -24, 86, 41]

Received Bob's KB: [16, 103, 39, -13, 46, -120, 115, -116]   //this is same as Bob's encrypted KB below

Decrypted Bob's KB: [-98, -98, 118, 42, 39, -70, 100, -84]   //same as Bob's Random KB generated

//CLIENT - Bob

Received Alice's encrypted KA: [-63, 81, -91, 119, 124, -24, 86, 41]  

Decrypted Alice's KA: [45, 58, -4, 93, -1, -127, 127, 20]   //this is same as Alice's Random KA above

Bob's Random KB:[-98, -98, 118, 42, 39, -70, 100, -84]

Bob's encrypted KB: [16, 103, 39, -13, 46, -120, 115, -116]

2 个答案:

答案 0 :(得分:1)

当然,您意识到[B@6791d8c1只是没有自己版本的对象的默认toString[45, 58, -4, 93, -1, -127, 127, 20](加密前的原始数据)是&#34; - :]&#34; - 不是真正有效的字符数据,所以人们会期望它看起来像&#34;垃圾&#34;。

答案 1 :(得分:1)

//Convert from String to byte[]:

String s = "some text here";
byte[] b = s.getBytes("UTF-8");

//Convert from byte[] to String:

byte[] b = {(byte) 99, (byte)97, (byte)116};

String s = new String(b, "US-ASCII");

当然,您应该使用正确的编码名称。我的例子使用&#34; US-ASCII&#34;和&#34; UTF-8&#34;,两种最常见的编码。