byte[] to String and back to byte[]

Whts wrong with this ?

i need to test this as , i am implemention the same logic in AES for rmi program.

where i am sending the encrypted data as string,

and the decrypter has to convert the string to byte[].

hence the both byte[] as to be same.

please correct my logic/program.

publicclass testbyte_string{

publicstaticvoid main(String[] args)throws Exception{

// Convert the String to byte[]

byte [] stryst = args[0].getBytes("utf-8");

System.out.println(" string to byte [] : " +stryst);

// Convert the byte [] to string

String strs = stryst.toString();

System.out.println("byte [] to string :: "+ strs);

// Convert the String to byte[]

byte[] ins = strs.getBytes("utf-8");

System.out.println(" string to byte []: " + ins);

// the stryst and ins must be equal:

}

}

It gives the output has :

jjava testbyte_string samtt

string tobyte [] stryst : [B@24c70

byte [] to string : strs : [B@24c70

string tobyte []: ins: [B@24c60

-

why (stryst != ins ) ?

[1838 byte] By [samjala] at [2007-10-3 9:24:44]
# 1
Method toString() on an array does not give a representation of the content of the array, just a pseudo pointer to the array. Use Arrays.toString(...) . For example -System.out.println(" string to byte [] : " +Arrays.toString(stryst));
sabre150a at 2007-7-15 4:38:44 > top of Java-index,Security,Cryptography...
# 2

> Method toString() on an array does not give a

> representation of the content of the array, just a

> pseudo pointer to the array. Use Arrays.toString(...)

> . For example -

>

> System.out.println(" string to byte [] : "

> +Arrays.toString(stryst));

it gives error as:

testbyte_string.java:23: toString() in java.lang.Object cannot be applied to (java.lang.String)

System.out.println(" string to byte [] : " + Arrays.toString(stryst));

samjala at 2007-7-15 4:38:44 > top of Java-index,Security,Cryptography...
# 3

What version of Java are you using? See if this works

import java.util.*;

public class Fred112

{

public static void main(String[] args) throws Exception

{

byte[] values = {0,1,2,3,4,5};

System.out.println(Arrays.toString(values));

}

}

sabre150a at 2007-7-15 4:38:44 > top of Java-index,Security,Cryptography...
# 4
its the same error.and mine is:java version "1.4.2" on fc5
samjala at 2007-7-15 4:38:44 > top of Java-index,Security,Cryptography...
# 5
Forget it. Round-tripping bInary byte arrays to String and back is never going to work, and there is nothing in the API that says it should work. That's not what Strings are for. Just keep the byte[] array.
ejpa at 2007-7-15 4:38:44 > top of Java-index,Security,Cryptography...
# 6
> its the same error.> > and mine is:> java version "1.4.2" on fc5Arrays.toString(,...) was introduced in 1.5 so you will have to write a method to print the content of a byte array.
sabre150a at 2007-7-15 4:38:44 > top of Java-index,Security,Cryptography...
# 7

can i get any code for AES encryption and decryption.

both in different programs.

b'coz i do byte[] to string : to transfer the encrypted byte[] over the network.

on server side: i retrive the string -> convert it back to byte[] , and do decryption over it.

please help me.

i am stuck in the middle.

in need to encrypt / decrypt the text , not the file.

plese reply fast.

samjala at 2007-7-15 4:38:44 > top of Java-index,Security,Cryptography...
# 8

> b'coz i do byte[] to string : to transfer the

> encrypted byte[] over the network.

>

> on server side: i retrive the string -> convert it

> back to byte[] , and do decryption over it.

and you've already been told that neither of these operations will work.

Rethink.

ejpa at 2007-7-15 4:38:44 > top of Java-index,Security,Cryptography...
# 9
then wts the other way for text encryption and decryption?over network..?
samjala at 2007-7-15 4:38:44 > top of Java-index,Security,Cryptography...
# 10
> then wts the other way for text encryption and> decryption?> over network..?Send the encrypted data as bytes.
sabre150a at 2007-7-15 4:38:44 > top of Java-index,Security,Cryptography...
# 11
as stated in #5 six hours ago ... I don' t know why I keep expecting that people who post questions will read the answers ...
ejpa at 2007-7-15 4:38:44 > top of Java-index,Security,Cryptography...
# 12

stryst.toString() will not work in this case;

use String strs= new String(strys); that will do the job.

-

public class testbyte_string{

public static void main(String[] args) throws Exception {

// Convert the String to byte[]

byte [] stryst = args[0].getBytes("utf-8");

System.out.println(" string to byte [] : " +stryst);

// Convert the byte [] to string

//String strs = stryst.toString();

String strs=new String(stryst);

System.out.println("byte [] to string :: "+ strs);

// Convert the String to byte[]

byte[] ins = strs.getBytes("utf-8");

System.out.println(" string to byte []: " + ins);

// the stryst and ins must be equal:

}

}

s_dhillona at 2007-7-15 4:38:44 > top of Java-index,Security,Cryptography...
# 13

> stryst.toString() will not work in this case;

> use String strs= new String(strys); that will do the

> job.

>

>

>

> -

> public class testbyte_string{

>

> ublic static void main(String[] args) throws

> Exception {

>

> // Convert the String to byte[]

>byte [] stryst = args[0].getBytes("utf-8");

> System.out.println(" string to byte [] : "

> +stryst);

>

>// Convert the byte [] to string

> //String strs = stryst.toString();

>

>String strs=new String(stryst);

> System.out.println("byte [] to string :: "+ strs);

>

> // Convert the String to byte[]

>byte[] ins = strs.getBytes("utf-8");

> System.out.println(" string to byte []: " +

> " + ins);

>

>// the stryst and ins must be equal:

> }

> }

Sorry but this is wrong! Since your byte array contains utf-8 encode bytes obtained from

byte [] stryst = args[0].getBytes("utf-8");

then to convert them back to a String you should specifiy utf-8 encoding. Your code will try to convert the bytes to a String using the default encoding which probably is not and in general will not be utf-8.

sabre150a at 2007-7-15 4:38:44 > top of Java-index,Security,Cryptography...
# 14

byte[] to String and back to byte[] is a fallacious operation. There is no guarantee anywhere that it will work, because there is no guarantee that the contents of the byte[] array conform to whatever character set you are trying to use. It shouldn't even be attempted. Find another solution. Such as, carting the byte[] array around the way it is. This is also more efficient.

ejpa at 2007-7-15 4:38:44 > top of Java-index,Security,Cryptography...