7E.....7E
public static List<byte[]> msgUnPkg(final byte[] msg){
List<byte[]> msgBytes=new LinkedList<byte[]>();
int i=0;
int k=0,index=0;
while(i<msg.length){
if(msg[i]==0x7E){
if(index%2!=0){
byte[] msgB3=new byte[i-k+1];
System.arraycopy(msg, k, msgB3, 0, msgB3.length);
msgBytes.add(msgB3);
}else{
k=i;
}
index++;
}
i++;
}
return msgBytes;
}
5B......5D
package com.zhonghong.platforms.dexchang.util;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.codec.net.URLCodec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MsgConverUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(MsgConverUtil.class);
public static MessageBuilder mainMb=null;
public static MessageBuilder subMb=null;
/**
* 消息转译
* @param msg
* @return
*/
public static byte[] translation(byte[] rsdata){
byte[] trans5b = new byte[2];
trans5b[0] = 0x5a;
trans5b[1] = 0x01;
byte[] trans5a = new byte[2];
trans5a[0] = 0x5a;
trans5a[1] = 0x02;
byte[] trans5d = new byte[2];
trans5d[0] = 0x5e;
trans5d[1] = 0x01;
byte[] trans5e = new byte[2];
trans5e[0] = 0x5e;
trans5e[1] = 0x02;
for(int i=1; i<rsdata.length-1; i++){
if(rsdata[i]==0x5b){
byte[] middleByte = new byte[rsdata.length+1];
System.arraycopy(rsdata, 0, middleByte, 0, i);
System.out.println("1."+ByteUtils.bytesToHexStringSpace(middleByte));
System.arraycopy(trans5b, 0, middleByte, i, 2);
System.out.println("2."+ByteUtils.bytesToHexStringSpace(middleByte));
System.arraycopy(rsdata, i+1, middleByte, 2+i, rsdata.length-i-1);
System.out.println("3."+ByteUtils.bytesToHexStringSpace(middleByte));
rsdata = Arrays.copyOf(middleByte, middleByte.length);
System.out.println("4."+ByteUtils.bytesToHexStringSpace(rsdata));
continue;
}
if(rsdata[i]==0x5a){
byte[] middleByte = new byte[rsdata.length+1];
System.arraycopy(rsdata, 0, middleByte, 0, i);
System.arraycopy(trans5a, 0, middleByte, i, 2);
System.arraycopy(rsdata, i+1, middleByte, 2+i, rsdata.length-i-1);
rsdata = Arrays.copyOf(middleByte, middleByte.length);
continue;
}
if(rsdata[i]==0x5d){
byte[] middleByte = new byte[rsdata.length+1];
System.arraycopy(rsdata, 0, middleByte, 0, i);
System.arraycopy(trans5d, 0, middleByte, i, 2);
System.arraycopy(rsdata, i+1, middleByte, 2+i, rsdata.length-i-1);
rsdata = Arrays.copyOf(middleByte, middleByte.length);
continue;
}
if(rsdata[i]==0x5e){
byte[] middleByte = new byte[rsdata.length+1];
System.arraycopy(rsdata, 0, middleByte, 0, i);
System.arraycopy(trans5e, 0, middleByte, i, 2);
System.arraycopy(rsdata, i+1, middleByte, 2+i, rsdata.length-i-1);
rsdata = Arrays.copyOf(middleByte, middleByte.length);
continue;
}
}
return rsdata;
}
/**
* 消息反转译
* @param msg
* @return
*/
public static byte[] unTranslation(byte[] data){
byte[] rsdata = data.clone();
for(int i=1; i<rsdata.length-1; i++){
if(rsdata[i]==0x5a&&rsdata[i+1]==0x01){
byte[] middleByte = new byte[rsdata.length-1];
System.arraycopy(rsdata, 0, middleByte, 0, i);
middleByte[i]=0x5b;
System.arraycopy(rsdata, i+2, middleByte, i+1, rsdata.length-i-2);
rsdata = Arrays.copyOf(middleByte, middleByte.length);
continue;
}
if(rsdata[i]==0x5a&&rsdata[i+1]==0x02){
byte[] middleByte = new byte[rsdata.length-1];
System.arraycopy(rsdata, 0, middleByte, 0, i);
middleByte[i]=0x5a;
System.arraycopy(rsdata, i+2, middleByte, i+1, rsdata.length-i-2);
rsdata = Arrays.copyOf(middleByte, middleByte.length);
continue;
}
if(rsdata[i]==0x5e&&rsdata[i+1]==0x01){
byte[] middleByte = new byte[rsdata.length-1];
System.arraycopy(rsdata, 0, middleByte, 0, i);
middleByte[i]=0x5d;
System.arraycopy(rsdata, i+2, middleByte, i+1, rsdata.length-i-2);
rsdata = Arrays.copyOf(middleByte, middleByte.length);
continue;
}
if(rsdata[i]==0x5e&&rsdata[i+1]==0x02){
byte[] middleByte = new byte[rsdata.length-1];
System.arraycopy(rsdata, 0, middleByte, 0, i);
middleByte[i]=0x5e;
System.arraycopy(rsdata, i+2, middleByte, i+1, rsdata.length-i-2);
rsdata = Arrays.copyOf(middleByte, middleByte.length);
continue;
}
}
return rsdata;
}
/**
* 生成CRC16-CCITT校验位
* @param 二进制消息
*/
public static byte[] genCrcCode(byte[] bytes)
{
if (bytes.length < 1)
{
return null;
}
int crc = 0xFFFF; // initial value
int polynomial = 0x1021; // 0001 0000 0010 0001 (0, 5, 12)
for (byte b : bytes) {
for (int i = 0; i < 8; i++) {
boolean bit = ((b >> (7 - i) & 1) == 1);
boolean c15 = ((crc >> 15 & 1) == 1);
crc <<= 1;
if (c15 ^ bit)
crc ^= polynomial;
}
}
crc &= 0xffff;
return TypeChangeUtil.intToWORD(crc);
}
public static boolean valiMsgCRC(byte[] msg){
byte[] msgCRCBody=new byte[msg.length-4];
System.arraycopy(msg, 1,msgCRCBody,0, msgCRCBody.length);
System.out.println(ByteUtils.bytesToHexStringSpace(msgCRCBody));
byte[] CRCCode=MsgConverUtil.genCrcCode(msgCRCBody);
String c1=ByteUtils.bytesToHexString(CRCCode);
byte[] msgCRC=new byte[2];
System.arraycopy(msg, msg.length-3,msgCRC,0, msgCRC.length);
String c2=ByteUtils.bytesToHexString(msgCRC);
if(c1.equals(c2)){
return true;
}
LOGGER.warn("消息效验码不匹配(src="+c2+",desc="+c1+").");
return false;
}
public static synchronized byte[] composeMainLinkPack(final byte[] msg){
if(msg[0]==0x5B&&msg.length>10&&msg[msg.length-1]!=0x5D){//开始
mainMb=new MessageBuilder();
mainMb.append(msg);
//LOGGER.warn("(main)消息开始组包(1):"+ByteUtils.bytesToHexStringSpace(mainMb.getBytes()));
}else if(msg[0]!=0x5B&&msg[msg.length-1]==0x5D){//结尾
mainMb.append(msg);
byte[] msg2=mainMb.getBytes();
//LOGGER.warn("(main)消息组包完成(2):"+ByteUtils.bytesToHexStringSpace(msg2));
mainMb=null;
return msg2;
}else if(msg[0]!=0x5B&&msg[msg.length-1]!=0x5D){//中间部分
mainMb.append(msg);
//LOGGER.warn("(main)消息组包中(3):"+ByteUtils.bytesToHexStringSpace(mainMb.getBytes()));
}else if(msg[0]==0x5B&&msg[msg.length-1]==0x5D){//完整消息,可能出现粘包情况
//LOGGER.warn("(main)完整消息包(4):"+ByteUtils.bytesToHexStringSpace(msg));
return msg;
}
return null;
}
public static byte[] composeSubLinkPack(final byte[] msg){
if(msg[0]==0x5B&&msg.length>10&&msg[msg.length-1]!=0x5D){//开始
subMb=new MessageBuilder();
subMb.append(msg);
//LOGGER.warn("(sub)消息开始组包(1):"+ByteUtils.bytesToHexStringSpace(subMb.getBytes()));
}else if(msg[0]!=0x5B&&msg[msg.length-1]==0x5D){//结尾
subMb.append(msg);
byte[] msg2=subMb.getBytes();
//LOGGER.warn("(sub)消息组包完成(2):"+ByteUtils.bytesToHexStringSpace(msg2));
subMb=null;
return msg2;
}else if(msg[0]!=0x5B&&msg[msg.length-1]!=0x5D){//中间部分
subMb.append(msg);
//LOGGER.warn("(sub)消息组包中(3):"+ByteUtils.bytesToHexStringSpace(subMb.getBytes()));
}else if(msg[0]==0x5B&&msg[msg.length-1]==0x5D){//完整消息,可能出现粘包情况
//LOGGER.warn("(sub)完整消息包(4):"+ByteUtils.bytesToHexStringSpace(msg));
return msg;
}
return null;
}
public static List<byte[]> msgUnPkg(byte[] msg){
List<byte[]> msgBytes=new LinkedList<byte[]>();
int i=0;
int s=0,e=0;
while(i<msg.length){
if(msg[i]==0x5B){
s=i;
}
if(msg[i]==0x5D){
e=i;
byte[] msgB2=new byte[(e-s)+1];
System.arraycopy(msg, s, msgB2, 0, msgB2.length);
msgBytes.add(msgB2);
}
i++;
}
//LOGGER.warn("数据包个数:"+msgBytes.size());
return msgBytes;
}
public static byte[] encryptData1(int key,byte[] msg){
int idx = 0;
if (0==key)
{
key = 1;
}
int mk=MsgConfig.M1;
if(mk==0){
mk=1;
}
while ( idx < msg.length)
{
key = MsgConfig.IA1 * (key % mk) + MsgConfig.IC1;
msg[idx++] ^= (byte)((key >> 20) & 0xFF);
}
return msg;
}
public static byte[] decryptData1(long key,byte[] msg)
{
int idx = 0;
if (0==key)
{
key = 1;
}
int mk=MsgConfig.M1;
if(mk==0){
mk=1;
}
while ( idx < msg.length)
{
key = MsgConfig.IA1 * (key % mk) + MsgConfig.IC1;
msg[idx++] ^= (byte)((key >> 20) & 0xFF);
}
return msg;
}
public static byte[] encryptNDataNO(long _key, byte[] msg) {
long key = _key;
int size = msg.length;
if (key == 0)
key = 1;
int i = 0;
while(i < size) {
int x = (int)(MsgConfig.IA1 * (key % MsgConfig.M1));
String tx = ByteUtils.ToHexString(x&0x0FFFFFFFF);
long t = Long.valueOf(tx, 16);
key = (t + MsgConfig.IC1);
//short te = (short)(b[i] ^ (short) (key >> 20) & 0xFF);
msg[i++] ^= ((byte) (key >> 20) & 0xFF);
}
return msg;
}
public static byte[] encryptDataDll(int _key, byte[] msg){
EncryptUtil helloWorld=new EncryptUtil();
helloWorld.size=msg.length;
helloWorld.bytes=msg;
helloWorld.key=_key;
helloWorld.encrypt1=MsgConfig.M1;
helloWorld.encrypt2=MsgConfig.IA1;
helloWorld.encrypt3=MsgConfig.IC1;
helloWorld.encryptDLL();
return helloWorld.bytes;
}
/*public static byte[] encryptDLL(int _key, byte[] msg){
try {
//byte[] bb=TypeChangeUtil.hexStringToByte("EF1051B72F82569A424F40F1CCD3D5DEA2C7A75E2F858D73E94AA9037444C0B7DE5C72FB9D9C59898ED0510849590CB3DC36C433AE12859DDA64228B666623D4459E0C108D9005A5D33F7983C6C5DBE5CE5C76FC1C417E02842A8024D7F27E8A88951435C37747BF4F1B06242E1DAAE834F03B57F2D7495AF6619F97E3AEFC748F9D3E324C2C20EE06E45CAC1401038E28CAF0D6398AD6D83F5E07C5CFFFD459F81919E31443BB6EC5D66B8164");
JNative jnative=new JNative("dllEncrypt","encrypt");
jnative.setRetVal(Type.INT);
Pointer pointer = new Pointer(MemoryBlockFactory.createMemoryBlock(msg.length));
pointer.setMemory(msg);
jnative.setParameter(0, Type.INT,_key+"");
jnative.setParameter(1,pointer);
jnative.setParameter(2, Type.INT,msg.length+"");
jnative.invoke();
LOGGER.debug("加解密结果:"+jnative.getRetValAsInt());
jnative.dispose();
return pointer.getMemory();
} catch (NativeException e) {
LOGGER.error(e.getMessage());
} catch (IllegalAccessException e) {
LOGGER.error(e.getMessage());
}
return msg;
}*/
public static void main(String[] args) {;
String a=new String("我加解密结果a".getBytes(), Charset.forName("ISO8859-1"));
char[] s=a.toCharArray();
byte[] b=new byte[s.length];
for(int i=0;i<s.length;i++){
b[i]=(byte)s[i];
}
System.out.println(new String(b));
System.out.println(new String(b)+""+ new String(b, Charset.forName("UTF-8")));
}
}