A Simple Java Program to Convert a String into MD5 Hashing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package practice; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Scanner; public class MD5Encryption { public static void main(String[] args) throws NoSuchAlgorithmException { Scanner sc = new Scanner(System.in); String val = sc.next(); MessageDigest digest = MessageDigest.getInstance("MD5"); byte[] encodedHash = digest.digest(val.getBytes(StandardCharsets.UTF_8)); StringBuffer md5EncodedString = bytestoHexString(encodedHash); System.out.println(md5EncodedString); } private static StringBuffer bytestoHexString(byte[] encodedHash) { StringBuffer hexString = new StringBuffer(); for (int i = 0; i < encodedHash.length; i++) { String hex = Integer.toHexString(0xff & encodedHash[i]); if (hex.length() == 0) hexString.append("0"); hexString.append(hex); } return hexString; } } |