!41 解决加密链路解码异常问题

Merge pull request !41 from NichenFly/dev
This commit is contained in:
傲世孤尘
2023-11-08 13:23:13 +00:00
committed by Gitee
2 changed files with 4 additions and 7 deletions
@@ -83,7 +83,6 @@ public class ProxyMessageDecoder extends LengthFieldBasedFrameDecoder {
// 考虑isSecurity为null的情况,null的情况也为false
if (isSecurity != null && isSecurity) {
log.info("执行解密逻辑");
int packageLength = in.readInt();
if (in.readableBytes() < packageLength) {
return null;
@@ -103,7 +102,6 @@ public class ProxyMessageDecoder extends LengthFieldBasedFrameDecoder {
buf = Unpooled.wrappedBuffer(decryptedData);
} else {
buf = in;
log.info("链路不加密解码");
}
ProxyMessage proxyMessage = new ProxyMessage();
@@ -65,10 +65,8 @@ public class ProxyMessageEncoder extends MessageToByteEncoder<ProxyMessage> {
// 考虑isSecurity为null的情况,null的情况也为false
if (isSecurity != null && isSecurity) {
log.info("执行加密逻辑");
buf = Unpooled.buffer(bodyLength);
buf = Unpooled.directBuffer(bodyLength);
} else {
log.info("不执行加密的链路编码");
buf = out;
}
@@ -92,14 +90,15 @@ public class ProxyMessageEncoder extends MessageToByteEncoder<ProxyMessage> {
// 考虑isSecurity为null的情况,null的情况也为false
if (isSecurity != null && isSecurity) {
// 执行加密
byte[] data = new byte[bodyLength];
byte[] data = new byte[buf.writerIndex()];
buf.readBytes(data);
// 获取加密密钥
Attribute<byte[]> secureKeyAttr = ctx.attr(SECURE_KEY);
byte[] secureKey = secureKeyAttr.get();
// 执行加密
byte[] encryptedData = SmEncryptUtil.encryptBySm4(secureKey, data);
out.writeByte(encryptedData.length);
out.writeInt(encryptedData.length);
out.writeBytes(encryptedData);
}