Wednesday, July 26, 2023

Java MD5

 MD5 (Message-Digest algorithm 5) 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.

MD5 is one in a series of message digest algorithms designed by Professor Ronald Rivest of MIT (Rivest, ); however, the security of MD5 has been severely compromised, most infamously by the Flame malware in . The CMU Software Engineering Institute essentially considers MD5 to be "cryptographically broken and unsuitable for further use".

Given an alphanumeric string, , denoting a password, compute and print its MD5 encryption value.

Input Format

A single alphanumeric string denoting .

Constraints

  • String  consists of English alphabetic letters (i.e.,  and/or decimal digits (i.e.,  through ) only.

Output Format

Print the MD5 encryption value of  on a new line.

Sample Input 0

HelloWorld

Sample Output 0

68e109f0f40ca72a15e05cc22786f8e6

Sample Input 1

Javarmi123

Sample Output 1

2da2d1e0ce7b4951a858ed2d547ef485


SOLUTION:


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Solution {
    
    static String MD5(String message)
    {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(message.getBytes());
            BigInteger no = new BigInteger(1, messageDigest);
            String hashtext = no.toString(16);
            while (hashtext.length() < 32) {
                hashtext = "0" + hashtext;
            }
            return hashtext;
        }
 

        catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        String message = scanner.nextLine();
        scanner.close();
        String encryptedMessage=MD5(message);
        System.out.println(encryptedMessage);
    }
}

No comments:

Post a Comment

Featured Post

14. Longest Common Prefix

Popular Posts