0

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]
17
  • What's this S array in encrypt method? Commented Nov 2, 2014 at 12:38
  • @almasshaikh S is byte array. Updated above. Commented Nov 2, 2014 at 12:55
  • So, what is in your byte array? I'm guessing they're not legit Unicode characters. Commented Nov 2, 2014 at 12:58
  • Lessee: The incoming data prints OK, you decrypt it, and then it doesn't print OK. Do you see a problem with this? Commented Nov 2, 2014 at 13:00
  • @user23 Am getting correct value. Can you paste content of cipherKB ? Commented Nov 2, 2014 at 13:05

2 Answers 2

1

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".

Sign up to request clarification or add additional context in comments.

Comments

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.