Password encryption using MD-5 and SHA algorithms
Question:- What is MD-5 algorithm?Answer:-
MD5 (Message-Digest algorithm 5)
MD5 was developed by Ronald Rivest in 1991 to replace an earlier hash function which was MD4. It was used to create digital signatures.
In cryptography, MD5 (Message-Digest algorithm 5) are secure one-way hash functions means that it takes a message and converts it into a fixed string of digits. This algorithm will take arbitrary-sized (any sized) data and provides output a fixed-length hash value. It’s a widely used algorithm, which is used to produce unique and reliable identifiers of data. MD5 cryptograph algorithm has been employed in a wide variety of security applications. As per the investigators, it has been marked as MD5 is not collision resistant, so MD5 is not suitable for applications like SSL certificates or digital signatures that rely on this property. An MD5 hash is typically expressed as a 32 digit hexadecimal number.
Question:- What is SHA algorithm?
Answer:-
SHA (Secure Hash Algorithm)
The SHA hash functions are a set of cryptographic
hash functions designed by the National Security Agency
(NSA) and published by the NIST as a U.S. Federal Information Processing
Standard. The three SHA algorithms are
structured differently and are distinguished as SHA-0, SHA-1, and SHA-2. The
SHA-2 family uses an identical algorithm with a variable digest size which is
distinguished as SHA-224, SHA-256, SHA-384, and SHA-512. SHA is a
cryptographic message digest algorithm similar to MD5. SHA-1 hash
considered to be one of the most secure hashing functions, producing a
160-bit digest (40 hex numbers) from any data with a maximum size of 264
bits. While Java has built in classes to compute SHA 1 hash, it's quite
uneasy to use them for a simple task -- calculate SHA-1 hash and return 40 byte
hexadecimal string.SHA-1 is the best established of the existing SHA hash functions, and is employed in several widely used security applications and protocols.
Java Sample Example to use MD-5 and SHA algorithms:-
PasswordEncryptUsingMD5_SHA.java
package com.gaurav.java.security;
import
java.io.UnsupportedEncodingException;
import
java.security.MessageDigest;
import
java.security.NoSuchAlgorithmException;
import sun.misc.BASE64Encoder;
public class
PasswordEncryptUsingMD5_SHA {
/**
* This method is used for Password Encryption
using any passed algorithm
* like SHA-1 and MD-5. we can also pass
encoding style for password
* encryption like UTF-8 or UTF-16
*/
public
static synchronized String encryptUsingOtherAlgorithm(
String
plaintext, String algorithmUsed, String encodingStyle)
throws
Exception {
MessageDigest
messageDigest = null;
String
encryptedStringValue
= null;
try
{
messageDigest
= MessageDigest.getInstance(algorithmUsed);
messageDigest.update(plaintext.getBytes(encodingStyle));
byte
updatedByteArray[] = messageDigest.digest();
encryptedStringValue = (new
BASE64Encoder())
.encode(updatedByteArray);
}
catch (NoSuchAlgorithmException e) {
System.out
.println("No
such algorithm is supported by any provider :-"
+
e.getMessage());
}
catch (UnsupportedEncodingException e) {
System.out.println("The
encoding is not supported:-"
+
e.getMessage());
}
return
encryptedStringValue;
}
}
PasswordEncryptUsingMD5_SHACaller.java
package com.gaurav.java.security;
public class
PasswordEncryptUsingMD5_SHACaller {
public
static void main(String[] args) {
try
{
String
passwordToEncrypt = "KumarGaurav";
System.out.println("Encrypted
Password Using SHA Algorithm and UTF-8 encoding style :-> "
+
PasswordEncryptUsingMD5_SHA.encryptUsingOtherAlgorithm(passwordToEncrypt,"SHA",
"UTF-8"));
System.out.println("Encrypted
Password Using SHA-1 Algorithm and UTF-16 encoding style :-> "
+
PasswordEncryptUsingMD5_SHA.encryptUsingOtherAlgorithm(passwordToEncrypt,"SHA-1",
"UTF-16"));
System.out.println("Encrypted
Password Using MD-5 Algorithm and UTF-8 encoding style :-> "
+
PasswordEncryptUsingMD5_SHA.encryptUsingOtherAlgorithm(passwordToEncrypt,"MD5",
"UTF-8"));
System.out.println("Encrypted
Password Using MD-5 Algorithm and UTF-16 encoding style :-> "
+
PasswordEncryptUsingMD5_SHA.encryptUsingOtherAlgorithm(passwordToEncrypt,"MD5",
"UTF-16"));
}
catch (Exception e) {
System.out
.println("Error
while password encryption and decryption is :-> "
+
e.getMessage());
}
}
}
Result:-
Encrypted Password Using SHA
Algorithm and UTF-8 encoding style :-> JEI4rXZtc37LO1sQDsSulbZ/pvw=
Encrypted Password Using SHA-1
Algorithm and UTF-16 encoding style :-> kXOnz+lWUoYzlOLXwHt/3J+qdCQ=
Encrypted Password Using MD-5
Algorithm and UTF-8 encoding style :-> 3i8hCnt+SgJ/mILHKJciqg==
Encrypted
Password Using MD-5 Algorithm and UTF-16 encoding style :->
PjNcbOwPBWBTaXoiN8gTFQ==
No comments:
Post a Comment