473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

convert back to original string

254 100+
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 "dje3h344h3hk4w9sjs" (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.



Expand|Select|Wrap|Line Numbers
  1. public class Encryptor {
  2.  
  3.     public static String toHexString(byte [] ab) {
  4.         StringBuffer sb = new StringBuffer(ab.length * 2) ;
  5.  
  6.          for(int i=0; i<ab.length; ++i) {
  7.              String sTemp = Integer.toHexString(0xFF & ab[i]) ;
  8.              if(1==sTemp.length()) {
  9.                  sb.append("0") ;
  10.              }
  11.              sb.append(sTemp) ;
  12.          }
  13.          System.out.println(sb.toString());
  14.  
  15.          return sb.toString() ;
  16.          }
  17.  
  18.     public static String genHash(String s)
  19.     {
  20.  
  21.  
  22.          byte[] ab = new byte[25];
  23.  
  24.          try {
  25.              com.sun.midp.ssl.MessageDigest md = com.sun.midp.ssl.MessageDigest.getInstance(com.sun.midp.ssl.MessageDigest.ALG_SHA, true);
  26.  
  27.              md.update(s.getBytes(), 0, s.length()) ;
  28.  
  29.              System.out.println("in genHash method");
  30.              System.out.println("\t" + s);
  31.              System.out.println("\t" +md);
  32.  
  33.              short sh = md.doFinal(s.getBytes(), 0, s.length(), ab, 0 );
  34.  
  35.              System.out.println("\tsh = "+ sh);
  36.  
  37.          } catch (com.sun.midp.ssl.CryptoException e) {
  38.  
  39.             // TODO Auto-generated catch block
  40.             e.printStackTrace();
  41.          } catch (Exception e) {
  42.              e.printStackTrace();
  43.          }
  44.  
  45.  
  46.          return toHexString(ab) ;
  47.     }
  48.  
  49. }
  50.  
Dec 31 '07 #1
1 5478
chaarmann
785 Expert 512MB
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,
Expand|Select|Wrap|Line Numbers
  1. 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!
Dec 31 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Islam Elkhayat | last post by:
In my C# Webapplication.. I enter the date in a textbox using popup Calender in the format MM/dd/yyyy.. when i retrieve date from Sql server for updating using datarow to fill the textbox the...
12
by: Brian Henry | last post by:
first question... I have a flat file which unfortinuatly has columns seperated by nulls instead of spaces (a higher up company created it this way for us) is there anyway to do a readline with this...
14
by: Michael Barrido | last post by:
I have this for example: Dim iSeconds as int32 = 3600 '3600 seconds is one hour How do i convert it to "01:00:00" ? Please help. Thanks in advance!
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
29
by: Harlin Seritt | last post by:
Hi... I would like to take a string like 'supercalifragilisticexpialidocius' and write it to a file in binary forms -- this way a user cannot read the string in case they were try to open in...
0
by: deloford | last post by:
Hi This is going to be a question for anyone who is an expert in C# Text Encoding. My situation is this: I have a Sybase database which is firing back ISO-8559 encoded strings. I am unable to...
6
by: Bob Altman | last post by:
Hi all, I'm looking for the fastest way to convert an array of bytes to String. I also need to convert a String back to its original Byte() representation. Convert.ToBase64String and...
14
by: rtillmore | last post by:
Hello, I did a quick google search and nothing that was returned is quite what I am looking for. I have a 200 character hexadecimal string that I need to convert into a 100 character string. ...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.