国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java實現DES加密與解密,md5加密以及Java實現MD5加密解密類

Java實現DES加密與解密,md5加密以及Java實現MD5加密解密類

2020-01-16 16:38mrr JAVA教程

這篇文章主要介紹了Java實現DES加密與解密,md5加密以及Java實現MD5加密解密類 ,需要的朋友可以參考下

很多時候要對秘要進行持久化加密,此時的加密采用md5。采用對稱加密的時候就采用DES方法了

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import java.io.IOException;
  import java.security.MessageDigest;
  import java.security.SecureRandom;
  import javax.crypto.Cipher;
  import javax.crypto.SecretKey;
  import javax.crypto.SecretKeyFactory;
  import javax.crypto.spec.DESKeySpec;
 import sun.misc.BASEDecoder;
 import sun.misc.BASEEncoder;
 /**
  * 密匙工具類(包含des加密與md加密)
  * @author mingge
  *
  */
 public class KeysUtil {
     private final static String DES = "DES";
     private final static String MD = "MD";
     private final static String KEY="opeddsaeaddadbcabf";
     /**
      * MD加密算法
      * @param data
      * @return
      */
     public static String mdEncrypt(String data) {
       String resultString = null;
       try {
         resultString = new String(data);
         MessageDigest md = MessageDigest.getInstance(MD);
         resultString =bytehexString(md.digest(resultString.getBytes()));
       } catch (Exception ex) {
       }
       return resultString;
     }
     private static String bytehexString(byte[] bytes) {
       StringBuffer bf = new StringBuffer(bytes.length * );
       for (int i = ; i < bytes.length; i++) {
         if ((bytes[i] & xff) < x) {
           bf.append("T");
         }
         bf.append(Long.toString(bytes[i] & xff, ));
       }
       return bf.toString();
     }
     /**
      * Description 根據鍵值進行加密
      * @param data
      * @param key 加密鍵byte數組
      * @return
      * @throws Exception
      */
     public static String desEncrypt(String data, String key) throws Exception {
       if (key==null) {
         key=KEY;
       }
       byte[] bt = encrypt(data.getBytes(), key.getBytes());
       String strs = new BASEEncoder().encode(bt);
       return strs;
     }
     /**
      * Description 根據鍵值進行解密
      * @param data
      * @param key 加密鍵byte數組
      * @return
      * @throws IOException
      * @throws Exception
      */
     public static String desDecrypt(String data, String key) throws IOException,
         Exception {
       if (data == null){
         return null;
       }
       if (key==null) {
         key=KEY;
       }
       BASEDecoder decoder = new BASEDecoder();
       byte[] buf = decoder.decodeBuffer(data);
       byte[] bt = decrypt(buf,key.getBytes());
       return new String(bt);
     }
     /**
      * Description 根據鍵值進行加密
      * @param data
      * @param key 加密鍵byte數組
      * @return
      * @throws Exception
      */
     private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
       // 生成一個可信任的隨機數源
       SecureRandom sr = new SecureRandom();
       // 從原始密鑰數據創建DESKeySpec對象
       DESKeySpec dks = new DESKeySpec(key);
       // 創建一個密鑰工廠,然后用它把DESKeySpec轉換成SecretKey對象
       SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
       SecretKey securekey = keyFactory.generateSecret(dks);
       // Cipher對象實際完成加密操作
       Cipher cipher = Cipher.getInstance(DES);
       // 用密鑰初始化Cipher對象
       cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
       return cipher.doFinal(data);
     }
     /**
     * Description 根據鍵值進行解密
     * @param data
     * @param key 加密鍵byte數組
     * @return
     * @throws Exception
     */
     private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
       // 生成一個可信任的隨機數源
       SecureRandom sr = new SecureRandom();
       // 從原始密鑰數據創建DESKeySpec對象
       DESKeySpec dks = new DESKeySpec(key);
       // 創建一個密鑰工廠,然后用它把DESKeySpec轉換成SecretKey對象
       SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
       SecretKey securekey = keyFactory.generateSecret(dks);
       // Cipher對象實際完成解密操作
       Cipher cipher = Cipher.getInstance(DES);
       // 用密鑰初始化Cipher對象
       cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
       return cipher.doFinal(data);
     }
 }

下面在給大家介紹一段代碼關于Java實現MD5加密解密類

Java實現MD5加密以及解密類,附帶測試類,具體見代碼。

