头图

Implementation principle of MD5 encryption algorithm

  • of MD5 encryption algorithm in Java:

    public class MD5 {
      // 全局数组
      private final static String[] strDigit = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
    
      public class MD5 {
      }
    
      // 返回形式为数字和字符串
      private static String byteToArrayString(byte bByte) {
          int iRet = bByte;
          if (iRet < 0) {
              iRet += 256;
          }
          int iD1 = iRet / 16;
          int iD2 = iRet % 16;
          return strDigits[iD1] + strDigits[iD2];
      }    
    
      // 返回形式只为数字
      private static String byteToNum(byte bByte) {
          int iRet = bByte;
          if (iRet < 0) {
              iRet += 256;
          }
          return String.valueOf(iRet);
      }
    
      // 将字节数组转换成为16进制的字符串
      private static String byteToString(byte[] bByte) {
          StringBuffer stringBuffer = new StringBuffer();
          for (int i; i < bByte.length; i++) {
              StringBuffer.append(byteToArrayString(bByte[i]));
          }
          return stringBuffer.toString();
      }
    
      // 获取MD5值
      public static String GetMD5Code(String strObj) {
          String resultString = null;
          try {
              resultString = new String();
              MessageDigest md5 = MessageDigest.getInstance("MD5");
              // md5.digest() - 返回值为存放Hash值结果的byte数组
              resultString = byteToString(md5.digest(strObj.getBytes()));
          } catch (NoSuchAlgorithmException e) {
              e.printStackTrace();
          }
          return resultString;
      }
    } 

    MessageDigest class

  • MessageDigest class:

    • Provides the function of information digest algorithm for applications. Such as MD5 algorithm and SHA algorithm
    • The message digest is a secure one-way Hash function : Receives data of any size and outputs a fixed-length Hash value

update

  • The MessageDigest object is initialized at the beginning
  • The object processes data by calling update() method

    /**
     * 使用指定的byte数组更新摘要
     *
     * @param input 指定的byte数组
     */
    public void update(byte[] input);

    reset

  • The digest can be reset at any time by calling reset() method

    digest

  • Once the data to be updated has been updated, the digest() method should be called to complete the Hash calculation
  • For a given amount of updated data , the digest() method can only be called once. After calling the digest() method, the MessageDigest object is reset to its initial state

    /**
     * 通过执行诸如填充之类的最终操作完成Hash计算. 
     * 在调用此方法之后,摘要被重置
     *
     * @return byte[] Hash计算后的byte数组
     */
    public byte[] digest();

    isEqual

    /**
     * 比较两个摘要的相等性.做简单的字节比较
     *
     * @param digestA 比较的摘要字节数组A
     * @param digestB 比较的摘要字节数组B
     * @return boolean 是否相等
     */
    public static boolean isEqual(byte[] digestA, byte[] digestB);

    getInstance

  • Returns a MessageDigest object implementing the specified digest algorithm

    /**
     * 返回实现指定摘要算法的MessageDigest对象
     *
     * @param algorithm 请求的算法的名称
     * @param provider 提供者名称
     * @return MessageDigest 指定摘要算法的MessageDigest对象
     * @throws NoSuchAlgorithmException 当指定的请求算法名称不存在时抛出异常
     */
    public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException; 
    
    /**
     * 返回实现指定摘要算法的MessageDigest对象
     *
     * @param algorithm 请求算法的名称
     * @return MessageDigest 指定摘要算法的MessageDigest对象
     * @throws NoSuchAlgorithmException 当指定的请求算法名称不存在时抛出异常
     */
    public static MessageDigest getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException;
  • Provider The list of registered providers can be obtained through the getProviders() method of java.security.Security
  • Common algorithms provided by SUN:

    • MD2
    • MD5
    • SHA-1
    • SHA-256
    • SHA-384
    • SHA-512

      MD5 encryption algorithm for strings

  • Use Java's own MessageDigest to implement the MD5 encryption algorithm for text:

    /**
     * 将字符串转换为MD5
     */
     public class ParseMD5 {
       public static String parseStrToMd5L32(String str) {
           // 将字符串转换为32位小写MD5 
           String reStr = null;
           try {
               MessageDigest md5 = MessageDigest.getInstance("MD5");
               byte[] bytes = md5.digest(str.getBytes());
               StringBuffer stringBuffer = new StringBuffer();
               for (byte b : bytes) {
                   int bt = b&0xff;
                   if (bt < 16) {
                       stringBuffer.append(0);
                   }
                   stringBuffer.append(Integer.toHexString(bt));
               }
               reStr = stringBuffer.toString();
           } catch (NoSuchAlgorithmException e) {
               e.printStackTrace();
           }
           return reStr;
       }
    
      // 将字符串转换为32位大写的MD5
      public static String parseStrToMd5U32(String str) {
          String reStr = parseStrToMd5L32(str);
          if (reStr != null) {
              reStr = reStr.toUpperCase();
          }
          return resStr;
      }
    
      // 将字符串转换为16位小写的MD5
      public static String parseStrToMd5L16(String str) {
          String reStr = paseStrToMd5L32(str);
          if (reStr != null) {
              reStr = reStr.subString(8, 24);
          }
          return reStr;
      }
    
      // 将字符串转换为16位大写的MD5
      public static String parseStrToMd5U16(String str) {
          String reStr = parseStrToMd5L32(str);
          if (reStr != null) {
              reStr = reStr.toUpperCase().subString(8, 24);
          }
          return reStr;
      }
     }

    MD5 encryption tool for text

  • Java provides the built-in MessageDigest to implement the encryption algorithm for text. The MD5 encryption tool class for encrypting text is as follows:

    public class MD5Util {
      // 将文本转换为32位小写的MD5
      public static String textToMd5L32(String plainText) {
          String result = null;
          // 判断需要转换的文本是否为空
          if (StringUtils.isBlank(plainText)) {
              return null;
          }
          try {
              // 进行实例化和初始化
              MessageDigest md5 = MessageDigest.getInstance("MD5");
              // 得到一个操作系统默认的字节编码格式的字节数组
              byte[] byteInput = plainText.getBytes();
              // 对得到的字节数组进行处理
              md5.update(byteInput);
              // 进行Hash计算并得到返回结果
              byte[] btResult = md5.digest();
              // 得到进行Hash计算后数据的长度
              StringBuffer stringBuffer = new StringBuffer();
              for (byte b : btResult) {
                  int bt = b&0xff;
                  if (bt < 16) {
                      stringBuffer.append(0);
                  }
                  stringBuffer.append(Integer.toHexString(bt));
              }
              reStr = stringBuffer.toString();
          } catch (NoSuchAlgorithmException e) {
              e.printStackTrace();
          }
          return reStr;
      }
    
      // 将文本转换为32位大写的MD5
      public static String textToMd5U32(String plainText) {
          if (StringUtils.isBlank(plainText)) {
              return null;
          }
          String result = textToMd5L32(plainText);
          result = result.toUpperCase();
          return result;
      }    
    }

攻城狮Chova
67 声望8 粉丝

一位有自我修养的攻城狮。