Are param1 and param2 read in raw (little endian)? I'm not very familiar with PERL's packing method so I'm not sure how it processes the numbers. My guess is that it is read in little endian but is converted into big endian in initialize_message_id_encryption
Also, how is the message ID read? Is it converted to big endian or is it left as little endian?
Can anyone supply me with sample input/output generated by these methods so I can make sure I interpreted the code correctly (server encryption key, enc_val1, enc_val2, unencrypted packet, new enc_val1, and encrypted packet)? I can probably answer all of the questions by myself if I had sample input/output to play with.
Here's the related code to make it easier on you guys:
Code: Select all
'02AE' => ['initialize_message_id_encryption', 'V1 V1', [qw(param1 param2)]],
sub initialize_message_id_encryption {
my ($self, $args) = @_;
if ($masterServer->{messageIDEncryption} ne '0') {
$messageSender->sendMessageIDEncryptionInitialized();
my @c;
my $shtmp = $args->{param1};
for (my $i = 8; $i > 0; $i--) {
$c[$i] = $shtmp & 0x0F;
$shtmp >>= 4;
}
my $w = ($c[6]<<12) + ($c[4]<<8) + ($c[7]<<4) + $c[1];
$enc_val1 = ($c[2]<<12) + ($c[3]<<8) + ($c[5]<<4) + $c[8];
$enc_val2 = (((($enc_val1 ^ 0x0000F3AC) + $w) << 16) | (($enc_val1 ^ 0x000049DF) + $w)) ^ $args->{param2};
}
}
sub encryptMessageID {
use bytes;
my ($self, $r_message) = @_;
if ($self->{net}->getState() != Network::IN_GAME) {
$enc_val1 = 0;
$enc_val2 = 0;
return;
}
my $messageID = unpack("v", $$r_message);
if ($enc_val1 != 0 && $enc_val2 != 0) {
# Prepare encryption
$enc_val1 = (0x000343FD * $enc_val1) + $enc_val2;
$enc_val1 = $enc_val1 % 2 ** 32;
debug (sprintf("enc_val1 = %x", $enc_val1) . "\n", "sendPacket", 2);
# Encrypt message ID
$messageID = $messageID ^ (($enc_val1 >> 16) & 0x7FFF);
$messageID &= 0xFFFF;
$$r_message = pack("v", $messageID) . substr($$r_message, 2);
}
}