JLChen
2021-12-07 d6a2ca78cc573243a44ff805ac01935fd36bb6d8
HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/protocol/LinkMessageDecoder.java
@@ -1,9 +1,12 @@
package com.hdl.sdk.connect.protocol;
import com.hdl.sdk.common.utils.LogUtils;
import com.hdl.sdk.connect.config.HDLLinkConfig;
import com.hdl.sdk.common.event.EventDispatcher;
import com.hdl.sdk.common.utils.ByteUtils;
import com.hdl.sdk.connect.bean.LinkResponse;
import com.hdl.sdk.connect.utils.AesUtil;
import com.hdl.sdk.connect.utils.ProtocolParse;
import com.hdl.sdk.socket.codec.ByteToMessageDecoder;
@@ -56,7 +59,23 @@
            if (bodyLength > 0) {
                if (byteArray.length >= bodyLength + bodyStartIndex) {
                    byte[] body = ByteUtils.getRangeBytes(bytes, bodyStartIndex, bodyStartIndex + bodyLength);
                    response.setData(new String(body, "utf-8"));
                    if (HDLLinkConfig.getInstance().ifNeedEncrypt(response.getTopic())) {
                        //需要解密
                        byte[] bodyBytes = AesUtil.aesDecrypt(body, HDLLinkConfig.getInstance().getLocalSecret());
//                        byte[] bodyBytes = AESUtils.decryptAES(body,AuthenticateConfig.getInstance().getLocalSecret());
                        if (bodyBytes != null) {
                            response.setData(new String(bodyBytes, "utf-8"));
//                            LogUtils.i("TAG", "解密 主题:"+response.getTopic()+ " body: "+response.getData());
                        } else {
                            //解密失败,返回原数据
                            response.setData(new String(body, "utf-8"));
                            LogUtils.e( "解密失败");
                        }
                    } else {
                        response.setData(new String(body, "utf-8"));
                    }
                    if (byteArray.length >= bodyLength + bodyStartIndex) {
                        //保存余留