锋盈数科-知识库 Logo
首页
软件开发
计算机基础
Hello Halo
新手必读
关于本知识库
登录 →
锋盈数科-知识库 Logo
首页 软件开发 计算机基础 Hello Halo 新手必读 关于本知识库
登录
  1. 首页
  2. 软件开发
  3. des工具类

des工具类

0
  • 软件开发
  • 发布于 2024-09-20
  • 0 次阅读
黄健
黄健

package com.pingan.zt.pamm.utils;

/**
*
*/

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.log4j.Logger;

import java.io.UnsupportedEncodingException;
@SuppressWarnings({“rawtypes”,“unused”,“unchecked”})
public class DESTools {

private static DESTools tool;
private static Logger logger = Logger.getLogger(DESTools.class);
private String _key = “2C436EE81A3F08FD4611CE27”;
private static final String Algorithm = “DESede”;
private static final byte[] keyBytes = { 0x21, 0x22, 0x4F, 0x38, (byte) 0x88, 0x10, 0x40, 0x38, 0x28, 0x28, 0x69, 0x51, (byte) 0xCB,
(byte) 0xBD, 0x55, 0x56, 0x78, 0x39, 0x63, (byte) 0x98, 0x30, 0x46, 0x36, (byte) 0xE2 };

public static DESTools getInstance() {

if (tool == null) {

tool = new DESTools();
}
return tool;
}

/**
* DES加密
*
* @param data
* 数据
* @param key
* 密钥
* @return 密文
*/
public String enocde(String data) {

return desEncode(data, _key, null, null);
}

/**
* DES加密
*
* @param data
* 数据
* @param firstKey
* 第一密钥
* @param secondKey
* 第二密钥
* @param thirdKey
* 第三密钥
* @return 加密密文
*/
public String desEncode(String data, String firstKey, String secondKey, String thirdKey) {

int leng = data.length();
String encData = ““;
List firstKeyBt = null, secondKeyBt = null, thirdKeyBt = null;
int firstLength = 0, secondLength = 0, thirdLength = 0;
if (isNotEmpty(firstKey)) {

firstKeyBt = getKeyBytes(firstKey);
firstLength = firstKeyBt.size();
}
if (isNotEmpty(secondKey)) {

secondKeyBt = getKeyBytes(secondKey);
secondLength = secondKeyBt.size();
}
if (isNotEmpty(thirdKey)) {

thirdKeyBt = getKeyBytes(thirdKey);
thirdLength = thirdKeyBt.size();
}
if (leng > 0) {

if (leng < 4) {

int[] bt = strToBt(data);
int[] encByte = null;
if (firstKey != null \&\& !““.equals(firstKey) \&\& secondKey != null \&\& !““.equals(secondKey) \&\& thirdKey != null
\&\& !““.equals(thirdKey)) {

int[] tempBt;
int x, y, z;
tempBt = bt;
for (x = 0; x < firstLength; x++) {

tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
}
for (y = 0; y < secondLength; y++) {

tempBt = enc(tempBt, (int[]) secondKeyBt.get(y));
}
for (z = 0; z < thirdLength; z++) {

tempBt = enc(tempBt, (int[]) thirdKeyBt.get(z));
}
encByte = tempBt;
} else {

if (firstKey != null \&\& firstKey != “” \&\& secondKey != null \&\& secondKey != ““) {

int[] tempBt;
int x, y;
tempBt = bt;
for (x = 0; x < firstLength; x++) {

tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
}
for (y = 0; y < secondLength; y++) {

tempBt = enc(tempBt, (int[]) secondKeyBt.get(y));
}
encByte = tempBt;
} else {

if (firstKey != null \&\& firstKey != ““) {

int[] tempBt;
int x = 0;
tempBt = bt;
for (x = 0; x < firstLength; x++) {

tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
}
encByte = tempBt;
}
}
}
encData = bt64ToHex(encByte);
} else {

int iterator = (leng / 4);
int remainder = leng % 4;
int i = 0;
for (i = 0; i < iterator; i++) {

String tempData = data.substring(i * 4 + 0, i * 4 + 4);
int[] tempByte = strToBt(tempData);
int[] encByte = null;
if (firstKey != null \&\& firstKey != “” \&\& secondKey != null \&\& secondKey != “” \&\& thirdKey != null \&\& thirdKey != ““) {

int[] tempBt;
int x, y, z;
tempBt = tempByte;
for (x = 0; x < firstLength; x++) {

tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
}
for (y = 0; y < secondLength; y++) {

tempBt = enc(tempBt, (int[]) secondKeyBt.get(y));
}
for (z = 0; z < thirdLength; z++) {

tempBt = enc(tempBt, (int[]) thirdKeyBt.get(z));
}
encByte = tempBt;
} else {

if (firstKey != null \&\& firstKey != “” \&\& secondKey != null \&\& secondKey != ““) {

int[] tempBt;
int x, y;
tempBt = tempByte;
for (x = 0; x < firstLength; x++) {

tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
}
for (y = 0; y < secondLength; y++) {

tempBt = enc(tempBt, (int[]) secondKeyBt.get(y));
}
encByte = tempBt;
} else {

if (firstKey != null \&\& firstKey != ““) {

int[] tempBt;
int x;
tempBt = tempByte;
for (x = 0; x < firstLength; x++) {

tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
}
encByte = tempBt;
}
}
}
encData += bt64ToHex(encByte);
}
if (remainder > 0) {

String remainderData = data.substring(iterator * 4 + 0, leng);
int[] tempByte = strToBt(remainderData);
int[] encByte = null;
if (firstKey != null \&\& firstKey != “” \&\& secondKey != null \&\& secondKey != “” \&\& thirdKey != null \&\& thirdKey != ““) {

int[] tempBt;
int x, y, z;
tempBt = tempByte;
for (x = 0; x < firstLength; x++) {

tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
}
for (y = 0; y < secondLength; y++) {

tempBt = enc(tempBt, (int[]) secondKeyBt.get(y));
}
for (z = 0; z < thirdLength; z++) {

tempBt = enc(tempBt, (int[]) thirdKeyBt.get(z));
}
encByte = tempBt;
} else {

if (firstKey != null \&\& firstKey != “” \&\& secondKey != null \&\& secondKey != ““) {

int[] tempBt;
int x, y;
tempBt = tempByte;
for (x = 0; x < firstLength; x++) {

tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
}
for (y = 0; y < secondLength; y++) {

tempBt = enc(tempBt, (int[]) secondKeyBt.get(y));
}
encByte = tempBt;
} else {

if (firstKey != null \&\& firstKey != ““) {

int[] tempBt;
int x;
tempBt = tempByte;
for (x = 0; x < firstLength; x++) {

tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
}
encByte = tempBt;
}
}
}
encData += bt64ToHex(encByte);
}
}
}
return encData;
}

/**
* DES解密
*
* @param data
* 密文
* @param key
* 密钥
* @return 数据
*/
public String decode(String data) {

String rs = desDecode(data, _key, null, null);
if (rs == null \|\| rs.trim().length() == 0) {

logger.warn(“DES解密失败,密文=” + data);

}
return rs;
}

/**
* DES解密
*
* @param data
* 密文
* @param firstKey
* 第一密钥
* @param secondKey
* 第二密钥
* @param thirdKey
* 第三密钥
* @return 数据
*/
public String desDecode(String data, String firstKey, String secondKey, String thirdKey) {

int leng = data.length();
String decStr = ““;
List firstKeyBt = null, secondKeyBt = null, thirdKeyBt = null;
int firstLength = 0, secondLength = 0, thirdLength = 0;
if (isNotEmpty(firstKey)) {

firstKeyBt = getKeyBytes(firstKey);
firstLength = firstKeyBt.size();
}
if (secondKey != null \&\& !““.equals(secondKey)) {

secondKeyBt = getKeyBytes(secondKey);
secondLength = secondKeyBt.size();
}
if (thirdKey != null \&\& thirdKey != ““) {

thirdKeyBt = getKeyBytes(thirdKey);
thirdLength = thirdKeyBt.size();
}

int iterator = leng / 16;
int i = 0;
for (i = 0; i < iterator; i++) {

String tempData = data.substring(i * 16 + 0, i * 16 + 16);
String strByte = hexToBt64(tempData);
int[] intByte = new int[64];
int j = 0;
for (j = 0; j < 64; j++) {

intByte[j] = Integer.parseInt(strByte.substring(j, j + 1));
}
int[] decByte = null;
if (firstKey != null \&\& firstKey != “” \&\& secondKey != null \&\& secondKey != “” \&\& thirdKey != null \&\& thirdKey != ““) {

int[] tempBt;
int x, y, z;
tempBt = intByte;
for (x = thirdLength - 1; x >= 0; x–) {

tempBt = dec(tempBt, (int[]) thirdKeyBt.get(x));
}
for (y = secondLength - 1; y >= 0; y–) {

tempBt = dec(tempBt, (int[]) secondKeyBt.get(y));
}
for (z = firstLength - 1; z >= 0; z–) {

tempBt = dec(tempBt, (int[]) firstKeyBt.get(z));
}
decByte = tempBt;
} else {

if (firstKey != null \&\& firstKey != “” \&\& secondKey != null \&\& secondKey != ““) {

int[] tempBt;
int x, y, z;
tempBt = intByte;
for (x = secondLength - 1; x >= 0; x–) {

tempBt = dec(tempBt, (int[]) secondKeyBt.get(x));
}
for (y = firstLength - 1; y >= 0; y–) {

tempBt = dec(tempBt, (int[]) firstKeyBt.get(y));
}
decByte = tempBt;
} else {

if (firstKey != null \&\& firstKey != ““) {

int[] tempBt;
int x, y, z;
tempBt = intByte;
for (x = firstLength - 1; x >= 0; x–) {

tempBt = dec(tempBt, (int[]) firstKeyBt.get(x));
}
decByte = tempBt;
}
}
}
decStr += byteToString(decByte);
}
return decStr;
}

/**
* 将密钥字符串转换为二进制数组,数组长度为64的倍数
*
* @param key
* @return
*/
public List getKeyBytes(String key) {

List keyBytes = new ArrayList();
int leng = key.length();
int iterator = (leng / 4);
int remainder = leng % 4;
int i = 0;
for (i = 0; i < iterator; i++) {

keyBytes.add(i, strToBt(key.substring(i * 4 + 0, i * 4 + 4)));
}
if (remainder > 0) {

// keyBytes[i] = strToBt(key.substring(i*4+0,leng));
keyBytes.add(i, strToBt(key.substring(i * 4 + 0, leng)));
}
return keyBytes;
}

/**
* 字符串转换为二进制数组,长度为64
*
* @param str
* @return
*/
public int[] strToBt(String str) {

int leng = str.length();
int[] bt = new int[64];
if (leng < 4) {

int i = 0, j = 0, p = 0, q = 0;
for (i = 0; i < leng; i++) {

int k = str.charAt(i);
for (j = 0; j < 16; j++) {

int pow = 1, m = 0;
for (m = 15; m > j; m–) {

pow *= 2;
}
bt[16 * i + j] = (k / pow) % 2;
}
}
for (p = leng; p < 4; p++) {

int k = 0;
for (q = 0; q < 16; q++) {

int pow = 1, m = 0;
for (m = 15; m > q; m–) {

pow *= 2;
}
bt[16 * p + q] = (k / pow) % 2;
}
}
} else {

for (int i = 0; i < 4; i++) {

int k = str.charAt(i);
for (int j = 0; j < 16; j++) {

int pow = 1;
for (int m = 15; m > j; m–) {

pow *= 2;
}
bt[16 * i + j] = (k / pow) % 2;
}
}
}
return bt;
}

/**
* 4位二进制转换为十六进制
*
* @param binary
* @return
*/
public String bt4ToHex(String binary) {

String hex = ““;
if (binary.equalsIgnoreCase(“0000”)) {

hex = “0”;
} else if (binary.equalsIgnoreCase(“0001”)) {

hex = “1”;
} else if (binary.equalsIgnoreCase(“0010”)) {

hex = “2”;
} else if (binary.equalsIgnoreCase(“0011”)) {

hex = “3”;
} else if (binary.equalsIgnoreCase(“0100”)) {

hex = “4”;
} else if (binary.equalsIgnoreCase(“0101”)) {

hex = “5”;
} else if (binary.equalsIgnoreCase(“0110”)) {

hex = “6”;
} else if (binary.equalsIgnoreCase(“0111”)) {

hex = “7”;
} else if (binary.equalsIgnoreCase(“1000”)) {

hex = “8”;
} else if (binary.equalsIgnoreCase(“1001”)) {

hex = “9”;
} else if (binary.equalsIgnoreCase(“1010”)) {

hex = “A”;
} else if (binary.equalsIgnoreCase(“1011”)) {

hex = “B”;
} else if (binary.equalsIgnoreCase(“1100”)) {

hex = “C”;
} else if (binary.equalsIgnoreCase(“1101”)) {

hex = “D”;
} else if (binary.equalsIgnoreCase(“1110”)) {

hex = “E”;
} else if (binary.equalsIgnoreCase(“1111”)) {

hex = “F”;
}

return hex;
}

/**
* 十六进制转换4位二进制
*
* @param hex
* @return
*/
public String hexToBt4(String hex) {

String binary = ““;
if (hex.equalsIgnoreCase(“0”)) {

binary = “0000”;
} else if (hex.equalsIgnoreCase(“1”)) {

binary = “0001”;
}
if (hex.equalsIgnoreCase(“2”)) {

binary = “0010”;
}
if (hex.equalsIgnoreCase(“3”)) {

binary = “0011”;
}
if (hex.equalsIgnoreCase(“4”)) {

binary = “0100”;
}
if (hex.equalsIgnoreCase(“5”)) {

binary = “0101”;
}
if (hex.equalsIgnoreCase(“6”)) {

binary = “0110”;
}
if (hex.equalsIgnoreCase(“7”)) {

binary = “0111”;
}
if (hex.equalsIgnoreCase(“8”)) {

binary = “1000”;
}
if (hex.equalsIgnoreCase(“9”)) {

binary = “1001”;
}
if (hex.equalsIgnoreCase(“A”)) {

binary = “1010”;
}
if (hex.equalsIgnoreCase(“B”)) {

binary = “1011”;
}
if (hex.equalsIgnoreCase(“C”)) {

binary = “1100”;
}
if (hex.equalsIgnoreCase(“D”)) {

binary = “1101”;
}
if (hex.equalsIgnoreCase(“E”)) {

binary = “1110”;
}
if (hex.equalsIgnoreCase(“F”)) {

binary = “1111”;
}
return binary;
}

/**
* 二进制数组转换为字符串
*
* @param byteData
* @return
*/
public String byteToString(int[] byteData) {

String str = ““;
for (int i = 0; i < 4; i++) {

int count = 0;
for (int j = 0; j < 16; j++) {

int pow = 1;
for (int m = 15; m > j; m–) {

pow *= 2;
}
count += byteData[16 * i + j] * pow;
}
if (count != 0) {

str += “” + (char) (count);
}
}
return str;
}

/**
* 64位二进制转换为十六进制
*
* @param byteData
* @return
*/
public String bt64ToHex(int[] byteData) {

String hex = ““;
for (int i = 0; i < 16; i++) {

String bt = ““;
for (int j = 0; j < 4; j++) {

bt += byteData[i * 4 + j];
}
hex += bt4ToHex(bt);
}
return hex;
}

/**
* 十六进制转换64位二进制
*
* @param hex
* @return
*/
public String hexToBt64(String hex) {

String binary = ““;
for (int i = 0; i < 16; i++) {

binary += hexToBt4(hex.substring(i, i + 1));
}
return binary;
}

/**
* DES加密算法
*
* @param dataByte
* @param keyByte
* @return
*/
public int[] enc(int[] dataByte, int[] keyByte) {

int[][] keys = generateKeys(keyByte);
int[] ipByte = initPermute(dataByte);
int[] ipLeft = new int[32];
int[] ipRight = new int[32];
int[] tempLeft = new int[32];
int i = 0, j = 0, k = 0, m = 0, n = 0;
for (k = 0; k < 32; k++) {

ipLeft[k] = ipByte[k];
ipRight[k] = ipByte[32 + k];
}
for (i = 0; i < 16; i++) {

for (j = 0; j < 32; j++) {

tempLeft[j] = ipLeft[j];
ipLeft[j] = ipRight[j];
}
int[] key = new int[48];
for (m = 0; m < 48; m++) {

key[m] = keys[i][m];
}
int[] tempRight = xor(pPermute(sBoxPermute(xor(expandPermute(ipRight), key))), tempLeft);
for (n = 0; n < 32; n++) {

ipRight[n] = tempRight[n];
}

}

int[] finalData = new int[64];
for (i = 0; i < 32; i++) {

finalData[i] = ipRight[i];
finalData[32 + i] = ipLeft[i];
}
return finallyPermute(finalData);
}

/**
* DES解密算法
*
* @param dataByte
* @param keyByte
* @return
*/
public int[] dec(int[] dataByte, int[] keyByte) {

int[][] keys = generateKeys(keyByte);
int[] ipByte = initPermute(dataByte);
int[] ipLeft = new int[32];
int[] ipRight = new int[32];
int[] tempLeft = new int[32];
int i = 0, j = 0, k = 0, m = 0, n = 0;
for (k = 0; k < 32; k++) {

ipLeft[k] = ipByte[k];
ipRight[k] = ipByte[32 + k];
}
for (i = 15; i >= 0; i–) {

for (j = 0; j < 32; j++) {

tempLeft[j] = ipLeft[j];
ipLeft[j] = ipRight[j];
}
int[] key = new int[48];
for (m = 0; m < 48; m++) {

key[m] = keys[i][m];
}

int[] tempRight = xor(pPermute(sBoxPermute(xor(expandPermute(ipRight), key))), tempLeft);
for (n = 0; n < 32; n++) {

ipRight[n] = tempRight[n];
}
}

int[] finalData = new int[64];
for (i = 0; i < 32; i++) {

finalData[i] = ipRight[i];
finalData[32 + i] = ipLeft[i];
}
return finallyPermute(finalData);
}

/**
* 初始化序列
*
* @param originalData
* @return
*/
public int[] initPermute(int[] originalData) {

int[] ipByte = new int[64];
int i = 0, m = 1, n = 0, j, k;
for (i = 0, m = 1, n = 0; i < 4; i++, m += 2, n += 2) {

for (j = 7, k = 0; j >= 0; j–, k++) {

ipByte[i * 8 + k] = originalData[j * 8 + m];
ipByte[i * 8 + k + 32] = originalData[j * 8 + n];
}
}
return ipByte;
}

/**
* 扩展序列
*
* @param rightData
* @return
*/
public int[] expandPermute(int[] rightData) {

int[] epByte = new int[48];
int i, j;
for (i = 0; i < 8; i++) {

if (i == 0) {

epByte[i * 6 + 0] = rightData[31];
} else {

epByte[i * 6 + 0] = rightData[i * 4 - 1];
}
epByte[i * 6 + 1] = rightData[i * 4 + 0];
epByte[i * 6 + 2] = rightData[i * 4 + 1];
epByte[i * 6 + 3] = rightData[i * 4 + 2];
epByte[i * 6 + 4] = rightData[i * 4 + 3];
if (i == 7) {

epByte[i * 6 + 5] = rightData[0];
} else {

epByte[i * 6 + 5] = rightData[i * 4 + 4];
}
}
return epByte;
}

/**
* 异或计算
*
* @param byteOne
* @param byteTwo
* @return
*/
public int[] xor(int[] byteOne, int[] byteTwo) {

int[] xorByte = new int[byteOne.length];
for (int i = 0; i < byteOne.length; i++) {

xorByte[i] = byteOne[i] ^ byteTwo[i];
}
return xorByte;
}

/**
*
* @param expandByte
* @return
*/
public int[] sBoxPermute(int[] expandByte) {

// var sBoxByte = new Array(32);
int[] sBoxByte = new int[32];
String binary = ““;
int[][] s1 = { { 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7 }, { 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8 },
{ 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0 }, { 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 } };

/* Table - s2 */
int[][] s2 = { { 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10 }, { 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5 },
{ 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15 }, { 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 } };

/* Table - s3 */
int[][] s3 = { { 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8 }, { 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1 },
{ 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7 }, { 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 } };
/* Table - s4 */
int[][] s4 = { { 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15 }, { 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9 },
{ 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4 }, { 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 } };

/* Table - s5 */
int[][] s5 = { { 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9 }, { 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6 },
{ 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14 }, { 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 } };

/* Table - s6 */
int[][] s6 = { { 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11 }, { 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8 },
{ 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6 }, { 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 } };

/* Table - s7 */
int[][] s7 = { { 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1 }, { 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6 },
{ 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2 }, { 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 } };

/* Table - s8 */
int[][] s8 = { { 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7 }, { 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2 },
{ 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8 }, { 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 } };

for (int m = 0; m < 8; m++) {

int i = 0, j = 0;
i = expandByte[m * 6 + 0] * 2 + expandByte[m * 6 + 5];
j = expandByte[m * 6 + 1] * 2 * 2 * 2 + expandByte[m * 6 + 2] * 2 * 2 + expandByte[m * 6 + 3] * 2 + expandByte[m * 6 + 4];
switch (m) {

case 0:
binary = getBoxBinary(s1[i][j]);
break;
case 1:
binary = getBoxBinary(s2[i][j]);
break;
case 2:
binary = getBoxBinary(s3[i][j]);
break;
case 3:
binary = getBoxBinary(s4[i][j]);
break;
case 4:
binary = getBoxBinary(s5[i][j]);
break;
case 5:
binary = getBoxBinary(s6[i][j]);
break;
case 6:
binary = getBoxBinary(s7[i][j]);
break;
case 7:
binary = getBoxBinary(s8[i][j]);
break;
}
sBoxByte[m * 4 + 0] = Integer.parseInt(binary.substring(0, 1));
sBoxByte[m * 4 + 1] = Integer.parseInt(binary.substring(1, 2));
sBoxByte[m * 4 + 2] = Integer.parseInt(binary.substring(2, 3));
sBoxByte[m * 4 + 3] = Integer.parseInt(binary.substring(3, 4));
}
return sBoxByte;
}

/**
*
* @param sBoxByte
* @return
*/
public int[] pPermute(int[] sBoxByte) {

int[] pBoxPermute = new int[32];
pBoxPermute[0] = sBoxByte[15];
pBoxPermute[1] = sBoxByte[6];
pBoxPermute[2] = sBoxByte[19];
pBoxPermute[3] = sBoxByte[20];
pBoxPermute[4] = sBoxByte[28];
pBoxPermute[5] = sBoxByte[11];
pBoxPermute[6] = sBoxByte[27];
pBoxPermute[7] = sBoxByte[16];
pBoxPermute[8] = sBoxByte[0];
pBoxPermute[9] = sBoxByte[14];
pBoxPermute[10] = sBoxByte[22];
pBoxPermute[11] = sBoxByte[25];
pBoxPermute[12] = sBoxByte[4];
pBoxPermute[13] = sBoxByte[17];
pBoxPermute[14] = sBoxByte[30];
pBoxPermute[15] = sBoxByte[9];
pBoxPermute[16] = sBoxByte[1];
pBoxPermute[17] = sBoxByte[7];
pBoxPermute[18] = sBoxByte[23];
pBoxPermute[19] = sBoxByte[13];
pBoxPermute[20] = sBoxByte[31];
pBoxPermute[21] = sBoxByte[26];
pBoxPermute[22] = sBoxByte[2];
pBoxPermute[23] = sBoxByte[8];
pBoxPermute[24] = sBoxByte[18];
pBoxPermute[25] = sBoxByte[12];
pBoxPermute[26] = sBoxByte[29];
pBoxPermute[27] = sBoxByte[5];
pBoxPermute[28] = sBoxByte[21];
pBoxPermute[29] = sBoxByte[10];
pBoxPermute[30] = sBoxByte[3];
pBoxPermute[31] = sBoxByte[24];
return pBoxPermute;
}

/**
*
* @param endByte
* @return
*/
public int[] finallyPermute(int[] endByte) {

int[] fpByte = new int[64];
fpByte[0] = endByte[39];
fpByte[1] = endByte[7];
fpByte[2] = endByte[47];
fpByte[3] = endByte[15];
fpByte[4] = endByte[55];
fpByte[5] = endByte[23];
fpByte[6] = endByte[63];
fpByte[7] = endByte[31];
fpByte[8] = endByte[38];
fpByte[9] = endByte[6];
fpByte[10] = endByte[46];
fpByte[11] = endByte[14];
fpByte[12] = endByte[54];
fpByte[13] = endByte[22];
fpByte[14] = endByte[62];
fpByte[15] = endByte[30];
fpByte[16] = endByte[37];
fpByte[17] = endByte[5];
fpByte[18] = endByte[45];
fpByte[19] = endByte[13];
fpByte[20] = endByte[53];
fpByte[21] = endByte[21];
fpByte[22] = endByte[61];
fpByte[23] = endByte[29];
fpByte[24] = endByte[36];
fpByte[25] = endByte[4];
fpByte[26] = endByte[44];
fpByte[27] = endByte[12];
fpByte[28] = endByte[52];
fpByte[29] = endByte[20];
fpByte[30] = endByte[60];
fpByte[31] = endByte[28];
fpByte[32] = endByte[35];
fpByte[33] = endByte[3];
fpByte[34] = endByte[43];
fpByte[35] = endByte[11];
fpByte[36] = endByte[51];
fpByte[37] = endByte[19];
fpByte[38] = endByte[59];
fpByte[39] = endByte[27];
fpByte[40] = endByte[34];
fpByte[41] = endByte[2];
fpByte[42] = endByte[42];
fpByte[43] = endByte[10];
fpByte[44] = endByte[50];
fpByte[45] = endByte[18];
fpByte[46] = endByte[58];
fpByte[47] = endByte[26];
fpByte[48] = endByte[33];
fpByte[49] = endByte[1];
fpByte[50] = endByte[41];
fpByte[51] = endByte[9];
fpByte[52] = endByte[49];
fpByte[53] = endByte[17];
fpByte[54] = endByte[57];
fpByte[55] = endByte[25];
fpByte[56] = endByte[32];
fpByte[57] = endByte[0];
fpByte[58] = endByte[40];
fpByte[59] = endByte[8];
fpByte[60] = endByte[48];
fpByte[61] = endByte[16];
fpByte[62] = endByte[56];
fpByte[63] = endByte[24];
return fpByte;
}

/**
*
* @param i
* @return
*/
public String getBoxBinary(int i) {

String binary = ““;
switch (i) {

case 0:
binary = “0000”;
break;
case 1:
binary = “0001”;
break;
case 2:
binary = “0010”;
break;
case 3:
binary = “0011”;
break;
case 4:
binary = “0100”;
break;
case 5:
binary = “0101”;
break;
case 6:
binary = “0110”;
break;
case 7:
binary = “0111”;
break;
case 8:
binary = “1000”;
break;
case 9:
binary = “1001”;
break;
case 10:
binary = “1010”;
break;
case 11:
binary = “1011”;
break;
case 12:
binary = “1100”;
break;
case 13:
binary = “1101”;
break;
case 14:
binary = “1110”;
break;
case 15:
binary = “1111”;
break;
}
return binary;
}

/**
*
* @param keyByte
* @return
*/
public int[][] generateKeys(int[] keyByte) {

int[] key = new int[56];
int[][] keys = new int[16][48];
int[] loop = new int[] { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };

for (int i = 0; i < 7; i++) {

for (int j = 0, k = 7; j < 8; j++, k–) {

key[i * 8 + j] = keyByte[8 * k + i];
}
}

int i = 0;
for (i = 0; i < 16; i++) {

int tempLeft = 0;
int tempRight = 0;
for (int j = 0; j < loop[i]; j++) {

tempLeft = key[0];
tempRight = key[28];
for (int k = 0; k < 27; k++) {

key[k] = key[k + 1];
key[28 + k] = key[29 + k];
}
key[27] = tempLeft;
key[55] = tempRight;
}
int[] tempKey = new int[48];
tempKey[0] = key[13];
tempKey[1] = key[16];
tempKey[2] = key[10];
tempKey[3] = key[23];
tempKey[4] = key[0];
tempKey[5] = key[4];
tempKey[6] = key[2];
tempKey[7] = key[27];
tempKey[8] = key[14];
tempKey[9] = key[5];
tempKey[10] = key[20];
tempKey[11] = key[9];
tempKey[12] = key[22];
tempKey[13] = key[18];
tempKey[14] = key[11];
tempKey[15] = key[3];
tempKey[16] = key[25];
tempKey[17] = key[7];
tempKey[18] = key[15];
tempKey[19] = key[6];
tempKey[20] = key[26];
tempKey[21] = key[19];
tempKey[22] = key[12];
tempKey[23] = key[1];
tempKey[24] = key[40];
tempKey[25] = key[51];
tempKey[26] = key[30];
tempKey[27] = key[36];
tempKey[28] = key[46];
tempKey[29] = key[54];
tempKey[30] = key[29];
tempKey[31] = key[39];
tempKey[32] = key[50];
tempKey[33] = key[44];
tempKey[34] = key[32];
tempKey[35] = key[47];
tempKey[36] = key[43];
tempKey[37] = key[48];
tempKey[38] = key[38];
tempKey[39] = key[55];
tempKey[40] = key[33];
tempKey[41] = key[52];
tempKey[42] = key[45];
tempKey[43] = key[41];
tempKey[44] = key[49];
tempKey[45] = key[35];
tempKey[46] = key[28];
tempKey[47] = key[31];
int m;
switch (i) {

case 0:
for (m = 0; m < 48; m++) {

keys[0][m] = tempKey[m];
}
break;
case 1:
for (m = 0; m < 48; m++) {

keys[1][m] = tempKey[m];
}
break;
case 2:
for (m = 0; m < 48; m++) {

keys[2][m] = tempKey[m];
}
break;
case 3:
for (m = 0; m < 48; m++) {

keys[3][m] = tempKey[m];
}
break;
case 4:
for (m = 0; m < 48; m++) {

keys[4][m] = tempKey[m];
}
break;
case 5:
for (m = 0; m < 48; m++) {

keys[5][m] = tempKey[m];
}
break;
case 6:
for (m = 0; m < 48; m++) {

keys[6][m] = tempKey[m];
}
break;
case 7:
for (m = 0; m < 48; m++) {

keys[7][m] = tempKey[m];
}
break;
case 8:
for (m = 0; m < 48; m++) {

keys[8][m] = tempKey[m];
}
break;
case 9:
for (m = 0; m < 48; m++) {

keys[9][m] = tempKey[m];
}
break;
case 10:
for (m = 0; m < 48; m++) {

keys[10][m] = tempKey[m];
}
break;
case 11:
for (m = 0; m < 48; m++) {

keys[11][m] = tempKey[m];
}
break;
case 12:
for (m = 0; m < 48; m++) {

keys[12][m] = tempKey[m];
}
break;
case 13:
for (m = 0; m < 48; m++) {

keys[13][m] = tempKey[m];
}
break;
case 14:
for (m = 0; m < 48; m++) {

keys[14][m] = tempKey[m];
}
break;
case 15:
for (m = 0; m < 48; m++) {

keys[15][m] = tempKey[m];
}
break;
}
}
return keys;
}

public static byte[] encryptMode(byte[] keybyte, byte[] src) {

try {

SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE, deskey);
return c1.doFinal(src);
} catch (java.lang.Exception e3) {

e3.printStackTrace();
}
return null;
}

