如何解密SHA-256加密的String? [英] How to decrypt SHA-256 encrypted String?

查看:187
本文介绍了如何解密SHA-256加密的String?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用下面的方法编码的字符串,有没有办法将这个字符串解码为其原始值?感谢。

I have a string that was encoded using the following method, is there a way to decode this string back to its original value? Thanks.

public synchronized String encode(String password)
        throws NoSuchAlgorithmException, IOException {

    String encodedPassword = null;
    byte[] salt = base64ToByte(saltChars);

    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    digest.reset();
    digest.update(salt);

    byte[] btPass = digest.digest(password.getBytes("UTF-8"));
    for (int i = 0; i < ITERATION_COUNT; i++) {
        digest.reset();
        btPass = digest.digest(btPass);
    }

    encodedPassword = byteToBase64(btPass);     
    return encodedPassword;
}

private byte[] base64ToByte(String str) throws IOException {

    BASE64Decoder decoder = new BASE64Decoder();
    byte[] returnbyteArray = decoder.decodeBuffer(str);

    return returnbyteArray;
}

private String byteToBase64(byte[] bt) {

    BASE64Encoder endecoder = new BASE64Encoder();
    String returnString = endecoder.encode(bt);

    return returnString;
}


推荐答案

SHA-256 是一种加密(单向) )散列函数,所以没有直接的方法来解码它。加密散列函数的整体目的是不能撤消它。

SHA-256 is a cryptographic (one-way) hash function, so there is no direct way to decode it. The entire purpose of a cryptographic hash function is that you can't undo it.

你可以做的一件事是 brute-force strategy ,你猜到什么被哈希,然后用相同的函数哈希,看看它是否匹配。除非散列数据非常容易猜到,否则可能需要一段时间。

One thing you can do is a brute-force strategy, where you guess what was hashed, then hash it with the same function and see if it matches. Unless the hashed data is very easy to guess, it could take a long time though.

您可能会发现问题散列密码和加密密码之间的差异有趣。

You may find the question "Difference between hashing a password and encrypting it" interesting.

这篇关于如何解密SHA-256加密的String?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