Java MD5

 Message Digest algorithm version 5:

MD5 is a widely-used cryptographic hash function with a -bit hash value. Here are some common uses for MD5:
  • To store a one-way hash of a password.
  • To provide some assurance that a transferred file has arrived intact.
Note: It has been compromised using Flame malware in 2012.

This is the example to encrypt the alphanumeric string in java.

We have package named security in java(Check here).

Below code take the string as input and encrypt using MD5 algorithm.
import java.io.*;
import java.util.*;
import java.security.*;
import java.math.BigInteger;

public class MleClass {

    public static void main(String[] args) {
     
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        try{
        MessageDigest md=  MessageDigest.getInstance("MD5");
        
        System.out.printf("%032x\n", new BigInteger(1, md.digest(s.getBytes())));
        }catch(NoSuchAlgorithmException e){
            // Handle exceptions
        }
        
    }
}


No comments:

Post a Comment