/**
* 用MAP中的KEY值,替换str中的标识符
*/
public static String replaceStrWithMap(Map params, String str) {

if (null == params \|\| null == str) {

return str;
}
try {

Iterator it = (Iterator) params.keySet().iterator();
String key = null;
while (it.hasNext()) {

key = (String) it.next();
Object o = params.get(key);
if (null != o \&\& o instanceof String \&\& !““.equals(o)) {

// 先组装KEY,然后再组装VALUE
// str = str.replace(“${” + key + “}“,
// (String)params.get(key));
str = str.replaceAll(“${” + key + “}“, (String) params.get(key));
} else {

str = str.replaceAll(“\&” + key + “=${” + key + “}“, ““);
}

}
String paramStr = null;
if (str.indexOf(“des{“) > 0) {

// 加密前的串
logger.info(“加密前的串:” + str);
// 获取des的位置
paramStr = str.substring(str.indexOf(“des{“) + 4, str.indexOf(“}“));
byte[] afertStr = encryptMode(keyBytes, paramStr.getBytes());
byte[] sfterBsae64 = encode(afertStr, true);
String message = java.net.URLEncoder.encode(new String(sfterBsae64), “UTF-8”);
str = str.replaceAll(“des{” + paramStr + “}“, “a” + message + “a”);
}
} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block
e.printStackTrace();
return str;
}
return str;
}

