/* */ #include "InternalARC4Encryptor.h" namespace aria2 { void ARC4Encryptor::init(const unsigned char* key, size_t keyLength) { j = 0; for (auto& c : state_) c = j++; j = 0; for (i = 0; i < sizeof(state_); ++i) { j = (j + state_[i] + key[i % keyLength]) & 0xff; auto tmp = state_[i]; state_[i] = state_[j]; state_[j] = tmp; } i = j = 0; } void ARC4Encryptor::encrypt(size_t len, unsigned char* out, const unsigned char* in) { for (auto c = 0u; c < len; ++c) { i = (i + 1) & 0xff; j = (j + state_[i]) & 0xff; auto sj = state_[i]; auto si = state_[i] = state_[j]; state_[j] = sj; out[c] = in[c] ^ state_[(si + sj) & 0xff]; } } } // namespace aria2