2014-11-02 133 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」。

相關問題