public static long getDaySub(String beginDateStr, String endDateStr) {

long day = 0;
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(“yyyy-MM-dd”);
java.util.Date beginDate;
java.util.Date endDate;
try {

beginDate = format.parse(beginDateStr);
endDate = format.parse(endDateStr);
day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
} catch (ParseException e) {

// TODO 自动生成 catch 块
e.printStackTrace();
}
return day;

}

public boolean isNotEmpty(String str) {

if (str != null \&\& !““.equals(str)) {

return true;
} else {

return false;
}
}

public static void main(String[] args) {

/*
* Map map = new HashMap(); map.put(“test”, “0000”);
* System.out.println(replaceStrWithMap
* (map,“http://ddd?value=des{ffffffffff={test}}“));
*/
String str = “[\“DDC68B0BA0393E81E043ED12190A4FD0\“,\“DDC68B0BA0393E81E043ED12190A4FD0\“]“;

/*
* List taskIds = JsonUtils.stringToList(String.class, str);
* for(int i=0;i

* System.out.println(taskIds.get(i)); } StringBuffer sb = new
* StringBuffer(); String tskId=““; for(int i=0;i

* sb.append(“'“).append(taskIds.get(i)).append(“'“).append(“,“); }
* tskId = sb.toString(); if (tskId.endsWith(“,“)) { tskId =
* tskId.substring(0, tskId.length() - 1); } System.out.println(tskId);
*/
/*
* JSONArray jsonArray = JSONArray.fromObject(str);
* System.out.println(jsonArray); String v =
* jsonArray.toString().substring(1,jsonArray.toString().length()-1);
* System.out.println(v); JSONObject ycJson = JSONObject.fromObject(v);
* System.out.println(ycJson);
*/
String clientName = “曾国辉”;
String clientNo = “2222”;
String clientLogonName = “22222”;
String sex = “男”;
String partyNo = “1111”;
String clientLevel = “111”;
String st = “clientName=” + clientName + “\&clientNo=” + clientNo + “\&clientLogonName=” + clientLogonName + “\&sex=” + sex

  • “\&partyNo=” + partyNo + “\&clientLevel=” + clientLevel;
    DESTools js = new DESTools();
    String b = js.enocde(st);
    System.out.println(b);
    String data = “9F50EF23C4D49F2FE8ACDC406E8793993F1CCCA9A70492C65F7DA7ECDE198CE160E276BF5AB8544E1990938D75FDD4669E6444CBCAC32FC034B931E2AD1E306C0CB81AA589FDA1980DA36FC50F3C15C4B949FA0467B33519CBB2A1FC5B70DF333F1CCCA9A70492C69EE69D1E3ABD330B4564D4ABED5C205F870AA7B241D2B64BE09DAD16DE8EBE7D6AD4A75D295328C640544F059103B2070DA36FC50F3C15C4E00E95C18DA31C2AF894AEBE58355456517B77BD4695CAEA”;
    System.out.println(js.decode(data));

}

