2014-11-02 82 views
0

我有一个问题,使用新的String()方法将字节数组转换为字符串。 即使使用编码,输出仍是输出垃圾。转换字节数组为字符串显示垃圾

我正在研究UDP套接字编程,并能够在主机/客户端之间发送加密和解密的消息。我想要做的是将解密的消息以字节转换为使用SHA-1的十六进制函数的字符串,该SHA-1采用字符串。

//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 [[email protected] 
     .....  
} 

//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:[[email protected]  //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] 
+0

加密方法是什么S数组? – SMA 2014-11-02 12:38:12

+0

@almasshaikh S是字节数组。上面更新。 – user23 2014-11-02 12:55:06

+0

那么,你的字节数组是什么?我猜他们不是合法的Unicode字符。 – 2014-11-02 12:58:26

回答

1

你意识到,当然,这[[email protected]简直是一个对象,它没有自己的默认toString版。和[45, 58, -4, 93, -1, -127, 127, 20](你的原始数据在加密之前)是“ - :]” - 不是真正有效的字符数据,所以人们会认为它看起来像“垃圾”。

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

您当然应该使用正确的编码名称。我的例子使用了两种最常见的编码“US-ASCII”和“UTF-8”。

相关问题