MD2 Hash In Java

The MD2 is a Message-Digest Algorithm. It is a cryptographic hash function developed by Ronald Rivest in 1989. It is optimized for 8-bit computers. The MD2 algorithm is used in public key infrastructures as part of certificates generated with MD2 and RSA. From 2014, this algorithm is now not considered as a secure algorithm.

To calculate cryptographic hashing value in Java, MessageDigest Class is used, under the package java.security.

MessagDigest Class provides following cryptographic hash function to find hash value of a text as follows:

  • MD2
  • MD5
  • SHA-1
  • SHA-224
  • SHA-256
  • SHA-384
  • SHA-512

These algorithms are initialized in static method called getInstance(). After selecting the algorithm the message digest value is calculated and the results are returned as a byte array. BigInteger class is used, to convert the resultant byte array into its signum representation. This representation is then converted into a hexadecimal format to get the expected MessageDigest.


Input : hello world
Output : d9cce882ee690a5c1ce70beff3a78c77

Input : GeeksForGeeks
Output : 787df774a3d25dca997b1f1c8bfee4af


Below program shows the implementation of MD2 hash in Java.

// Java program to calculate MD2 hash value
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class GFG {
    public static String encryptThisString(String input)
        try {
            // getInstance() method is called with algorithm MD2
            MessageDigest md = MessageDigest.getInstance("MD2");
            // digest() method is called
            // to calculate message digest of the input string
            // returned as array of byte
            byte[] messageDigest = md.digest(input.getBytes());
            // Convert byte array into signum representation
            BigInteger no = new BigInteger(1, messageDigest);
            // Convert message digest into hex value
            String hashtext = no.toString(16);
            // Add preceding 0s to make it 32 bit
            while (hashtext.length() < 32) {
                hashtext = "0" + hashtext;
            // return the HashText
            return hashtext;
        // For specifying wrong message digest algorithms
        catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
    // Driver code
    public static void main(String args[]) throws
        System.out.println("HashCode Generated by MD2 for: ");
        String s1 = "GeeksForGeeks";
        System.out.println(" " + s1 + " : " + encryptThisString(s1));
        String s2 = "hello world";
        System.out.println(" " + s2 + " : " + encryptThisString(s2));


HashCode Generated by MD2 for: 

GeeksForGeeks : 787df774a3d25dca997b1f1c8bfee4af

hello world : d9cce882ee690a5c1ce70beff3a78c77


  • Cryptography
  • Data Integrity

This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment



load comments

Subscribe to Our Newsletter