Emu/source/common/crypto/crypto.cpp
2025-09-06 18:48:40 -05:00

54 lines
1.0 KiB
C++

// Copyright (C) 2007-2025 EQ2EMulator
// Licensed under GPL v3
#include "crypto.h"
#include <algorithm>
#include <array>
#include <cstring>
#include <mutex>
int64 Crypto::RSADecrypt(uchar* text, int16 size) noexcept
{
int64 ret = 0;
std::array<uchar, 8> buffer{};
for (int i = 7; i >= 0; --i) {
buffer[7 - i] = text[i];
}
std::memcpy(&ret, buffer.data(), 8);
return ret;
}
void Crypto::RC4Decrypt(uchar* text, int32 size) noexcept
{
const std::lock_guard<std::mutex> lock(m_crypto_mutex);
client->Cypher(text, size);
}
void Crypto::RC4Encrypt(uchar* text, int32 size) noexcept
{
const std::lock_guard<std::mutex> lock(m_crypto_mutex);
server->Cypher(text, size);
}
void Crypto::setRC4Key(int64 key) noexcept
{
rc4_key = key;
if (key > 0) {
encrypted = true;
client = std::make_unique<RC4>(~key);
server = std::make_unique<RC4>(key);
std::array<uchar, 20> temp{};
client->Cypher(temp.data(), 20);
server->Cypher(temp.data(), 20);
} else {
encrypted = false;
client.reset();
server.reset();
}
}