Wms (2966) Exp 5
Wms (2966) Exp 5
1
NAME: Abhay singh UID: 20BCS2966
SECTION & GROUP: 602-B SEMESTER: 5
SUBJECT: WEB & MOBILE SECURITY LAB
Aim: Write a program to generate message digest for the given message using the
SHA/MD5 algorithm and verify the integrity of message.
Software/Hardware Requirements:
window 7 and above version
Tools to be used:
1. Eclipse IDE
2. JDK (Java Development kit)
3. IntelliJ IDEA
Steps/Method/Coding:
2. After selecting the algorithm it calculate the digest value and return the results in
byte array.
3. BigInteger class is used, which converts the resultant byte array into its sign-
magnitude representation.
4. This representation is then converted into a hexadecimal format to get the expected
MessageDigest.
Examples:
Input : hello world
Output : 5eb63bbbe01eeed093cb22bb8f5acdc3
Input : GeeksForGeeks
Output : e39b9c178b2c9be4e99b141d956c6ff6
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 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);
}
OUTPUT
String s1 = "GeeksForGeeks";
System.out.println("\n" + s1 + " : " + encryptThisString(s1));
String s2 = "hello world";
System.out.println("\n" + s2 + " : " + encryptThisString(s2));
}
}
Output screenshot:
Learning Outcomes:
Output is often known as hash values, hash codes, message digest. The length of output
hashes is generally less than its corresponding input message length.