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

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

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java AES加密解密的簡單實現方法

Java AES加密解密的簡單實現方法

2020-11-13 10:44Java教程網 Java教程

下面小編就為大家帶來一篇Java AES加密解密的簡單實現方法。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

廢話不多說,直接上代碼

?
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
package com.mstf.aes;
 
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
 
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
 
/**
 * @author ceet
 *
 */
public class AESUntil {
 /**
  * 加密
  *
  *
  */
 public static String Ecodes(String content, String key) {
  if (content == null || content.length() < 1)
   return null;
 
  try {
   KeyGenerator kgen = KeyGenerator.getInstance("AES");
   kgen.init(128, new SecureRandom(key.getBytes()));
   SecretKey secretKey = kgen.generateKey();
   byte[] enCodeFormat = secretKey.getEncoded();
   SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
   Cipher cipher = Cipher.getInstance("AES");
   byte[] byteContent = content.getBytes("utf-8");
   cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
   byte[] byteRresult = cipher.doFinal(byteContent);
   StringBuffer sb = new StringBuffer();
   for (int i = 0; i < byteRresult.length; i++) {
    String hex = Integer.toHexString(byteRresult[i] & 0xFF);
    if (hex.length() == 1) {
     hex = '0' + hex;
    }
    sb.append(hex.toUpperCase());
   }
   return sb.toString();
  } catch (NoSuchAlgorithmException e) {
   e.printStackTrace();
  } catch (NoSuchPaddingException e) {
   e.printStackTrace();
  } catch (InvalidKeyException e) {
   e.printStackTrace();
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  } catch (IllegalBlockSizeException e) {
   e.printStackTrace();
  } catch (BadPaddingException e) {
   e.printStackTrace();
  }
  return null;
 }
 
 /**
  * 解密
  *
  *
  */
 public static String Dcodes(String content, String key) {
  if (content == null || content.length() < 1)
   return null;
 
  if (content.trim().length() < 19)
   return content;
 
  byte[] byteRresult = new byte[content.length() / 2];
  for (int i = 0; i < content.length() / 2; i++) {
   int high = Integer.parseInt(content.substring(i * 2, i * 2 + 1), 16);
   int low = Integer.parseInt(content.substring(i * 2 + 1, i * 2 + 2), 16);
   byteRresult[i] = (byte) (high * 16 + low);
  }
  try {
   KeyGenerator kgen = KeyGenerator.getInstance("AES");
   kgen.init(128, new SecureRandom(key.getBytes()));
   SecretKey secretKey = kgen.generateKey();
   byte[] enCodeFormat = secretKey.getEncoded();
   SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
   Cipher cipher = Cipher.getInstance("AES");
   cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
   byte[] result = cipher.doFinal(byteRresult);
   return new String(result);
  } catch (NoSuchAlgorithmException e) {
   e.printStackTrace();
  } catch (NoSuchPaddingException e) {
   e.printStackTrace();
  } catch (InvalidKeyException e) {
   e.printStackTrace();
  } catch (IllegalBlockSizeException e) {
   e.printStackTrace();
  } catch (BadPaddingException e) {
   e.printStackTrace();
  }
  return null;
 }
 
 /**
  * 詳細解釋
  * 【ceet為加密的密匙】
  * 【admin為需要加密的字符串】
  * 【67BE5ED967DBA9B9810C295BE6DEF5D5為解密后的字符串】
  * 【如果更改ceet,那么67BE5ED967DBA9B9810C295BE6DEF5D5字符串會發生變化】
  * @param args
  */
 // 調用測試
 public static void main(String[] args) {
  System.out.println("需要加密的內容:"+Ecodes("admin", "ceet"));
  System.out.println("經過解密的內容:"+Dcodes("67BE5ED967DBA9B9810C295BE6DEF5D5", "ceet"));
 }
}

以上這篇Java AES加密解密的簡單實現方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 在线看一区 | 亚洲一区二区 | 日本激情综合网 | 日韩一区二区影视 | 成av在线| 日韩av高清在线 | 毛片大全 | 精品国产乱码一区二区三区 | 欧美精品三区 | 国产成人一区 | 国产美女久久久 | 在线播放视频一区 | 中文字幕1区2区3区 亚洲欧美日韩精品久久亚洲区 | 麻豆产精国品免费入口 | 久久天天躁狠狠躁夜夜免费观看 | 天天色天天色 | 激情久久久 | www久久久| 污污视频网址 | 国产午夜精品一区二区三区嫩草 | 久久久久久久久成人 | 成人午夜小视频 | 美女h视频 | 久久久久久久久国产成人免费 | 成人欧美一区二区三区色青冈 | 久久机热 | 懂色av中文字幕一区二区三区 | 国产亚洲一区二区三区在线观看 | 亚洲免费不卡视频 | 久久免费视频3 | 亚洲精品久久久久久下一站 | 国产一区二区三区四 | 亚洲视频免费 | 精品国产91久久 | 久久人爽 | 中文字幕一区二区三 | 美女视频一区二区三区 | 国产毛片毛片 | 国产一级片儿 | 91高清在线| 久久久久亚洲 |