You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 lines
4.5 KiB
1 lines
4.5 KiB
package com.fr.plugin;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import com.fr.log.FineLoggerFactory;
import org.bouncycastle.crypto.engines.DESEngine;
import org.bouncycastle.crypto.params.KeyParameter;
/**
* 3DES加密
*
* @author xxx
* @date 2017-10-19
*/
public class ThreeDESUtil {
private static final byte[] HEX_DIGITS = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70};
private static DESEngine cipher = new DESEngine();
private static byte[] groupingEncrypt(byte[] key, byte[] data) {
int range = 8;
int iRet = data.length % range;
byte[] out;
if (iRet > 0) {
int length = data.length + (range - iRet);
out = new byte[length];
}
else {
out = new byte[data.length];
}
cipher.init(true, new KeyParameter(key));
int i;
for (i = 0; i < data.length / range; i++) {
cipher.processBlock(data, i * range, out, i * range);
}
if (iRet > 0) {
byte[] temp = new byte[range];
System.arraycopy(data, i * range, temp, 0, iRet);
temp[iRet] = -128;
cipher.processBlock(temp, 0, out, i * range);
}
byte[] toasc = new byte[out.length * 2];
int k = 0;
int j = 0;
for (i = 0; i < out.length; i++) {
k = out[i];
toasc[j++] = HEX_DIGITS[k >>> 4 & 0xf];
toasc[j++] = HEX_DIGITS[k & 0xf];
}
return toasc;
}
private static byte[] groupingDecrypt(byte[] key, byte[] data) {
byte[] result = null;
int range = 2;
int group = 8;
int k = 0;
byte[] tobyte = new byte[data.length / range];
for (int i = 0; i < data.length; i += range) {
char ch;
if (data[i] <= 57) {
ch = (char)(data[i] & 0xf);
}
else {
ch = (char)((data[i] & 0xf) + 9);
}
ch <<= '\004';
if (data[i + 1] <= 57) {
ch |= data[i + 1] & 0xf;
}
else {
ch |= (data[i + 1] & 0xf) + 9;
}
tobyte[k] = (byte)ch;
k++;
}
cipher.init(false, new KeyParameter(key));
if (tobyte.length % group == 0) {
byte[] out = new byte[tobyte.length];
for (int i = 0; i < tobyte.length / group; i++) {
cipher.processBlock(tobyte, i * group, out, i * group);
}
int len = padCount(out);
result = new byte[out.length - len];
System.arraycopy(out, 0, result, 0, out.length - len);
return result;
}
else {
return result;
}
}
private static int padCount(byte[] in) {
int count;
for (count = in.length; count > 0; count--) {
if (in[count - 1] != 0) {
break;
}
}
int minNum = -128;
if (in[count - 1] == minNum) {
return (in.length - count) + 1;
}
else {
return in.length - count;
}
}
/**
* 加密
*
* @param code
* @return
*/
public static final String encode(String code, String key) {
if (code == null) {
return null;
}
else {
try {
byte[] cryptogragh = groupingEncrypt(key.getBytes(StandardCharsets.ISO_8859_1), code.getBytes(StandardCharsets.UTF_8.toString()));
return new String(cryptogragh, StandardCharsets.UTF_8.toString());
}
catch (UnsupportedEncodingException e) {
FineLoggerFactory.getLogger().error(e.toString(), e);
return null;
}
}
}
/**
* 解密
*
* @param code
* @return
*/
public static final String decode(String code, String key) {
if (code == null) {
return null;
}
else {
try {
byte[] debyte = groupingDecrypt(key.getBytes(StandardCharsets.ISO_8859_1), code.getBytes(StandardCharsets.UTF_8.toString()));
return debyte != null ? new String(debyte, StandardCharsets.UTF_8.toString()) : null;
}
catch (UnsupportedEncodingException e) {
FineLoggerFactory.getLogger().error(e.toString(), e);
return null;
}
}
}
}
|