×

SHA Decrypt in Java

In this section, we will be acknowledged about the decryption of SHA in Java.

Java SHA

The SHA cryptographic hash algorithm in cryptography outputs the hash value as an approximately 40-digit-long hexadecimal number after accepting 20 bytes as input.

Secure Hash Algorithm is referred to as SHA. One of the most popular cryptographic hash functions in Java is SHA. Using a cryptographic hash, a text sign or data file can be produced. However, SHA is nothing more than a cryptographic algorithm which accepts input up to 20 bytes long and produces a hash result in hexadecimal notation that is roughly 40 digits long. The SHA-256 technique may generate 256-bit (32-byte) hash values that are almost entirely unique and non-static. But it's important to keep in mind that this function is only one-way, meaning the result it generates cannot be reverse-encrypted to reveal its original value.

After SHA-2, SHA-3 is the most recent safe hashing standard. Compared to SHA-2, SHA-3 offers a novel method for creating a distinctive one-way hash and can be considerably quicker on some embedded applications. Similar to the SHA-256 method, the SHA3-256 approach has a constant length of 256 bits.

Java offers the MessageDigest class to implement the SHA-256 algorithm.

Message Digest Class

Java uses the MessageDigest class to determine the cryptographic hashing value. The java.security package contains it.

The MessageDigest Class's cryptographic hash methods for generating a text's hash value are listed below. They are:

  • SHA-1
  • MD5
  • SHA-256

The static procedure getInstance is where these algorithms are introduced ( ). After choosing an algorithm, the digest value is calculated, and the output is returned in the form of a byte array.

With the aid of the "BigInteger" class, the output byte array is transformed into its sign-magnitude representation. The representation is then transformed to hexadecimal representation to obtain the "MessageDigest" after that.

Examples

Example1

Input: hello word

Output: b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

Example2

Input: K1t4fo0V

Output: 0a979e43f4874eb24b740c0157994e34636eed0425688161cc58e8b26b1dcf4e

Example3

Input: Just an Example

Output: 284cdc2bb78a70e5661bb5214534593fad996635e7f0554d885c1c27dafac9a2

Now let us write the program code that decrypts the hash code or hash value for certain string.

File name: SHA.java

// Java programme that determines the SHA hash value
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


class SHA {
public static byte[] getSHA(String input) throws NoSuchAlgorithmException
{
// With SHA hashing, the static getInstance function is called.
MessageDigest md = MessageDigest.getInstance("SHA-256");
// Message digest of such an entry is calculated using the digest() method, which returns an //array of bytes.
return md.digest(input.getBytes(StandardCharsets.UTF_8));
}

public static String toHexString(byte[] hash)
{
// byte array to signum representation conversion
BigInteger number = new BigInteger(1, hash);


// message digest -> hex value conversion
StringBuilder hexString = new StringBuilder(number.toString(16));


// Add leading zeros to the pad
while (hexString.length() < 64)
{
hexString.insert(0, '0');
}


return hexString.toString();
}


public static void main(String args[])
{
try
{
System.out.println("HashCode Generated by SHA-256 for:");


String s1 = "Just an Example ";
System.out.println("\n" + s1 + " : " + toHexString(getSHA(s1)));


String s2 = "hello world";
System.out.println("\n" + s2 + " : " + toHexString(getSHA(s2)));

String s3 = "K1t4fo0V";
System.out.println("\n" + s3 + " : " + toHexString(getSHA(s3)));
}
// due to the inappropriate message digest algorithms being specified
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown for incorrect algorithm: " + e);
}
}
}

Output:

SHA-256 generated a hash values for:


Just an Example:
284cdc2bb78a70e5661bb5214534593fad996635e7f0554d885c1c27dafac9a2


hello world: b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9


K1t4fo0V:
a979e43f4874eb24b740c0157994e34636eed0425688161cc58e8b26b1dcf4e

Advantages

Technology pioneers mostly employ SHA-256 because, unlike some other well-liked hashing algorithms, it has not been "broken" and has no known security flaws.

We use SHA-256 over other popular hashing algorithms because its 256-bit key is significantly more secure. The main advantages of SHA-256, without delving into far to much specific information, are as follows:

  • It's an established, reliable industrial standard:

