MD5是 哈希
(即单向转换),因此您无法对其进行解密。您可以将已知哈希与从明文计算出的哈希进行比较,以验证输入的有效性。Java已经为此内置了库。我在下面使用了此代码,可以根据需要进行调整,还请验证您在javascript中生成的哈希具有相同的编码。
import java.security.MessageDigest; import sun.misc.base64Enprer;public class MD5Sample { private final MessageDigest md; public MD5Sample() throws SecurityException { try { md = MessageDigest.getInstance("MD5", "SUN"); }catch(Exception se) { throw new SecurityException("In MD5 constructor " + se); } } public String enpre(String in) throws Exception { if (in == null) { return null; } try { byte[] raw = null; byte[] stringBytes = null; stringBytes = in.getBytes("UTF8"); synchronized(md) { raw = md.digest(stringBytes); } base64Enprer enprer = new base64Enprer(); return enprer.enpre(raw); } catch (Exception se) { throw new Exception("Exception while encoding " + se); } } public String depre(String in) { throw new RuntimeException("NOT SUPPORTED"); } public static void main(String[] args) { String clearText = "apple"; try { MD5Sample app = new MD5Sample(); String encryptedHash = app.enpre(clearText); System.out.println(encryptedHash); } catch (Exception e) { e.printStackTrace(); } }}


