zoukankan      html  css  js  c++  java
  • MD5 加密

    数据加密:

      我们在做关于私人信息时,我们总要使用到加密,特别是密码加密。如果我们的系统被黑客攻破了,他可以看见我们的全部信息。如果我们使用加密技术,即使他攻破了也无法拿到我们的正真信息,因为我们使用了加密技术,加密后的密码是不可逆。

      我的加密代码如下:

    import java.io.UnsupportedEncodingException;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    
    /**
     * 这是实现 MD5 数据加密的算法
     * 
     * @author TongZhou
     *
     */
    public class EncodeByMd5 {
        /**
         * 
         * @param str 传入的参数
         * @return 返回加密后的字符串
         * @throws NoSuchAlgorithmException
         * @throws UnsupportedEncodingException
         */
        public static String EncoderByMd5(String string) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    
            char hexDigits[] = {
                    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
            };
            try {
                byte[] btInput = string.getBytes();
                // 获得MD5摘要算法的 MessageDigest 对象
                MessageDigest mdInst = MessageDigest.getInstance("MD5");
                // 使用指定的字节更新摘要
                mdInst.update(btInput);
                // 获得密文
                byte[] md = mdInst.digest();
                // 把密文转换成十六进制的字符串形式
                int j = md.length;
                char str[] = new char[j * 2];
                int k = 0;
                for (int i = 0; i < j; i++) {
                    byte byte0 = md[i];
                    str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                    str[k++] = hexDigits[byte0 & 0xf];
                }
                return new String(str);
            } catch (Exception e) {
                return null;
            }
        }
    }
  • 相关阅读:
    hlgoj 1766 Cubing
    Reverse Linked List
    String to Integer
    Bitwise AND of Numbers Range
    Best Time to Buy and Sell Stock III
    First Missing Positive
    Permutation Sequence
    Next Permutation
    Gray Code
    Number of Islands
  • 原文地址:https://www.cnblogs.com/gzbit-zxx/p/7598835.html
Copyright © 2011-2022 走看看