55#include " PacketSenderKCP.h"
66#include " Engine/KBDebug.h"
77
8- #ifndef KBENGINE_NO_CRYPTO
9- #include " cryptlib.h"
10- #include " rdrand.h"
11- #include " secblock.h"
12- #endif
8+ EncryptionFilter::~EncryptionFilter ()
9+ {
10+ }
1311
14- BlowfishFilter::BlowfishFilter (int keySize):
12+ BlowfishFilter::BlowfishFilter (int keySize) :
1513 isGood_(false ),
1614 pPacket_(new MemoryStream()),
1715 pEncryptStream_(new MemoryStream()),
1816 packetLen_(0 ),
19- padSize_(0 )
17+ padSize_(0 ),
18+ key_(),
19+ keySize_(0 ),
20+ pBlowFishKey_(NULL )
2021{
21- #ifndef KBENGINE_NO_CRYPTO
22- key_.Init (0 , keySize);
22+ unsigned char buf[20 ] = " " ;
23+ RAND_bytes (buf, 20 );
24+ key_ = (char *)buf;
25+ keySize_ = key_.Len ();
2326
24- CryptoPP::RDRAND rng;
25- rng.GenerateBlock (key_.GetData (), key_.Num ());
2627 init ();
27- #endif
2828}
2929
30- BlowfishFilter::BlowfishFilter (const TArray<uint8> & key):
30+ BlowfishFilter::BlowfishFilter (const FString & key) :
3131 isGood_(false ),
32- key_(key),
3332 pPacket_(new MemoryStream()),
3433 pEncryptStream_(new MemoryStream()),
3534 packetLen_(0 ),
36- padSize_(0 )
35+ padSize_(0 ),
36+ key_(key),
37+ keySize_(key_.Len()),
38+ pBlowFishKey_(NULL )
3739{
3840 init ();
3941}
@@ -42,31 +44,32 @@ BlowfishFilter::~BlowfishFilter()
4244{
4345 KBE_SAFE_RELEASE (pPacket_);
4446 KBE_SAFE_RELEASE (pEncryptStream_);
47+ KBE_SAFE_RELEASE (pBlowFishKey_);
4548}
4649
4750bool BlowfishFilter::init ()
4851{
49- #ifndef KBENGINE_NO_CRYPTO
50- if (key_.Num () >= encripter.MinKeyLength () && key_.Num () <= encripter.MaxKeyLength ())
52+ pBlowFishKey_ = new BF_KEY;
53+
54+ if (MIN_KEY_SIZE <= keySize_ && keySize_ <= MAX_KEY_SIZE)
5155 {
52- encripter. SetKey (key_. GetData (), key_. Num ());
53- decripter. SetKey (key_. GetData (), key_.Num ( ));
56+
57+ BF_set_key ( this -> pBlowFishKey (), key_.Len (), reinterpret_cast < const unsigned char *>( TCHAR_TO_ANSI (*key_) ));
5458 isGood_ = true ;
5559 }
5660 else
5761 {
58- ERROR_MSG (" BlowfishFilter::init: invalid length %d" , key_.Num ());
62+ ERROR_MSG (" BlowfishFilter::init: invalid length %d" , key_.Len ());
5963 isGood_ = false ;
6064 }
61- # endif
65+
6266 return isGood_;
6367}
6468
6569void BlowfishFilter::encrypt (MemoryStream *pMemoryStream)
6670{
6771 // BlowFish 每次只能加密和解密8字节数据
6872 // 不足8字节则填充0
69- #ifndef KBENGINE_NO_CRYPTO
7073 uint8 padSize = 0 ;
7174
7275 if (pMemoryStream->length () % BLOCK_SIZE != 0 )
@@ -86,12 +89,10 @@ void BlowfishFilter::encrypt(MemoryStream *pMemoryStream)
8689
8790 pMemoryStream->swap (*pEncryptStream_);
8891 pEncryptStream_->clear (false );
89- #endif
9092}
9193
9294void BlowfishFilter::encrypt (uint8 *buf, MessageLengthEx len)
9395{
94- #ifndef KBENGINE_NO_CRYPTO
9596 if (len % BLOCK_SIZE != 0 )
9697 {
9798 ERROR_MSG (" BlowfishFilter::encrypt: Input length (%d) is not a multiple of block size " , len);
@@ -113,9 +114,8 @@ void BlowfishFilter::encrypt(uint8 *buf, MessageLengthEx len)
113114 prevBlock = *(uint64*)(data + i);
114115 }
115116
116- encripter. ProcessData (data + i, data + i, BLOCK_SIZE );
117+ BF_ecb_encrypt (data + i, data + i, this -> pBlowFishKey (), BF_ENCRYPT );
117118 }
118- #endif
119119}
120120
121121void BlowfishFilter::encrypt (uint8 *buf, MessageLengthEx offset, MessageLengthEx len)
@@ -130,7 +130,6 @@ void BlowfishFilter::decrypt(MemoryStream *pMemoryStream)
130130
131131void BlowfishFilter::decrypt (uint8 *buf, MessageLengthEx len)
132132{
133- #ifndef KBENGINE_NO_CRYPTO
134133 if (len % BLOCK_SIZE != 0 )
135134 {
136135 ERROR_MSG (" BlowfishFilter::decrypt: Input length (%d) is not a multiple of block size " , len);
@@ -141,16 +140,15 @@ void BlowfishFilter::decrypt(uint8 *buf, MessageLengthEx len)
141140 uint64 prevBlock = 0 ;
142141 for (uint32 i = 0 ; i < len; i += BLOCK_SIZE)
143142 {
144- decripter. ProcessData (data + i, data + i, BLOCK_SIZE );
143+ BF_ecb_encrypt (data + i, data + i, this -> pBlowFishKey (), BF_DECRYPT );
145144
146145 if (prevBlock != 0 )
147146 {
148147 *(uint64*)(data + i) = *(uint64*)(data + i) ^ (prevBlock);
149148 }
150-
149+
151150 prevBlock = *(uint64*)(data + i);
152151 }
153- #endif
154152}
155153
156154void BlowfishFilter::decrypt (uint8 *buf, MessageLengthEx offset, MessageLengthEx len)
@@ -160,21 +158,18 @@ void BlowfishFilter::decrypt(uint8 *buf, MessageLengthEx offset, MessageLengthEx
160158
161159bool BlowfishFilter::send (PacketSenderBase* pPacketSender, MemoryStream *pPacket)
162160{
163- #ifndef KBENGINE_NO_CRYPTO
164161 if (!isGood_)
165162 {
166163 ERROR_MSG (" BlowfishFilter::send: Dropping packet due to invalid filter" );
167164 return false ;
168165 }
169166
170167 encrypt (pPacket);
171- #endif
172168 return pPacketSender->send (pPacket);;
173169}
174170
175171bool BlowfishFilter::recv (MessageReader* pMessageReader, MemoryStream *pPacket)
176172{
177- #ifndef KBENGINE_NO_CRYPTO
178173 if (!isGood_)
179174 {
180175 ERROR_MSG (" BlowfishFilter::recv: Dropping packet due to invalid filter" );
@@ -185,7 +180,7 @@ bool BlowfishFilter::recv(MessageReader* pMessageReader, MemoryStream *pPacket)
185180 uint32 len = pPacket->length ();
186181 uint16 packeLen = pPacket->readUint16 ();
187182
188- if ( 0 == pPacket_->length () && len > MIN_PACKET_SIZE && packeLen - 1 == len - 3 )
183+ if (0 == pPacket_->length () && len > MIN_PACKET_SIZE && packeLen - 1 == len - 3 )
189184 {
190185 int padSize = pPacket->readUint8 ();
191186 decrypt (pPacket);
@@ -267,12 +262,6 @@ bool BlowfishFilter::recv(MessageReader* pMessageReader, MemoryStream *pPacket)
267262 packetLen_ = 0 ;
268263 padSize_ = 0 ;
269264 }
270- #else
271- if (pMessageReader)
272- {
273- pMessageReader->process (pPacket->data () + pPacket->rpos (), 0 , pPacket->length ());
274- }
275- #endif
276-
277265 return true ;
278266}
267+
0 commit comments