Password Encryption

Password Encryption java example.

package hash;

import java.security.*;

public class Encryption {

    public static String md5(String plainText) {

        String encryptedText = "";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] encryptedData = md.digest(plainText.getBytes());
            encryptedText = new String(encryptedData);

        } catch (NoSuchAlgorithmException ex) {
        }
        return encryptedText;

    }
}

package hash;
public class Main {
    public static void main(String[] args) {
        System.out.println(Encryption.md5("123"));
    }
}
Out put String : ,¹b¬Y [–K -#Kp


package hash;

import java.security.*;

public class Encrypt {

    public static String md5(String plainText) {
        String encryptedText = "";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte encryptedData[] = md.digest(plainText.getBytes());
            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < encryptedData.length; i++) {
                String hex = Integer.toHexString(0xFF & encryptedData[i]);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }
            encryptedText = hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return encryptedText;
    }
}

package hash;
public class Main {
    public static void main(String[] args) {
        System.out.println(Encrypt.md5("123"));
    }
}

 Out put string : 202cb962ac59075b964b07152d234b70

9 comments:

  1. Thanks Sameera,
    the code of Password Encryption work fine for me..
    Can u also give the code for Password decryption also...
    Thanks so much...

    ReplyDelete
    Replies
    1. MD5 like password hashing algorithms are one way algorithms. There are no ways to decrypt those.

      Delete
    2. Thanks for reply..
      So please tell me the two way encryption/decryption code...
      And please give me some information about SALT algorithm.

      Delete
    3. And tell me, what is the use of one way algorithm if we doesn't get the same result after decryption.

      Delete
    4. We use one-way algorithems to hash passwords before saving to database.
      When user submit the login form we,
      get the palin password
      hash it
      compare hash with the hash in database
      if match user can login

      if sombody hack the database he cannot get user passwords.Because we don't save plian text passwords in database.

      Delete
  2. Hi, can you help me to gain a better approach to hacking? I'm a newcomer & I would like to learn some useful skills.

    Many thanks. Reece

    ReplyDelete
  3. can u tell me how to do encrypt and decrypt using struts with hibernate. how can we connect in same generator key. i tried but it showing other other names

    ReplyDelete