简体   繁体   English

DecodeBase64使用apache commons

[英]DecodeBase64 using apache commons

I am using a java class CryptoSHA1BASE64.java to encrypt a plain text to sha1Base64 key 我使用java类CryptoSHA1BASE64.java将纯文本加密到sha1Base64密钥

String result = CryptoSHA1BASE64.hash(text); 字符串结果= CryptoSHA1BASE64.hash(text);

The code of the class - CryptoSHA1BASE64.java is 类的代码 - CryptoSHA1BASE64.java是

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.servlet.ServletException;

public final class CryptoSHA1BASE64 {
  public static String hash(String plaintext) throws ServletException {
    MessageDigest md = null;

    try {
      md = MessageDigest.getInstance("SHA"); // SHA-1 generator instance
    } catch (NoSuchAlgorithmException e) {
      throw new ServletException(e.getMessage());
    }

    try {
      md.update(plaintext.getBytes("UTF-8")); // Message summary
      // generation
    } catch (UnsupportedEncodingException e) {
      throw new ServletException(e.getMessage());
    }

    byte raw[] = md.digest(); // Message summary reception
    try {
      String hash = new String(org.apache.commons.codec.binary.Base64.encodeBase64(raw), "UTF-8");
      return hash;
    } catch (UnsupportedEncodingException use) {
      throw new ServletException(use);
    }
  }
}

I want to decrypt the generated key back to plain text, i tried same approach ie, the decrypt method of apache commons - 我想将生成的密钥解密回纯文本,我尝试了相同的方法,即apache commons的解密方法 -

Base64.decodeBase64(key) Base64.decodeBase64(钥匙)

But, I gest some weird character instead of plain text. 但是,我提出一些奇怪的字符而不是纯文本。 Any suggestions/comments would be of great help. 任何建议/意见都会有很大帮助。 Thanks!!! 谢谢!!!

Hashing algorithms are one way. 散列算法是一种方法。 You can't undigest the string to get the original text. 您无法取消删除字符串以获取原始文本。 That is why these algorithms are used for passwords before they are stored in a database for example, so that even if the database is hacked, you can't get the original text. 这就是为什么这些算法在存储在数据库中之前用于密码的原因,因此即使数据库被黑客攻击,也无法获得原始文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM