Question

How can I reverse this output from this method? (So that I get to see the plaintext) I don't see any keys being used.

    public String encrypt(String plaintext) {
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("SHA");
    } catch (NoSuchAlgorithmException e) {
    }
    try {
        md.update(plaintext.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
    }
    byte raw[] = md.digest();
    String hash = (new BASE64Encoder()).encode(raw);
    return hash;
}
Was it helpful?

Solution

You can't. The SHA family are hash algorithms; that is, they are one-way cryptographic algorithms producing a hash or message digest. There is no way back. A hash is a fixed length result from a message of any length, with the design aim that it is computationally infeasible to find two messages that produce the same hash value; thus, to all intents, the hash can be seen as identifying the message. But you can't go back and retrieve the message from it.

Hashes are unkeyed. There are keyed constructs that can be made with them, for example a message authentication code or MAC.

The method is actually poorly named - this is not "encryption" per se.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top