static final int[] enc = { 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, 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, 48,
49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47 };

public static final byte[] encode(byte[] data, boolean useNL) {

if (data == null)
return null;

int lpos = 0;

int spos = 0;
int slen = data.length;

int dpos = 0;
int dstate = 0;
int dlen = (slen + 2) / 3 * 4;
if (useNL)
dlen += dlen / 76 * 2;
byte[] dest = new byte[dlen];

while (spos < slen) {

switch (dstate) {

case 0:
dest[(dpos++)] = (byte) enc[(data[spos] >> 2 \& 0x3F)];
break;
case 1:
if (spos + 1 >= slen) {

dest[(dpos++)] = (byte) enc[(data[spos] << 4 \& 0x30)];
dest[(dpos++)] = 61;
dest[(dpos++)] = 61;
++lpos;
} else {

dest[(dpos++)] = (byte) enc[(data[spos] << 4 \& 0x30 \| data[(spos + 1)] >> 4 \& 0xF)];
}

++spos;
break;
case 2:
if (spos + 1 >= slen) {

dest[(dpos++)] = (byte) enc[(data[spos] << 2 \& 0x3C)];
dest[(dpos++)] = 61;
} else {

dest[(dpos++)] = (byte) enc[(data[spos] << 2 \& 0x3C \| data[(spos + 1)] >> 6 \& 0x3)];
}

++spos;
break;
case 3:
dest[(dpos++)] = (byte) enc[(data[spos] \& 0x3F)];
++spos;
}

if (++dstate == 4)
dstate = 0;

++lpos;
if ((lpos != 76) \|\| (!(useNL)))
continue;
lpos = 0;
dest[(dpos++)] = 13;
dest[(dpos++)] = 10;
}

if ((++lpos == 76) \&\& (useNL)) {

dest[(dpos++)] = 13;
dest[(dpos++)] = 10;
}

return dest;
}

}

