×

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 check if date is valid in Java?

In this article, you will acknowledge about how to verify if a date is valid or not. For this you will learn the approach, you will be able to write...

3 minutes read.

Java Class Keyword

We know that java is object-oriented programming language which contains the essential key concepts such as classes and objects etc to have clear idea about the object-oriented programming.Java is mainly...

3 minutes read.

Tower of Hanoi Program in Java

Tower of Hanoi Program in Java The Tower of Hanoi program in Java is written to solve a mathematical puzzle, called Tower of Hanoi, where we have three poles and n...

4 minutes read.

Java Anon Proxy

The Java Anon Proxy (JAP), also known as JonDonym, is a proxy system designed to enable Web browsing with revocable (the use of or publication under a pseudonym, a false...

4 minutes read.

JDBC Program in Java

JDBC Program in Java JDBC is an API that defines how a client may access a database. It is a part of Java Standard Edition (Java SE). JDBC stands for Java...

4 minutes read.

Pangram Program in Java

If a string comprises all alphabet letters from A to Z or from a to z without regard to case, it is referred to as a pangram. Some examples of pangram...

3 minutes read.

if-else Program in Java

if-else Program in Java The if-else program in Java controls which code snippet will execute. The if-else program is very basic and yet very important. Because it checks how well one...

19 minutes read.

AbstractSet Class in Java

The AbstractSet class is used for the implementation of the Abstract Collection class and interface. It is the part of Collection Frameworks. In the AbstractSet, the implementation is same as the...

3 minutes read.

String Manipulation in Java

String Manipulation in Java In Java, string manipulation is a common task performed by programmer. Java String class provides many built-in functions that are used to manipulate string. The manipulation of...

4 minutes read.

Java Math getExponent() Method

The getExponent() method of Math class returns the unbiased exponent of the argument. Syntax: public static int getExponent (double d) Parameters: The parameter ‘d’ represents the double value. Return Value: The getExponent () method returns the...

1 minute read.

Deadlock in Java

Deadlock is when two or more processes wait for the state to do their tasks, but none of them can do so. It is a very common problem that one...

6 minutes read.

Java Extends vs Implements

Java: We known that the java is a pure object oriented programming language. Java programming language consists of many features such as portable, plat form independence, secured, robust, simple, architecture neutral,...

4 minutes read.

How to Set Environment Variables for Java

Introduction Java is an object-oriented programming language that is based on classes and can be employed mostly to develop web and desktop applications. No matter the computer architecture, Java applications are...

7 minutes read.

Java Math acos() Method

The acos() method of Math class computes the trigonometric Arc Cosine (inverse of cosine ) of an angle. The value returned is between 0.0 to pi. Syntax: public static double acos(double a) Parameters: The...

1 minute read.

Polymorphism Program in Java

Polymorphism Program in Java: Polymorphism means the existence of different forms of an object. The object can be a class object or a real-time entity. For example, a person can...

5 minutes read.

Zig Zag star and Number Pattern in Java

We covered many Java pattern applications in the preceding part. We will write Java applications for zigzag star and number patterns in this part. Printing Zig Zag Number Pattern Steps Print one...

3 minutes read.

Ad Hoc Problem on Arrays in Java

Ad hoc problems are issues that arise unexpectedly and require immediate attention. These problems can range from small and straightforward issues to more complex and time-consuming problems. Examples of ad...

3 minutes read.

Factorial Program in Java using Recursion

Factorial Program in Java Factorials are used in mathematics to calculate permutations and combinations. It is denoted by the exclamatory symbol (!). Suppose, p is a number whose factorial is to...

3 minutes read.

Java Code Coverage Tools

Code coverage testing is a crucial metric that gauges how thoroughly the program's source code has been tested. The market is flooded with Code Coverage Tools, making it difficult to...

7 minutes read.

Star Program in Java

By solving the patterns, we can develop our coding skills and logical thinking. Mostly each pattern program uses two or more loops. Loops' number depends on the complexity of logic....

3 minutes read.