|
|
@@ -0,0 +1,416 @@ |
|
|
|
|
|
|
|
package cn.com.taiji.core.manager.tools.encryption; |
|
|
|
|
|
|
|
//import com.techrich.util.BytesBuffer; |
|
|
|
//import com.techrich.util.Des3Cipher; |
|
|
|
//import com.techrich.util.DesCipher; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.io.InterruptedIOException; |
|
|
|
import java.text.SimpleDateFormat; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.Random; |
|
|
|
|
|
|
|
/** |
|
|
|
* VFJ 工具类 |
|
|
|
* |
|
|
|
* @author hou yi |
|
|
|
* @date 2023年4月26日 下午7:09:45 |
|
|
|
*/ |
|
|
|
public class Utils { |
|
|
|
public static byte[] getRandom(final int length) { |
|
|
|
final Random random = new Random(); |
|
|
|
final byte[] result = new byte[length]; |
|
|
|
for (int i = 0; i < length; ++i) { |
|
|
|
result[i] = (byte) random.nextInt(256); |
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
public static byte high(final byte chr) { |
|
|
|
byte temp = 0; |
|
|
|
for (int i = 7; i >= 4; --i) { |
|
|
|
if ((1L << i & (long) chr) != 0x0L) { |
|
|
|
temp |= (byte) (1L << i - 4); |
|
|
|
} |
|
|
|
} |
|
|
|
return temp; |
|
|
|
} |
|
|
|
|
|
|
|
public static int bytes2int(final byte[] bytes, final int offset) { |
|
|
|
final int length1 = bytes[offset + 3] << 24 & 0xFF000000; |
|
|
|
final int length2 = bytes[offset + 2] << 16 & 0xFF0000; |
|
|
|
final int length3 = bytes[offset + 1] << 8 & 0xFF00; |
|
|
|
final int length4 = bytes[offset + 0] << 0 & 0xFF; |
|
|
|
return length1 + length2 + length3 + length4; |
|
|
|
} |
|
|
|
|
|
|
|
public static short bytes2short(final byte[] bytes, final int offset) { |
|
|
|
final int length1 = bytes[offset + 1] << 8 & 0xFF00; |
|
|
|
final int length2 = bytes[offset + 0] << 0 & 0xFF; |
|
|
|
return (short) (length1 + length2); |
|
|
|
} |
|
|
|
|
|
|
|
public static byte[] int2bytes(final int value) { |
|
|
|
final byte[] bytes = { (byte) (value & 0xFF), (byte) ((value & 0xFF00) >> 8), (byte) ((value & 0xFF0000) >> 16), |
|
|
|
(byte) ((value & 0xFF000000) >> 24) }; |
|
|
|
return bytes; |
|
|
|
} |
|
|
|
|
|
|
|
public static byte[] short2bytes(final short value) { |
|
|
|
final byte[] bytes = { (byte) (value & 0xFF), (byte) ((value & 0xFF00) >> 8) }; |
|
|
|
return bytes; |
|
|
|
} |
|
|
|
|
|
|
|
public static String bytes2hex(final byte[] bytes, int len) { |
|
|
|
if (len > bytes.length || len <= 0) { |
|
|
|
len = bytes.length; |
|
|
|
} |
|
|
|
final StringBuffer sb = new StringBuffer(); |
|
|
|
for (int i = 0; i < len; ++i) { |
|
|
|
String temp = Integer.toHexString(bytes[i]); |
|
|
|
switch (temp.length()) { |
|
|
|
case 0: |
|
|
|
case 1: { |
|
|
|
temp = "0" + temp; |
|
|
|
break; |
|
|
|
} |
|
|
|
default: { |
|
|
|
temp = temp.substring(temp.length() - 2); |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
sb.append(temp); |
|
|
|
} |
|
|
|
return sb.toString().toUpperCase(); |
|
|
|
} |
|
|
|
|
|
|
|
public static String bytes2hex(final byte[] bytes) { |
|
|
|
return bytes2hex(bytes, 0); |
|
|
|
} |
|
|
|
|
|
|
|
public static int toInt(final char a) { |
|
|
|
if (a >= '0' && a <= '9') { |
|
|
|
return a - '0'; |
|
|
|
} |
|
|
|
if (a >= 'A' && a <= 'F') { |
|
|
|
return a - '7'; |
|
|
|
} |
|
|
|
if (a >= 'a' && a <= 'f') { |
|
|
|
return a - 'W'; |
|
|
|
} |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
public static byte[] hex2bytes(String hex) { |
|
|
|
if (hex.length() % 2 != 0) { |
|
|
|
hex = "0" + hex; |
|
|
|
} |
|
|
|
final int len = hex.length() / 2; |
|
|
|
final byte[] val = new byte[len]; |
|
|
|
for (int i = 0; i < len; ++i) { |
|
|
|
val[i] = (byte) (toInt(hex.charAt(2 * i)) * 16 + toInt(hex.charAt(2 * i + 1))); |
|
|
|
} |
|
|
|
return val; |
|
|
|
} |
|
|
|
|
|
|
|
public static String bcd2string(final byte[] bcddata, final int offset, final int len) { |
|
|
|
final int strlen = len * 2; |
|
|
|
final byte[] temp = new byte[strlen]; |
|
|
|
int i = 0; |
|
|
|
int j = 0; |
|
|
|
while (i < len) { |
|
|
|
temp[j++] = high(bcddata[offset + i]); |
|
|
|
temp[j++] = (byte) (bcddata[offset + i] & 0xF); |
|
|
|
++i; |
|
|
|
} |
|
|
|
for (i = 0; i < strlen; ++i) { |
|
|
|
if (temp[i] >= 10) { |
|
|
|
temp[i] = (byte) (65 + temp[i] - 10); |
|
|
|
} else { |
|
|
|
final byte[] array = temp; |
|
|
|
final int n = i; |
|
|
|
array[n] += 48; |
|
|
|
} |
|
|
|
} |
|
|
|
return new String(temp); |
|
|
|
} |
|
|
|
|
|
|
|
public static String bcd2string(final byte[] bcddata) { |
|
|
|
return bcd2string(bcddata, 0, bcddata.length); |
|
|
|
} |
|
|
|
|
|
|
|
static byte hxv(final byte chr) { |
|
|
|
if (chr > 96) { |
|
|
|
return (byte) (chr - 97 + 10); |
|
|
|
} |
|
|
|
if (chr > 64) { |
|
|
|
return (byte) (chr - 65 + 10); |
|
|
|
} |
|
|
|
if (chr == 0) { |
|
|
|
return 0; |
|
|
|
} |
|
|
|
return (byte) (chr - 48); |
|
|
|
} |
|
|
|
|
|
|
|
public static byte[] string2bcd(final String str, final int len) { |
|
|
|
byte[] temp = new byte[len * 2]; |
|
|
|
for (int i = 0; i < len * 2; ++i) { |
|
|
|
temp[i] = 48; |
|
|
|
} |
|
|
|
StringBuffer sb = new StringBuffer(new String(temp)); |
|
|
|
sb = sb.replace(len * 2 - str.length(), sb.length(), str); |
|
|
|
temp = new byte[len]; |
|
|
|
int lpi = 0; |
|
|
|
int lpj = 0; |
|
|
|
while (lpi < len) { |
|
|
|
final byte high = (byte) (hxv((byte) sb.charAt(lpj++)) << 4); |
|
|
|
final byte low = hxv((byte) sb.charAt(lpj++)); |
|
|
|
temp[lpi] = (byte) (high | low); |
|
|
|
++lpi; |
|
|
|
} |
|
|
|
return temp; |
|
|
|
} |
|
|
|
|
|
|
|
public static byte[] int2bcd(final int data, final int len) { |
|
|
|
final String buf = String.valueOf(data); |
|
|
|
return string2bcd(buf, len); |
|
|
|
} |
|
|
|
|
|
|
|
public static byte[] long2bcd(final long data, final int len) { |
|
|
|
final String buf = String.valueOf(data); |
|
|
|
return string2bcd(buf, len); |
|
|
|
} |
|
|
|
|
|
|
|
public static int ntohl(final byte[] bytes) { |
|
|
|
return ntohl(bytes, 0); |
|
|
|
} |
|
|
|
|
|
|
|
public static int ntohl(final byte[] bytes, final int offset) { |
|
|
|
final int length1 = bytes[offset + 0] << 24 & 0xFF000000; |
|
|
|
final int length2 = bytes[offset + 1] << 16 & 0xFF0000; |
|
|
|
final int length3 = bytes[offset + 2] << 8 & 0xFF00; |
|
|
|
final int length4 = bytes[offset + 3] << 0 & 0xFF; |
|
|
|
return length1 + length2 + length3 + length4; |
|
|
|
} |
|
|
|
|
|
|
|
public static byte[] htonl(final int value) { |
|
|
|
final byte[] bytes = { (byte) ((value & 0xFF000000) >> 24), (byte) ((value & 0xFF0000) >> 16), |
|
|
|
(byte) ((value & 0xFF00) >> 8), (byte) (value & 0xFF) }; |
|
|
|
return bytes; |
|
|
|
} |
|
|
|
|
|
|
|
public static short ntohs(final byte[] bytes, final int offset) { |
|
|
|
final int length = (bytes[offset + 0] << 8 & 0xFF00) + (bytes[offset + 1] << 0 & 0xFF); |
|
|
|
return (short) length; |
|
|
|
} |
|
|
|
|
|
|
|
public static byte[] htons(final short value) { |
|
|
|
final byte[] bytes = { (byte) ((value & 0xFF00) >> 8), (byte) (value & 0xFF) }; |
|
|
|
return bytes; |
|
|
|
} |
|
|
|
|
|
|
|
public static byte[] xor(final byte[] b1, final byte[] b2) { |
|
|
|
final int len = (b1.length <= b2.length) ? b1.length : b2.length; |
|
|
|
final byte[] temp = new byte[len]; |
|
|
|
for (int i = 0; i < len; ++i) { |
|
|
|
temp[i] = (byte) (b1[i] ^ b2[i]); |
|
|
|
} |
|
|
|
return temp; |
|
|
|
} |
|
|
|
|
|
|
|
public static String trunkString(int length, final String orig) { |
|
|
|
if (length <= 0) { |
|
|
|
length = 256; |
|
|
|
} |
|
|
|
final int len = orig.length(); |
|
|
|
if (len < 2 * length + 3) { |
|
|
|
return orig; |
|
|
|
} |
|
|
|
final StringBuffer result = new StringBuffer(); |
|
|
|
return result.append(orig.substring(0, length)).append("...").append(orig.substring(orig.length() - length)) |
|
|
|
.toString(); |
|
|
|
} |
|
|
|
|
|
|
|
public static String adjustString(final String value, final char fill, final int length, final boolean head) { |
|
|
|
if (value == null || length < 0) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
final int srcLen = value.getBytes().length; |
|
|
|
if (srcLen >= length) { |
|
|
|
return new String(value.getBytes(), 0, length); |
|
|
|
} |
|
|
|
final StringBuffer sb = new StringBuffer().append(fill); |
|
|
|
int len = length - srcLen; |
|
|
|
while (len != 1) { |
|
|
|
len /= 2; |
|
|
|
sb.append(sb); |
|
|
|
} |
|
|
|
sb.append(sb).setLength(length - srcLen); |
|
|
|
if (head) { |
|
|
|
return sb.append(value).toString(); |
|
|
|
} |
|
|
|
return sb.insert(0, value).toString(); |
|
|
|
} |
|
|
|
|
|
|
|
public static String centerString(final String value, final int len) { |
|
|
|
final int length = value.getBytes().length; |
|
|
|
if (length >= len) { |
|
|
|
return value.substring(0, len); |
|
|
|
} |
|
|
|
final int subLen = (len - length) / 2; |
|
|
|
return padString(padString(value, length + subLen, -1), len, 1); |
|
|
|
} |
|
|
|
|
|
|
|
public static String padString(final String str, final int len, final int direct) { |
|
|
|
if (str == null) { |
|
|
|
return ""; |
|
|
|
} |
|
|
|
if (str.length() >= len) { |
|
|
|
return str; |
|
|
|
} |
|
|
|
final int fill = len - str.length(); |
|
|
|
int precount = 0; |
|
|
|
int postcount = 0; |
|
|
|
if (direct < 0) { |
|
|
|
precount = fill; |
|
|
|
} else if (direct > 0) { |
|
|
|
postcount = fill; |
|
|
|
} else { |
|
|
|
precount = ((fill % 2 > 0) ? (fill / 2 + 1) : (fill / 2)); |
|
|
|
postcount = fill - precount; |
|
|
|
} |
|
|
|
final StringBuffer sb = new StringBuffer(); |
|
|
|
for (int i = 0; i < precount; ++i) { |
|
|
|
sb.append(" "); |
|
|
|
} |
|
|
|
sb.append(str); |
|
|
|
for (int i = 0; i < postcount; ++i) { |
|
|
|
sb.append(" "); |
|
|
|
} |
|
|
|
return sb.toString(); |
|
|
|
} |
|
|
|
|
|
|
|
// public static byte[] encryptTDes(final byte[] key, final byte[] plainText) { |
|
|
|
// final Des3Cipher cipher = new Des3Cipher(key); |
|
|
|
// final BytesBuffer bb = new BytesBuffer(plainText); |
|
|
|
// while (bb.length() % 8 != 0) { |
|
|
|
// bb.append((byte) 0); |
|
|
|
// } |
|
|
|
// final BytesBuffer result = new BytesBuffer(); |
|
|
|
// for (int i = 0; i < bb.length() / 8; i += 8) { |
|
|
|
// final byte[] p = bb.getValueN(i, 8); |
|
|
|
// final byte[] c = new byte[8]; |
|
|
|
// cipher.encrypt(p, c); |
|
|
|
// result.append(c); |
|
|
|
// } |
|
|
|
// return result.getValue(); |
|
|
|
// } |
|
|
|
|
|
|
|
// public static byte[] decryptTDes(final byte[] key, final byte[] ciperText) { |
|
|
|
// final Des3Cipher cipher = new Des3Cipher(key); |
|
|
|
// final BytesBuffer bb = new BytesBuffer(ciperText); |
|
|
|
// final BytesBuffer result = new BytesBuffer(); |
|
|
|
// for (int i = 0; i < bb.length() / 8; i += 8) { |
|
|
|
// final byte[] p = bb.getValueN(i, 8); |
|
|
|
// final byte[] c = new byte[8]; |
|
|
|
// cipher.decrypt(p, c); |
|
|
|
// result.append(c); |
|
|
|
// } |
|
|
|
// return result.getValue(); |
|
|
|
// } |
|
|
|
|
|
|
|
// public static byte[] encryptPboc(final byte[] key, final byte[] plainText) { |
|
|
|
// final Des3Cipher cipher = new Des3Cipher(key); |
|
|
|
// final BytesBuffer bb = new BytesBuffer(plainText); |
|
|
|
// if (bb.length() % 8 != 0) { |
|
|
|
// bb.append((byte) (-128)); |
|
|
|
// } |
|
|
|
// while (bb.length() % 8 != 0) { |
|
|
|
// bb.append((byte) 0); |
|
|
|
// } |
|
|
|
// final BytesBuffer result = new BytesBuffer(); |
|
|
|
// for (int i = 0; i < bb.length() / 8; i += 8) { |
|
|
|
// final byte[] p = bb.getValueN(i, 8); |
|
|
|
// final byte[] c = new byte[8]; |
|
|
|
// cipher.encrypt(p, c); |
|
|
|
// result.append(c); |
|
|
|
// } |
|
|
|
// return result.getValue(); |
|
|
|
// } |
|
|
|
|
|
|
|
// public static byte[] decryptPboc(final byte[] key, final byte[] ciperText) { |
|
|
|
// return decryptTDes(key, ciperText); |
|
|
|
// } |
|
|
|
|
|
|
|
// public static byte[] DiversifyKey(final byte[] key, final byte[] plainText) { |
|
|
|
// final byte[] plain = new byte[8]; |
|
|
|
// System.arraycopy(plainText, 0, plain, 0, 8); |
|
|
|
// final byte[] leftKey = encryptPboc(key, plain); |
|
|
|
// for (int i = 0; i < 8; ++i) { |
|
|
|
// plain[i] ^= -1; |
|
|
|
// } |
|
|
|
// final byte[] rightKey = encryptPboc(key, plain); |
|
|
|
// final BytesBuffer bb = new BytesBuffer(leftKey); |
|
|
|
// return bb.append(rightKey).getValue(); |
|
|
|
// } |
|
|
|
|
|
|
|
// public static byte[] PBOCMac(final byte[] message, final byte[] key, final byte[] initData) { |
|
|
|
// final BytesBuffer keyBuffer = new BytesBuffer(key); |
|
|
|
// final BytesBuffer bb = new BytesBuffer(message); |
|
|
|
// if (bb.length() % 8 != 0) { |
|
|
|
// bb.append((byte) (-128)); |
|
|
|
// } |
|
|
|
// while (bb.length() % 8 != 0) { |
|
|
|
// bb.append((byte) 0); |
|
|
|
// } |
|
|
|
// final DesCipher des = new DesCipher(keyBuffer.getValueN(0, 8)); |
|
|
|
// final byte[] cipherData = new byte[8]; |
|
|
|
// System.arraycopy(initData, 0, cipherData, 0, 8); |
|
|
|
// for (int i = 0; i < bb.length(); i += 8) { |
|
|
|
// final byte[] data = bb.getValueN(i, 8); |
|
|
|
// for (int j = 0; j < 8; ++j) { |
|
|
|
// final byte[] array = cipherData; |
|
|
|
// final int n = j; |
|
|
|
// array[n] ^= data[j]; |
|
|
|
// } |
|
|
|
// des.encrypt(cipherData, cipherData); |
|
|
|
// } |
|
|
|
// des.setKey(keyBuffer.getValueN(8, 8)); |
|
|
|
// des.decrypt(cipherData, cipherData); |
|
|
|
// des.setKey(keyBuffer.getValueN(0, 8)); |
|
|
|
// des.encrypt(cipherData, cipherData); |
|
|
|
// return new byte[] { cipherData[0], cipherData[1], cipherData[2], cipherData[3] }; |
|
|
|
// } |
|
|
|
|
|
|
|
public static String getSystemDate() { |
|
|
|
final Date date = new Date(); |
|
|
|
final SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); |
|
|
|
return format.format(date); |
|
|
|
} |
|
|
|
|
|
|
|
public static String getSystemTime() { |
|
|
|
final Date date = new Date(); |
|
|
|
final SimpleDateFormat format = new SimpleDateFormat("HHmmss"); |
|
|
|
return format.format(date); |
|
|
|
} |
|
|
|
|
|
|
|
public static String getSystemDateTime() { |
|
|
|
final Date date = new Date(); |
|
|
|
final SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); |
|
|
|
return format.format(date); |
|
|
|
} |
|
|
|
|
|
|
|
public static byte[] read(final InputStream in, final int size) throws IOException, InterruptedIOException { |
|
|
|
final byte[] buf = new byte[size]; |
|
|
|
int rc; |
|
|
|
for (int remain = size, received = 0; remain > 0; remain -= rc, received += rc) { |
|
|
|
rc = in.read(buf, received, remain); |
|
|
|
if (rc == -1) { |
|
|
|
throw new IOException("\u7f51\u7edc\u8fde\u63a5\u53ef\u80fd\u51fa\u73b0\u95ee\u9898"); |
|
|
|
} |
|
|
|
} |
|
|
|
return buf; |
|
|
|
} |
|
|
|
} |