user23
user23

Reputation: 425

Converting byte array to String shows garbage

I have a problem converting byte array to String using new String() method. Even with the encoding, the output is still output garbage.

I am working on UDP socket programming and able to send encrypted and decrypted messages between host/ client. What I am trying to do is to convert the decrypted message in byte to String for hex function using SHA-1 which takes in String.

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

Output data to show encryption is working:

//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]

Upvotes: 0

Views: 4071

Answers (2)

msmukesh4
msmukesh4

Reputation: 589

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

You should, of course, use the correct encoding name. My examples used "US-ASCII" and "UTF-8", the two most common encodings.

Upvotes: 1

Hot Licks
Hot Licks

Reputation: 47699

You realize, of course, that [B@6791d8c1 is simply the default toString for an object that does not have its own version. And [45, 58, -4, 93, -1, -127, 127, 20] (your original data before encrypting) is "-:] " -- not really valid character data, so one would expect it to look like "garbage".

Upvotes: 1

Related Questions