0% found this document useful (0 votes)
40 views5 pages

Wms (2966) Exp 5

This document describes an experiment to generate message digests using SHA and MD5 algorithms and verify message integrity. It provides the aim, software requirements, steps, and coding examples to hash messages using MD5 and SHA algorithms in Java. The MD5 and SHA coding examples show how to initialize the algorithms, calculate digest values from input bytes, convert the byte array to a hexadecimal string, and output the hashed messages. The learning outcome is that message digest outputs, also called hash values or codes, are generally shorter than the original input messages.

Uploaded by

Adnan Ytseller
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views5 pages

Wms (2966) Exp 5

This document describes an experiment to generate message digests using SHA and MD5 algorithms and verify message integrity. It provides the aim, software requirements, steps, and coding examples to hash messages using MD5 and SHA algorithms in Java. The MD5 and SHA coding examples show how to initialize the algorithms, calculate digest values from input bytes, convert the byte array to a hexadecimal string, and output the hashed messages. The learning outcome is that message digest outputs, also called hash values or codes, are generally shorter than the original input messages.

Uploaded by

Adnan Ytseller
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

EXPERIMENT 2.

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:

To calculate cryptographic hashing value in Java, MessageDigest Class is used,


under the package java.security.
MessageDigest 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
1. This Algorithms are initialize in static method called getInstance().

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

Coding (MD5 algorithm)

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);
}

public static void main(String args[]) throws NoSuchAlgorithmException


{
String s = "GeeksForGeeks";
System.out.println("Your HashCode Generated by MD5 is: " + getMd5(s));
}
}

OUTPUT

Coding (SHA algorithm)

// Java program to calculate SHA-1 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 SHA-1
MessageDigest md = MessageDigest.getInstance("SHA-1");

// 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
NoSuchAlgorithmException
{
System.out.println("HashCode Generated by SHA-1 for: ");

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.

You might also like