Leading public-sector organisations rely on the industry standard SHA-256, which is also extensively adopted by top technological experts.

  • Collisions are very unlikely to occur:

When employing SHA-256, there are 2256 potential hash values, making it nearly unlikely for two distinct documents to accidentally have the same hash value.

  • The impact of an avalanche:

Contrary to certain older hashing algorithms, the avalanche effect causes even very small modifications to the original data to drastically alter the hash result.


Related Topics

How to Convert Object to String in Java

How to Convert Object to String in Java You can convert any Object to String in Java whether it is a user-defined class, StringBuilder or StringBuffer, etc. There are two methods...

2 minutes read.

Difference between = = and equals ( ) in java

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

6 minutes read.

Interface Program in Java

Interface Program in Java In the previous topic, we discussed that abstraction is possible through the interface and abstract class. An abstract class provides partial to 100% abstraction. 100 %abstraction in...

4 minutes read.

Java try catch

Java try catch There can be statements that can cause exceptions, and the exception leads to abnormal termination of the program. To avoid that abnormal termination, those statements that look potent...

4 minutes read.

How to run Java Program in Eclipse

How to run Java Program in Eclipse In this section, we will learn how to write, save, compile, and execute or run a Java program in Eclipse. Eclipse is one of...

2 minutes read.

URLConnection Class

What is the URL? URL stands for Uniform Resource Locator, is used to specify addresses on the World Wide Web. A URL relates to the identification of any resource connected to the web. URL syntax: Protocol://hostname/other_information(files...

6 minutes read.

Java Pi

What is Pi? There are many formulas in geometry that employ the Pi constant to calculate things like circumference, area, and volume. A circle's circumference divided by its diameter yields a...

3 minutes read.

Java String Methods

Java String Methods Java String class is the most important class of the java.lang package. It is used to handle the String related operations. It contains a lot of built-in Java...

2 minutes read.

Object class in Java

In Java, a class is a file containing the Java byte code. It can essentially specifically be executed on the JVM (Java Virtual Machine), fairly significant. The JVM mostly generally...

6 minutes read.

Java file Writer

File Writer: It is used to write all the data from text files. It inherits the properties from the Output Stream class. It is meant for writing characters. It is meant...

4 minutes read.

Reserved Keywords in Java

In Java, a reserved term is used as a code key called a keyword. Because they are predefined, these terms cannot be used for anything else. They cannot serve as...

3 minutes read.

Minimum Number of Platforms Required for a Railway Station

The train station problem is one of the most significant problems typically posed in the programming round interview to gauge a candidate's aptitude for logic and problem-solving. Problem of Railway Station The...

6 minutes read.

Synchronized Keyword in Java

Synchronization is the process of limiting access to a shared resource or data to a single thread at a given moment in time. This aids in shielding the data from...

6 minutes read.

How to convert list to String in Java

Sometimes, we need to transform a listing of characters into a string. A string is a chain of characters, so we will make a string from an individual array without...

4 minutes read.

Java String length() method

Java String length() method returns length of the characters in a String. Syntax: public int length() Returns It returns length of the characters Java String length() method example 1          public class JavaStringLengthEx1  ...

1 minute read.

Creation of Multi Thread in java

What are threads in Java? We can use threads to facilitate parallel processing. Threads are helpful when you wish to execute several pieces of code concurrently. A thread is a small process...

3 minutes read.

Crown Pattern in Java

We know the importance of solving pattern problems. We can solve pattern problems by using any programming language. There is no rule to translating into a particular programming language. We...

4 minutes read.

Java Integer valueOf() method

The valueOf() method of Java Integer class returns an Integer object holding the specified int value. The second method returns an Integer object holding the specified String value. The third syntax returns...

2 minutes read.

Statements in java

What is Statement in java: A statement in java is an instruction that explains what will happen based on the condition. Types of java statements: There are different statements in java Expression statementDeclaration statementControl...

2 minutes read.

Bubble Sort in Java

Bubble Sort in Java Bubble sort isalso known as sinking sort. It is one of the simplest sorting algorithms. In the bubble sort algorithm, the given array is traversed from left...

5 minutes read.