MD5加密解密類——MyMD5Util,代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.zyg.security.md5;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;
public class MyMD5Util {
  private static final String HEX_NUMS_STR="0123456789ABCDEF";
  private static final Integer SALT_LENGTH = 12;
  /**
   * 將16進制字符串轉換成字節數組
   * @param hex
   * @return
   */
  public static byte[] hexStringToByte(String hex) {
    int len = (hex.length() / 2);
    byte[] result = new byte[len];
    char[] hexChars = hex.toCharArray();
    for (int i = 0; i < len; i++) {
      int pos = i * 2;
      result[i] = (byte) (HEX_NUMS_STR.indexOf(hexChars[pos]) << 4
              | HEX_NUMS_STR.indexOf(hexChars[pos + 1]));
    }
    return result;
  }
  /**
   * 將指定byte數組轉換成16進制字符串
   * @param b
   * @return
   */
  public static String byteToHexString(byte[] b) {
    StringBuffer hexString = new StringBuffer();
    for (int i = 0; i < b.length; i++) {
      String hex = Integer.toHexString(b[i] & 0xFF);
      if (hex.length() == 1) {
        hex = '0' + hex;
      }
      hexString.append(hex.toUpperCase());
    }
    return hexString.toString();
  }
  /**
   * 驗證口令是否合法
   * @param password
   * @param passwordInDb
   * @return
   * @throws NoSuchAlgorithmException
   * @throws UnsupportedEncodingException
   */
  public static boolean validPassword(String password, String passwordInDb)
      throws NoSuchAlgorithmException, UnsupportedEncodingException {
    //將16進制字符串格式口令轉換成字節數組
    byte[] pwdInDb = hexStringToByte(passwordInDb);
    //聲明鹽變量
    byte[] salt = new byte[SALT_LENGTH];
    //將鹽從數據庫中保存的口令字節數組中提取出來
    System.arraycopy(pwdInDb, 0, salt, 0, SALT_LENGTH);
    //創建消息摘要對象
    MessageDigest md = MessageDigest.getInstance("MD5");
    //將鹽數據傳入消息摘要對象
    md.update(salt);
    //將口令的數據傳給消息摘要對象
    md.update(password.getBytes("UTF-8"));
    //生成輸入口令的消息摘要
    byte[] digest = md.digest();
    //聲明一個保存數據庫中口令消息摘要的變量
    byte[] digestInDb = new byte[pwdInDb.length - SALT_LENGTH];
    //取得數據庫中口令的消息摘要
    System.arraycopy(pwdInDb, SALT_LENGTH, digestInDb, 0, digestInDb.length);
    //比較根據輸入口令生成的消息摘要和數據庫中消息摘要是否相同
    if (Arrays.equals(digest, digestInDb)) {
      //口令正確返回口令匹配消息
      return true;
    } else {
      //口令不正確返回口令不匹配消息
      return false;
    }
  }
  /**
   * 獲得加密后的16進制形式口令
   * @param password
   * @return
   * @throws NoSuchAlgorithmException
   * @throws UnsupportedEncodingException
   */
  public static String getEncryptedPwd(String password)
      throws NoSuchAlgorithmException, UnsupportedEncodingException {
    //聲明加密后的口令數組變量
    byte[] pwd = null;
    //隨機數生成器
    SecureRandom random = new SecureRandom();
    //聲明鹽數組變量
    byte[] salt = new byte[SALT_LENGTH];
    //將隨機數放入鹽變量中
    random.nextBytes(salt);
    //聲明消息摘要對象
    MessageDigest md = null;
    //創建消息摘要
    md = MessageDigest.getInstance("MD5");
    //將鹽數據傳入消息摘要對象
    md.update(salt);
    //將口令的數據傳給消息摘要對象
    md.update(password.getBytes("UTF-8"));
    //獲得消息摘要的字節數組
    byte[] digest = md.digest();
    //因為要在口令的字節數組中存放鹽,所以加上鹽的字節長度
    pwd = new byte[digest.length + SALT_LENGTH];
    //將鹽的字節拷貝到生成的加密口令字節數組的前12個字節,以便在驗證口令時取出鹽
    System.arraycopy(salt, 0, pwd, 0, SALT_LENGTH);
    //將消息摘要拷貝到加密口令字節數組從第13個字節開始的字節
    System.arraycopy(digest, 0, pwd, SALT_LENGTH, digest.length);
    //將字節數組格式加密后的口令轉化為16進制字符串格式的口令
    return byteToHexString(pwd);
  }
}

測試類——Client,代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.zyg.security.md5;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
public class Client {
  private static Map users = new HashMap();
  public static void main(String[] args){
    String userName = "zyg";
    String password = "123";
    registerUser(userName,password);
    userName = "changong";
    password = "456";
    registerUser(userName,password);
    String loginUserId = "zyg";
    String pwd = "1232";
    try {
      if(loginValid(loginUserId,pwd)){
        System.out.println("歡迎登陸!!!");
      }else{
        System.out.println("口令錯誤,請重新輸入!!!");
      }
    } catch (NoSuchAlgorithmException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    
  }
  /**
   * 注冊用戶
   *
   * @param userName
   * @param password
   */
  public static void registerUser(String userName,String password){
    String encryptedPwd = null;
    try {
      encryptedPwd = MyMD5Util.getEncryptedPwd(password);
      users.put(userName, encryptedPwd);
    } catch (NoSuchAlgorithmException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  /**
   * 驗證登陸
   *
   * @param userName
   * @param password
   * @return
   * @throws UnsupportedEncodingException
   * @throws NoSuchAlgorithmException
   */
  public static boolean loginValid(String userName,String password) 
        throws NoSuchAlgorithmException, UnsupportedEncodingException{
    String pwdInDb = (String)users.get(userName);
    if(null!=pwdInDb){ // 該用戶存在
        return MyMD5Util.validPassword(password, pwdInDb);
    }else{
      System.out.println("不存在該用戶!!!");
      return false;
    }
  }
}

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲精品一区二区 | 日韩成人中文字幕 | 成人免费av | 日韩欧美中文字幕在线视频 | 国产成人精品一区二区三区四区 | 久草电影在线 | 欧美精品一二区 | 日韩精品小视频 | 欧美一级全黄 | 日韩影音| 精品少妇一区二区三区日产乱码 | 激情五月综合 | 欧美午夜精品一区二区三区电影 | 看黄免费 | 国产黄色片免费 | 国产成人综合av | 精品无码久久久久久国产 | 欧美一级一区 | 美女视频黄的免费 | 老师的朋友2 | 日韩三级电影在线免费观看 | 天天干天天搞天天射 | 成人国产电影 | av网址在线| 国产午夜精品一区二区三区免费 | 精品国内视频 | 欧美成人久久 | 自拍视频在线观看 | 欧美国产精品一区二区 | 久操视频免费在线观看 | 欧美 日韩 成人 | 波多野结衣一区二区三区 | 欧美在线日韩 | 成人精品福利 | 日韩中文在线观看 | 天天操天天操 | 韩日精品视频 | 午夜影晥 | 精品久久99 | 精品久 | 久久综合一 |