Java實(shí)現(xiàn)的3des加密解密工具類示例
本文實(shí)例講述了Java實(shí)現(xiàn)的3des加密解密工具類。分享給大家供大家參考,具體如下:
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ù)加密算法工具類
* 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);
}
}
PS:關(guān)于加密解密感興趣的朋友還可以參考本站在線工具:
文字在線加密解密工具(包含AES、DES、RC4等):
http://tools.jb51.net/password/txt_encode
MD5在線加密工具:
http://tools.jb51.net/password/CreateMD5Password
在線散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt
在線MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha
在線sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java字符與字符串操作技巧總結(jié)》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
SpringBoot實(shí)現(xiàn)配置文件的替換
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)配置文件的替換,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Maven打包跳過(guò)測(cè)試的實(shí)現(xiàn)方法
使用Maven打包的時(shí)候,可能會(huì)因?yàn)閱卧獪y(cè)試打包失敗,這時(shí)候就需要跳過(guò)單元測(cè)試。本文就介紹了Maven打包跳過(guò)測(cè)試的實(shí)現(xiàn)方法,感興趣的可以了解一下2021-06-06
Spring的IOC容器實(shí)例化bean的方式總結(jié)
IOC容器實(shí)例化bean的三種方式:構(gòu)造方法、靜態(tài)工廠、實(shí)例工廠,本文將通過(guò)代碼示例給大家詳細(xì)講解一下這三種方式,對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-01-01
Springboot日期轉(zhuǎn)換器實(shí)現(xiàn)代碼及示例
這篇文章主要介紹了Springboot日期轉(zhuǎn)換器實(shí)現(xiàn)代碼及示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
Mybatis-plus實(shí)現(xiàn)join連表查詢的示例代碼
mybatis-plus在連表查詢上是不行的,如果需要連表查詢,就得乖乖的去寫xml文件了,本文介紹了mybatis-plus-join框架,它支持連表查詢,感興趣的可以了解一下2023-08-08
Java使用kafka發(fā)送和生產(chǎn)消息的示例
本篇文章主要介紹了Java使用kafka發(fā)送和生產(chǎn)消息的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04

