convert back to original string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nickyeng
    Contributor
    • Nov 2006
    • 252

    #1

    convert back to original string

    The following code i search some where from websites.

    it generate hash value for a string.

    but is that possible that convert it back to original string ?

    like if the string is "this is string" and hash value is "dje3h344h3hk4w 9sjs" (just an example)...

    so how i'm going to make the hash value back to original string ?
    can anyone help ?
    i am a noob on this situation.



    Code:
    public class Encryptor {
    	
    	public static String toHexString(byte [] ab) {
    		StringBuffer sb = new StringBuffer(ab.length * 2) ;
    
    		 for(int i=0; i<ab.length; ++i) {
    			 String sTemp = Integer.toHexString(0xFF & ab[i]) ;
    			 if(1==sTemp.length()) {
    				 sb.append("0") ;
    			 }
    			 sb.append(sTemp) ;
    		 }
    		 System.out.println(sb.toString());
    		 
    		 return sb.toString() ;
    		 }
    
    	public static String genHash(String s)
    	{
    			 
    		
    		 byte[] ab = new byte[25];
    		
    		 try {
    			 com.sun.midp.ssl.MessageDigest md = com.sun.midp.ssl.MessageDigest.getInstance(com.sun.midp.ssl.MessageDigest.ALG_SHA, true);
    			
    			 md.update(s.getBytes(), 0, s.length()) ;
     
    			 System.out.println("in genHash method");
    			 System.out.println("\t" + s);
    			 System.out.println("\t" +md);
    		
    			 short sh = md.doFinal(s.getBytes(), 0, s.length(), ab, 0 );
    			
    			 System.out.println("\tsh = "+ sh);
    			 
    		 } catch (com.sun.midp.ssl.CryptoException e) {
    			
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		 } catch (Exception e) {
    			 e.printStackTrace();
    		 }
     
    		 
    		 return toHexString(ab) ;
    	}
    
    }
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    Originally posted by nickyeng
    The following code i search some where from websites.

    it generate hash value for a string.

    but is that possible that convert it back to original string ?
    No, not in general.
    It may be for possible for short strings (just use reverse function of your hash-code algorithm to convert back), but definitively NOT for long strings.
    That's the principle of a hash code: to have a shorter identifier for the string. Else you would not need a hash code; you could have used the string itself.

    Look in your example,
    Code:
    byte[] ab = new byte[25];
    You can see that the size of the hash code is limited to 25 characters, meaning you can code exactly (2 power 16) power 25 different strings. But there exists an ENDLESS number of different strings, so a lot of strings must have the same hash code.

    If many different strings have exactly the same hash code, then how can you decide which one is the original that you used to make this hash code? You can't!

    Comment

    Working...