【发布时间】:2013-12-20 04:40:30
【问题描述】:
我正在使用 java 类 CryptoSHA1BASE64.java 将纯文本加密为 sha1Base64 密钥
字符串结果 = CryptoSHA1BASE64.hash(text);
类的代码——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);
}
}
}
我想将生成的密钥解密回纯文本,我尝试了相同的方法,即 apache commons 的解密方法 -
Base64.decodeBase64(key)
但是,我得到了一些奇怪的字符而不是纯文本。任何建议/cmets 都会有很大帮助。谢谢!!!
【问题讨论】:
-
由于某些限制,我无法更改编码部分,因此寻找解码 UTF-8 编码的解决方案
-
做不到。请参阅@ezadeen 以获取答案。
标签: java encryption base64 decode