Calculate the message digest of a text using the MD5 algorithm in JAVA | Cryptography
HARDWARE
AND SOFTWARE REQUIREMENT:
1. Intel
based Desktop PC: - RAM of 512 MB
2.
Notepad/Notepad ++ editor
3. Net
beans / Eclipse
THEORY:
Ø The MD5 hashing algorithm is a
one-way cryptographic function that accepts a message of any length
as input and returns as output a fixed-length digest value to be used
for authenticating the original message.
Ø The MD5 hash function was originally designed
for use as a secure cryptographic hash algorithm for authenticating digital
signatures(to validate the authenticity and integrity of a message, software or
digital document)
Ø MD5 is used for storing securing password in
database server. MD5 generated message digest of 128 bits.
SOURCE
CODE:
import java.security.*;
public class MD5 {
public
static void main(String[] a) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
System.out.println("Message digest object info:");
System.out.println(" Algorithm="
+ md.getAlgorithm());
System.out.println(" Provider="
+ md.getProvider());
System.out.println(" ToString="
+ md.toString());
String input = "";
md.update(input.getBytes());
byte[] output = md.digest();
System.out.println();
System.out.println("MD5(\"" + input +
"\")=" + bytesToHex(output));
input = "abc";
md.update(input.getBytes());
output = md.digest();
System.out.println();
System.out.println("MD5(\"" + input +
"\")=" + bytesToHex(output));
input = "abd";
md.update(input.getBytes());
output = md.digest();
System.out.println();
System.out.println("MD5(\"" + input +
"\")=" + bytesToHex(output));
input = "abcdefghijklmnopqrstuvwxyz";
md.update(input.getBytes());
output = md.digest();
System.out.println();
System.out.println("MD5(\"" + input +
"\")=" + bytesToHex(output));
input = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
md.update(input.getBytes());
output = md.digest();
System.out.println();
System.out.println("MD5(\"" + input +
"\")=" + bytesToHex(output));
System.out.println();
}
catch (Exception e) {
System.out.println("Exception:" + e);
}
}
private
static String bytesToHex(byte[] b) {
char
hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C',
'D', 'E', 'F'};
StringBuffer buf = new StringBuffer();
for
(int j = 0; j < b.length; j++) {
buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
buf.append(hexDigit[b[j] & 0x0f]);
}
return
buf.toString();
}
}
OUTPUT:
VIVA QUESTIONS:
1.What is JAVA?
2.What is MD5 Algorithm?
3.What is java script?
4.What is Cryptography?
5.What is Encryption?
6.What is Decryption?
Comments
Post a Comment