java sha256解密
时间: 2023-08-08 20:08:01 浏览: 176
SHA256是一种密码学哈希函数,它是不可逆的。因此,不能对其进行解密,只能进行哈希值的比对验证。在Java中,你可以使用以下代码来计算SHA256哈希值:
```java
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class SHA256Example {
public static void main(String[] args) throws NoSuchAlgorithmException {
String input = "Hello World!";
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
byte[] hash = messageDigest.digest(input.getBytes());
StringBuilder hexString = new StringBuilder();
for (byte b : hash) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
System.out.println("Hash value: " + hexString.toString());
}
}
```
上述代码将输出以下哈希值:
```
Hash value: 7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069
```
阅读全文