• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

bytearray to string and back to byte array

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am getting byte array from digest method. Then I would convert byte array into string using new String(byte[]) method. Afterwards, I would convert the same string into byte array using toBytes() method.
While doing this, initial byte array and final byte array is different. I would assume that It should be same...
Here is the sample code I am using..

public static void main(String[] args){
String Password = "fraud";
try{
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(Password.getBytes());
byte[] b1 = md.digest() ;

System.out.println("B1 length:" + b1.length);
System.out.println("B1 " + b1);

String finalString = new String(b1);


byte[] c1 = finalString.getBytes();
System.out.println("C1 length:" + c1.length);
System.out.println("C1 " + c1);
// you can notice b1 and c1 are different here..
for(int i=0; i<b1.length;i++)>
System.out.println(" letter " + i +" " + b1[i] + " " + c1[i] + " " );


}catch(Exception e){System.out.println(e.toString());}
}

thanks,
 
author
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"hits100"
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in again with a new name which meets the requirements.
Thanks.
Sean
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
did you see the what the diff was.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know what the problem is here. Try using methods which allow you to specify the encoding used - e.g.
<pre>
md.update(Password.getBytes("UTF-8"));
...
String finalString = new String(b1, "UTF-8");
byte[] c1 = finalString.getBytes("UTF-8");
</pre>
and see if that helps. It may also be useful to find out what the default encoding is on your machine - try
<pre>
System.out.println(new InputStreamReader(System.in).getEncoding());
</pre>
to find out what that is. (I don't know why this info isn't more readily available via a System.getProperty() call, but this is the best way I know to find out.) Anyway, my thinking is that it's possible that there are some encodings which are not fully reversible - that is, the process is not one-to-one, and for some byte sequences there may be more than one way to encode (or decode). Echoing Ganesh's question, it would also be useful to see exactly what the difference was between the two byte arrays.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic