1
0
rc4/rc4_eq.hpp
2025-07-31 21:50:57 -05:00

47 lines
883 B
C++

//
// EQ2-Compatible RC4 Implementation
//
// It differs from standard RC4
// - Fixed 64-bit key (int64_t), treated as 8 bytes in little-endian order
// - Key scheduling cycles through the 8 key bytes using (i & 7)
//
#pragma once
#include <cstdint>
#include <cstddef>
#include <cstring>
class RC4 {
public:
void init(int64_t key) {
for (int i = 0; i < 256; ++i)
S[i] = static_cast<uint8_t>(i);
uint8_t* keyBytes = reinterpret_cast<uint8_t*>(&key);
uint8_t j = 0;
for (int i = 0; i < 256; ++i) {
j += S[i] + keyBytes[i & 7]; // cycle through 8 key bytes
std::swap(S[i], S[j]);
}
i = 0;
j = 0;
}
void process(uint8_t* data, size_t len) {
for (size_t n = 0; n < len; ++n) {
i += 1;
j += S[i];
std::swap(S[i], S[j]);
uint8_t k = S[(S[i] + S[j]) & 0xFF];
data[n] ^= k;
}
}
private:
uint8_t S[256];
uint8_t i = 0;
uint8_t j = 0;
};