To implement MD5 > Java Program

To implement MD5 > Java Program

Cryptography and System Security

Program:
import java.io.*;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {
    public static String getMD5(String input) {

        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());
            BigInteger number = new BigInteger(1, messageDigest);
            String hashtext = number.toString(16);
            // Now we need to zero pad it if you actually want the full 32 chars.
            while (hashtext.length() < 32) {
                hashtext = "0" + hashtext;
            }
            return hashtext;
        }
        catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
     
        BufferedReader br = new BufferedReader(new InputStreamReader (System.in));
        String str = new String();
        str=br.readLine();
        System.out.println(getMD5(str));
    }
}


Output:
run:
prathamesh
fe7e087d2f99208e8a23dfdd08d2daf4
BUILD SUCCESSFUL (total time: 3 seconds)

Comments

Popular posts from this blog

Intermediate Code Generation > C Program