I have the following code to encrypt a password, but when I am trying to decode it, I don’t get the expected result here is the code.
BASE64Encoder encoder = new BASE64Encoder();
String afterhex=toSHA1("mypassword".getBytes());
String encodedBytes = encoder.encodeBuffer(afterhex.getBytes());
public static String toSHA1(byte[] convertme) {
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return new String(md.digest(convertme));
}
For example if you tried to encode, jill you should get LQBIF2TS0FSDYtGjaNmC2gl/klw=
Any advice for restoring it back 🙂
Hashing algorithm is not encryption / decryption algorithm.
Hashing is a one way process of mapping large data sets of variable length (e.g. message), to smaller data sets of a fixed length (hash). The length depends on hashing algorithm.
And it is NOT POSSIBLE to perform reverse operation from hash back to your message.
Even though it is possible to find a message generating the same hash (for example using rainbow tables; much easier for weaker hashing algorithms like MD5), you never know if the message is identical to the original one which was used to generate the hash. One of the methods which prevents finding (guessing) a message (password) generating the same hash value is using salt when hashing a massage (password).
EDIT
I would also recommend any of Bruce Schneier books e.g. “Cryptography Engineering: Design Principles and Practical Applications” (written in a very nice and digestible way) which will describe cryptography and hashing in great details.