解密OK
This commit is contained in:
parent
a768491dd4
commit
6bb0f51a8e
|
@ -154,6 +154,7 @@ bool TokenController::Valid(const Message& msg)
|
|||
return true;
|
||||
}
|
||||
|
||||
// msg所有成员序列化为data
|
||||
static bool Encrypt(Buffer& data, const Buffer& pass)
|
||||
{
|
||||
if (data.Length() <= 3) return false;
|
||||
|
@ -178,36 +179,25 @@ static bool Encrypt(Buffer& data, const Buffer& pass)
|
|||
RC4::Encrypt(bs, pass);
|
||||
|
||||
ms.Write(crc);
|
||||
|
||||
return data.SetLength(ms.Position());
|
||||
}
|
||||
|
||||
// Decrypt(Buffer(msg.Data,len),Key) 只处理data部分
|
||||
static bool Decrypt(Buffer& data, const Buffer& pass)
|
||||
{
|
||||
if(data.Length() <= 3) return false;
|
||||
if(pass.Length() == 0) return true;
|
||||
|
||||
// 握手不加密
|
||||
byte code = data[0] & 0x0F;
|
||||
if(code == 0x01) return true;
|
||||
|
||||
auto msgDataLen = data.Length() - 2;
|
||||
Stream ms(data);
|
||||
//ms.Seek(2); // 传进来的就是Data部分
|
||||
ms.Seek(msgDataLen);
|
||||
auto crc = ms.ReadUInt16(); // 读取数据包中crc
|
||||
|
||||
auto len = data.Length();
|
||||
auto bs = ms.ReadArray(len - 2); // 去掉crc的位置
|
||||
data.SetLength(msgDataLen);
|
||||
ByteArray bs(data.GetBuffer(),data.Length());
|
||||
RC4::Encrypt(bs, pass); // 解密
|
||||
|
||||
RC4::Encrypt(bs, pass);
|
||||
|
||||
// 新的加密指令最后有2字节的明文校验码
|
||||
if(ms.Position() + 2 > ms.Length)
|
||||
{
|
||||
debug_printf("不支持旧版本指令解密!");
|
||||
return false;
|
||||
}
|
||||
|
||||
auto crc = Crc::Hash16(bs);
|
||||
if(ms.ReadUInt16() != crc) return false;
|
||||
auto crc2 = Crc::Hash16(bs); // 明文的CRC值
|
||||
if(crc != crc2) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -40,8 +40,8 @@ bool TokenMessage::Read(Stream& ms)
|
|||
|
||||
if(ms.Remain() < len) return false;
|
||||
|
||||
// 避免错误指令超长,导致溢出
|
||||
if(Data == _Data && len > ArrayLength(_Data))
|
||||
// 避免错误指令超长,导致溢出 data后面有crc
|
||||
if(Data == _Data && (len + 2) > ArrayLength(_Data))
|
||||
{
|
||||
debug_printf("错误指令,长度 %d 大于消息数据缓冲区长度 %d \r\n", len, ArrayLength(_Data));
|
||||
//assert_param(false);
|
||||
|
@ -49,7 +49,7 @@ bool TokenMessage::Read(Stream& ms)
|
|||
}
|
||||
if(len > 0)
|
||||
{
|
||||
Buffer bs(Data, len);
|
||||
Buffer bs(Data, len+2); // DATA + CRC 位置
|
||||
ms.Read(bs);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue