16

I have a byte array which I'm encrypting then converting to a string so it can be transmitted. When I receive the string I then have to convert the string back into a byte array so it can be decrypted. I have checked that the received string matches the sent string (including length) but when I use something like str.getBytes() to convert it to a byte array, it does not match my original byte array.

example output:

SENT: WzShnf/fOV3NZO2nqnOXZbM1lNwVpcq3qxmXiiv6M5xqC1A3
SENT STR: [B@3e4a9a7d
RECEIVED STR: [B@3e4a9a7d
RECEIVED: W0JAM2U0YTlhN2Q=

any ideas how i can convert the received string to a byte array which matches the sent byte array?

Thanks

Mitch
  • 615
  • 2
  • 6
  • 6
  • 3
    For converting String to Byte Array use this : String stringToConvert = "This String is 76 characters long and will be converted to an array of bytes"; byte[] theByteArray = stringToConvert.getBytes(); – Kartik Domadiya Mar 31 '11 at 12:39
  • 1
    W0JAM2U0YTlhN2Q= is Base64 for [B@3e4a9a7d, which looks like a String representation for a byte array in Java. If you are performing byte manipulation yourself, double check it as you may be sending the wrong data. – mdrg Mar 31 '11 at 12:41
  • out is my byte array and when printed gives the first line of my example output. out.toString() definitely gives me [B@3e4a9a7d – Mitch Mar 31 '11 at 12:57
  • it would be helpful to see the code (part) – user85421 Mar 31 '11 at 13:29
  • http://stackoverflow.com/questions/17577887/writing-byte-array-to-txt-file-and-reading-it-back/17582115#17582115 – Sparrow_ua Jul 10 '13 at 22:45

4 Answers4

16

You used array.toString(), which is implemented like this:

return "[B@" + Integer.toString(this.hashCode(), 16);

(In fact it inherits the definition from Object, and the part before the @ simply is the result of getClass().getName().)

And the hashCode here does not depend on the content.

Instead, use new String(array, encoding).

Of course, this only works for byte-arrays which are really representable as Java strings (which then contain readable characters), not for arbitrary arrays. There better use base64 like Bozho recommended (but make sure to use it on both sides of the channel).

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
5

This looks like Base64. Take a look at commons-codec Base64 class.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • I did try using the sun.misc.BASE64Decoder which i believe is very similar and it didnt seem to work for me – Mitch Mar 31 '11 at 12:39
  • 1
    @Mitch try `string.getBytes("ISO-8859-1")`, and then passing that as constructor arugment as well. – Bozho Mar 31 '11 at 12:42
  • 2
    @Mitch do not use any class from `sun.*` packages. They are proprietary API and may be removed at any time. Use any other public implementation, like the one suggested by Bozho. – mdrg Mar 31 '11 at 12:46
4

You can't just use getBytes() on two different machines, since getBytes uses the plattform's default charset.

Decode and encode the array with a specified charset (i.e. UTF-8) to make sure you get the correct results.

Voo
  • 29,040
  • 11
  • 82
  • 156
1

First do convertion of your byte array to proper string, by doing

String line= new String(Arrays.toString(your_array))

Then send it and use function below

public static byte[] StringToByteArray(String line)
{

    String some=line.substring(1, line.length()-1);     
    int element_counter=1;

    for(int i=0; i<some.length(); i++)
    {           
        if (some.substring(i, i+1).equals(","))
        {
            element_counter++;
        }       

    }
    int [] comas =new int[element_counter-1];
    byte [] a=new byte[element_counter];
    if (a.length==1)
    {
        a[0]= Byte.parseByte(some.substring(0));
    }       
    else 
    {
        int j=0;
        for (int i = 0; i < some.length(); i++) 
        {
            if (some.substring(i, i+1).equals(","))
            {
                comas[j]=i;
                j++;
            }
        }           
        for (int i=0; i<element_counter; i++)
        {
            if(i==0)
            {
                a[i]=Byte.parseByte(some.substring(0, comas[i]));
            }
            else if (i==element_counter-1)
            {
                a[i]=Byte.parseByte(some.substring(comas[comas.length-1]+2));
            }
            else
            {
                a[i]=Byte.parseByte(some.substring(comas[i-1]+2, comas[i]));
            }

        }
    }
    return a;

}
Sparrow_ua
  • 664
  • 1
  • 11
  • 28