原文链接: https://blog.csdn.net/javajingling/article/details/50075437

标签: #软件开发 1171
相关文章

万字:支付“核心系统”详解 2024-11-02 15:33

专栏作者:隐墨星辰 \| 主编:陈天宇宙 这篇文章也尝试化繁为简,探寻支付系统的本质,讲清楚在线支付系统最核心的一些概念和设计理念。 虽然支付行业已经过了风头最劲的时光,但跨境支付仍然在蓬勃发展,每年依然有很多新人进入这个行业,这篇文章尝试为这些刚入行的新人提供一点帮助。 文章只介绍一些支付行业十几

资深支付架构师视角:实战从问题定义到代码落地的完整套路 2024-11-02 15:33

前言 今天从一个实际案例入手,介绍站在架构师的角度,如何识别并定义问题,提炼需求,技术方案选型,再到详细设计,最后利用AI的能力协助写出核心的代码,验证与调优。 解决问题存在一定的模式,也可以称之为框架,总结出自己的思考和解题框架,以后再碰到同类型的问题就可以如庖丁解牛一样容易。 很多年前,我写代码

Spring 实现 3 种异步接口 2024-10-18 09:07

大家好,我是苏三~ 如何处理比较耗时的接口? 这题我熟,直接上异步接口,使用 Callable、WebAsyncTask 和 DeferredResult、CompletableFuture等均可实现。 但这些方法有局限性,处理结果仅返回单个值。在某些场景下,如果需要接口异步处理的同时,还持续不断地

