0
我使用的Java類CryptoSHA1BASE64.java來加密明文sha1Base64鍵DecodeBase64使用Apache公地
字符串結果= CryptoSHA1BASE64.hash(文本);
類的代碼 - 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的百科全書的解密方法 -
Base64.decodeBase64(鍵)
但是,我武功一些奇怪的字符,而不是普拉伊的n文本。任何建議/意見將有很大的幫助。謝謝!!!
編碼部分是我不能改變,由於一些限制,所以尋找解決方案來解碼UTF-8編碼 –
可能的重複,http://stackoverflow.com/questions/3479067/how-to-decode- with-messagedigest-base64 – sasankad
這是無法完成的。見@ezzadeen的答案。 –