如何使用Java解码解密MD5加密

如何使用Java解码解密MD5加密,第1张

如何使用Java解码/解密MD5加密

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();    }  }}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5501578.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-12
下一篇2022-12-12

发表评论

登录后才能评论

评论列表(0条)

    保存