激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - Java實(shí)現(xiàn)的3des加密解密工具類(lèi)示例

Java實(shí)現(xiàn)的3des加密解密工具類(lèi)示例

2021-01-25 11:19CharlinGod Java教程

這篇文章主要介紹了Java實(shí)現(xiàn)的3des加密解密工具類(lèi),結(jié)合完整實(shí)例形式分析了3des加密解密的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Java實(shí)現(xiàn)的3des加密解密工具類(lèi)。分享給大家供大家參考,具體如下:

?
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package com.gcloud.common;
import org.apache.poi.poifs.property.Child;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.*;
import java.security.spec.AlgorithmParameterSpec;
/**
 * 三重?cái)?shù)據(jù)加密算法工具類(lèi)
 * Created by charlin on 2017/9/11.
 */
public class V3DESUtil {
  //密鑰存放位置
  public static String FILENAME = "d:/3des.key";
  // 1為加密,0為解密
  private int isEncrypt = -1;
  // 加密/解密密鑰,長(zhǎng)度為16byte或者24byte。
  private String keyStr;
  // 要加密/解密信息(解密時(shí)需為十六進(jìn)制顯示的字符串)
  private String message;
  public V3DESUtil() {
  }
  public V3DESUtil(int isEncrypt, String keyStr, String message) {
    this.isEncrypt = isEncrypt;
    this.keyStr = keyStr;
    this.message = message;
  }
  //numStr = 12345678
  public String V3DESChiper(String numStr) {
    String result = null;
    try {
      Security.addProvider(new BouncyCastleProvider());
      URL url = getClass().getResource(FILENAME);
      File myFile = new File(FILENAME);
      if (!myFile.exists()) {
        return "Can't Find " + FILENAME;
      }
      try {
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        while ((keyStr = in.readLine()) == null) {
          return "讀取密鑰失敗!";
        }
        in.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
      SecretKey key = new SecretKeySpec(keyStr.getBytes(), "DESede");
      result = null;
      byte[] textByte = null;
      byte[] messageByte = null;
      Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding", "BC");
      AlgorithmParameterSpec spec = new IvParameterSpec(numStr.getBytes());
      if (isEncrypt == 1) {
        messageByte = message.getBytes();
        cipher.init(Cipher.ENCRYPT_MODE, key, spec);
      } else if (isEncrypt == 0) {
        messageByte = decodeHex(message);
        cipher.init(Cipher.DECRYPT_MODE, key, spec);
      } else {
        return "加解密設(shè)置錯(cuò)誤,請(qǐng)確認(rèn)輸入:1為加密;0為解密";
      }
      textByte = cipher.doFinal(messageByte);
      if (isEncrypt == 1) {
        result = encodeHex(textByte);
      } else if (isEncrypt == 0) {
        result = new String(textByte);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return result;
  }
  public static final String encodeHex(byte bytes[]) {
    StringBuffer buf = new StringBuffer(bytes.length * 2);
    for (int i = 0; i < bytes.length; i++) {
      if ((bytes[i] & 0xff) < 16)
        buf.append("0");
      buf.append(Long.toString(bytes[i] & 0xff, 16));
    }
    return buf.toString();
  }
  public static final byte[] decodeHex(String hex) {
    char chars[] = hex.toCharArray();
    byte bytes[] = new byte[chars.length / 2];
    int byteCount = 0;
    for (int i = 0; i < chars.length; i += 2) {
      int newByte = 0;
      newByte |= hexCharToByte(chars[i]);
      newByte <<= 4;
      newByte |= hexCharToByte(chars[i + 1]);
      bytes[byteCount] = (byte) newByte;
      byteCount++;
    }
    return bytes;
  }
  private static final byte hexCharToByte(char ch) {
    switch (ch) {
      case 48: // '0'
        return 0;
      case 49: // '1'
        return 1;
      case 50: // '2'
        return 2;
      case 51: // '3'
        return 3;
      case 52: // '4'
        return 4;
      case 53: // '5'
        return 5;
      case 54: // '6'
        return 6;
      case 55: // '7'
        return 7;
      case 56: // '8'
        return 8;
      case 57: // '9'
        return 9;
      case 97: // 'a'
        return 10;
      case 98: // 'b'
        return 11;
      case 99: // 'c'
        return 12;
      case 100: // 'd'
        return 13;
      case 101: // 'e'
        return 14;
      case 102: // 'f'
        return 15;
      case 58: // ':'
      case 59: // ';'
      case 60: // '<'
      case 61: // '='
      case 62: // '>'
      case 63: // '?'
      case 64: // '@'
      case 65: // 'A'
      case 66: // 'B'
      case 67: // 'C'
      case 68: // 'D'
      case 69: // 'E'
      case 70: // 'F'
      case 71: // 'G'
      case 72: // 'H'
      case 73: // 'I'
      case 74: // 'J'
      case 75: // 'K'
      case 76: // 'L'
      case 77: // 'M'
      case 78: // 'N'
      case 79: // 'O'
      case 80: // 'P'
      case 81: // 'Q'
      case 82: // 'R'
      case 83: // 'S'
      case 84: // 'T'
      case 85: // 'U'
      case 86: // 'V'
      case 87: // 'W'
      case 88: // 'X'
      case 89: // 'Y'
      case 90: // 'Z'
      case 91: // '['
      case 92: // '\\'
      case 93: // ']'
      case 94: // '^'
      case 95: // '_'
      case 96: // '`'
      default:
        return 0;
    }
  }
  public static String getFILENAME() {
    return FILENAME;
  }
  public int getIsEncrypt() {
    return isEncrypt;
  }
  public void setIsEncrypt(int isEncrypt) {
    this.isEncrypt = isEncrypt;
  }
  public String getKeyStr() {
    return keyStr;
  }
  public void setKeyStr(String keyStr) {
    this.keyStr = keyStr;
  }
  public String getMessage() {
    return message;
  }
  public void setMessage(String message) {
    this.message = message;
  }
  public static void main(String[] args) {
    String key = "yycg12345678901234567890";
    String oldstring = "test" + "#" + "test" + "#" + System.currentTimeMillis();
    V3DESUtil tempDesEn = new V3DESUtil(1, oldstring, key);
    String strTemp = tempDesEn.V3DESChiper("12345678");
    System.out.println("strTemp=== " + strTemp);
    V3DESUtil tempDe = new V3DESUtil(0, strTemp, key);
    String strTempDe = tempDe.V3DESChiper("12345678");
    System.out.println("strTempDe===" + strTempDe);
  }
}

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

原文鏈接:http://blog.csdn.net/lovoo/article/details/77937656

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产美女的小嫩bbb图片 | 欧美精品毛片 | 亚洲一区在线视频观看 | 免费国产在线视频 | 国产成人精品网站 | 91亚洲免费视频 | 中文字幕在线视频网站 | 久久我不卡 | 色污视频 | 亚洲情av | h色网站免费观看 | 国产精品免费观看视频 | 久久精品日本一区 | 国产午夜亚洲精品午夜鲁丝片 | av噜噜噜噜| videos高潮| 亚洲第一男人天堂 | 国产在线一区二区三区 | 成人片在线播放 | 久久久久一本一区二区青青蜜月 | 午夜看毛片 | 古装三级在线观看 | 在线亚洲欧美 | 一本色道久久综合亚洲精品图片 | 欧美一级高清免费 | 斗破苍穹在线观看免费完整观看 | 中国一级免费视频 | 91精品国产网站 | 亚洲性视频 | 深夜毛片免费看 | 精品午夜影院 | 精品欧美一区二区精品久久小说 | 欧美日韩手机在线观看 | 性欧美xxxx极品摘花 | 黄色影院在线看 | 久久成年网站 | 精品久久久一二三区播放播放播放视频 | 91美女视频在线观看 | 免费一级高清毛片 | 国产成人高潮免费观看精品 | 国产毛片毛片 |