重学SpringBoot3-集成Redis(五)之布隆过滤器 2024-10-08 11:24

更多SpringBoot3内容请关注我的专栏:《SpringBoot3》 期待您的点赞👍收藏⭐评论✍ 重学SpringBoot3-集成Redis(五)之布隆过滤器 1. 什么是布隆过滤器? * 基本概念 适用场景 2. 使用 Redis 实现布隆过滤器 * 项目依赖 Redis 配置

设计模式第16讲——迭代器模式(Iterator) 2024-10-08 11:24

一、什么是迭代器模式 迭代器模式是一种行为型设计模式,它提供了一种统一的方式来访问集合对象中的元素,而不是暴露集合内部的表示方式。简单地说,就是将遍历集合的责任封装到一个单独的对象中,我们可以按照特定的方式访问集合中的元素。 二、角色组成 抽象迭代器(Iterator):定义了遍历聚合对象所需的方法

vue2路由和vue3路由区别及原理 2024-10-08 11:24

一、Vue2 与 Vue3 路由的区别 1. 创建路由实例方式的不同 Vue 2 中,通过 Vue.use() 注册路由插件,并通过 new VueRouter() 来创建路由实例。 import Vue from 'vue';import VueRouter from 'vue-router';i

目录

IT 外包服务商

  • 意见投递
  • zyf6619

软件开发应用

主菜单

  • 首页
  • 软件开发
  • 计算机基础
  • Hello Halo
  • 新手必读
  • 关于本知识库
Copyright © 2024 your company All Rights Reserved. Powered by Halo.