Improve encrypt/decrypt algorithms
This commit is contained in:
parent
f492d291eb
commit
4751941e4f
|
@ -89,9 +89,7 @@ function init()
|
||||||
|
|
||||||
-- generate machine uuid, this is a security measure for storing passwords
|
-- generate machine uuid, this is a security measure for storing passwords
|
||||||
if not g_crypt.setMachineUUID(g_configs.get('uuid')) then
|
if not g_crypt.setMachineUUID(g_configs.get('uuid')) then
|
||||||
local uuid = g_crypt.genUUID()
|
g_configs.set('uuid', g_crypt.getMachineUUID())
|
||||||
g_crypt.setMachineUUID(uuid)
|
|
||||||
g_configs.set('uuid', uuid)
|
|
||||||
g_configs.save()
|
g_configs.save()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -553,7 +553,7 @@ function processMouseAction(menuPosition, mouseButton, autoWalkPos, lookThing, u
|
||||||
player:stopAutoWalk()
|
player:stopAutoWalk()
|
||||||
|
|
||||||
if autoWalkPos and keyboardModifiers == KeyboardNoModifier and mouseButton == MouseLeftButton then
|
if autoWalkPos and keyboardModifiers == KeyboardNoModifier and mouseButton == MouseLeftButton then
|
||||||
player.onAutoWalkFail = function() modules.game_textmessage.displayStatusMessage(tr('There is no way.')) end
|
player.onAutoWalkFail = function() modules.game_textmessage.displayFailureMessage(tr('There is no way.')) end
|
||||||
player:autoWalk(autoWalkPos)
|
player:autoWalk(autoWalkPos)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
|
@ -387,7 +387,7 @@ end
|
||||||
function minimapAutoWalk(pos)
|
function minimapAutoWalk(pos)
|
||||||
local player = g_game.getLocalPlayer()
|
local player = g_game.getLocalPlayer()
|
||||||
if not player:autoWalk(pos) then
|
if not player:autoWalk(pos) then
|
||||||
modules.game_textmessage.displayStatusMessage(tr('There is no way.'))
|
player.onAutoWalkFail = function() modules.game_textmessage.displayFailureMessage(tr('There is no way.')) end
|
||||||
return false
|
return false
|
||||||
else
|
else
|
||||||
return true
|
return true
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
#include <framework/core/logger.h>
|
#include <framework/core/logger.h>
|
||||||
#include <framework/core/resourcemanager.h>
|
#include <framework/core/resourcemanager.h>
|
||||||
#include <framework/platform/platform.h>
|
#include <framework/platform/platform.h>
|
||||||
|
#include <framework/core/application.h>
|
||||||
|
|
||||||
#include <boost/uuid/uuid_generators.hpp>
|
#include <boost/uuid/uuid_generators.hpp>
|
||||||
#include <boost/uuid/uuid_io.hpp>
|
#include <boost/uuid/uuid_io.hpp>
|
||||||
|
@ -158,46 +159,57 @@ std::string Crypt::genUUID()
|
||||||
return boost::uuids::to_string(u);
|
return boost::uuids::to_string(u);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Crypt::setMachineUUID(const std::string& uuidstr)
|
bool Crypt::setMachineUUID(std::string uuidstr)
|
||||||
{
|
{
|
||||||
if(uuidstr.empty())
|
if(uuidstr.empty())
|
||||||
return false;
|
return false;
|
||||||
std::stringstream ss;
|
uuidstr = _decrypt(uuidstr, false);
|
||||||
ss << uuidstr;
|
if(uuidstr.length() != 16)
|
||||||
ss >> m_machineUUID;
|
return false;
|
||||||
return !m_machineUUID.is_nil();
|
std::copy(uuidstr.begin(), uuidstr.end(), m_machineUUID.begin());
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Crypt::getMachineUUID()
|
std::string Crypt::getMachineUUID()
|
||||||
{
|
{
|
||||||
return boost::uuids::to_string(m_machineUUID);
|
if(m_machineUUID.is_nil()) {
|
||||||
|
boost::uuids::random_generator gen;
|
||||||
|
m_machineUUID = gen();
|
||||||
|
}
|
||||||
|
return _encrypt(std::string(m_machineUUID.begin(), m_machineUUID.end()), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Crypt::getMachineKey()
|
std::string Crypt::getCryptKey(bool useMachineUUID)
|
||||||
{
|
{
|
||||||
boost::hash<boost::uuids::uuid> uuid_hasher;
|
boost::hash<boost::uuids::uuid> uuid_hasher;
|
||||||
|
boost::uuids::uuid uuid;
|
||||||
boost::uuids::name_generator gen(m_machineUUID);
|
if(useMachineUUID) {
|
||||||
boost::uuids::uuid u = gen(g_platform.getCPUName() + g_platform.getOSName() + g_resources.getUserDir());
|
uuid = m_machineUUID;
|
||||||
|
} else {
|
||||||
|
boost::uuids::nil_generator nilgen;
|
||||||
|
uuid = nilgen();
|
||||||
|
}
|
||||||
|
boost::uuids::name_generator namegen(uuid);
|
||||||
|
boost::uuids::uuid u = namegen(g_app.getCompactName() + g_platform.getCPUName() + g_platform.getOSName() + g_resources.getUserDir());
|
||||||
std::size_t hash = uuid_hasher(u);
|
std::size_t hash = uuid_hasher(u);
|
||||||
std::string key;
|
std::string key;
|
||||||
key.assign((const char *)&hash, sizeof(hash));
|
key.assign((const char *)&hash, sizeof(hash));
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Crypt::encrypt(const std::string& decrypted_string)
|
std::string Crypt::_encrypt(const std::string& decrypted_string, bool useMachineUUID)
|
||||||
{
|
{
|
||||||
std::string tmp = "0000" + decrypted_string;
|
std::string tmp = "0000" + decrypted_string;
|
||||||
uint32 sum = stdext::adler32((const uint8*)decrypted_string.c_str(), decrypted_string.size());
|
uint32 sum = stdext::adler32((const uint8*)decrypted_string.c_str(), decrypted_string.size());
|
||||||
stdext::writeLE32((uint8*)&tmp[0], sum);
|
stdext::writeLE32((uint8*)&tmp[0], sum);
|
||||||
std::string encrypted = base64Encode(xorCrypt(tmp, getMachineKey()));
|
std::string encrypted = base64Encode(xorCrypt(tmp, getCryptKey(useMachineUUID)));
|
||||||
return encrypted;
|
return encrypted;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Crypt::decrypt(const std::string& encrypted_string)
|
std::string Crypt::_decrypt(const std::string& encrypted_string, bool useMachineUUID)
|
||||||
{
|
{
|
||||||
std::string decoded = base64Decode(encrypted_string);
|
std::string decoded = base64Decode(encrypted_string);
|
||||||
std::string tmp = xorCrypt(base64Decode(encrypted_string), getMachineKey());
|
std::string tmp = xorCrypt(base64Decode(encrypted_string), getCryptKey(useMachineUUID));
|
||||||
if(tmp.length() >= 4) {
|
if(tmp.length() >= 4) {
|
||||||
uint32 readsum = stdext::readLE32((const uint8*)tmp.c_str());
|
uint32 readsum = stdext::readLE32((const uint8*)tmp.c_str());
|
||||||
std::string decrypted_string = tmp.substr(4);
|
std::string decrypted_string = tmp.substr(4);
|
||||||
|
|
|
@ -39,11 +39,11 @@ public:
|
||||||
std::string base64Encode(const std::string& decoded_string);
|
std::string base64Encode(const std::string& decoded_string);
|
||||||
std::string base64Decode(const std::string& encoded_string);
|
std::string base64Decode(const std::string& encoded_string);
|
||||||
std::string xorCrypt(const std::string& buffer, const std::string& key);
|
std::string xorCrypt(const std::string& buffer, const std::string& key);
|
||||||
|
std::string encrypt(const std::string& decrypted_string) { return _encrypt(decrypted_string, true); }
|
||||||
|
std::string decrypt(const std::string& encrypted_string) { return _decrypt(encrypted_string, true); }
|
||||||
std::string genUUID();
|
std::string genUUID();
|
||||||
bool setMachineUUID(const std::string& uuidstr);
|
bool setMachineUUID(std::string uuidstr);
|
||||||
std::string getMachineUUID();
|
std::string getMachineUUID();
|
||||||
std::string encrypt(const std::string& decrypted_string);
|
|
||||||
std::string decrypt(const std::string& encrypted_string);
|
|
||||||
std::string md5Encode(const std::string& decoded_string, bool upperCase);
|
std::string md5Encode(const std::string& decoded_string, bool upperCase);
|
||||||
std::string sha1Encode(const std::string& decoded_string, bool upperCase);
|
std::string sha1Encode(const std::string& decoded_string, bool upperCase);
|
||||||
std::string sha256Encode(const std::string& decoded_string, bool upperCase);
|
std::string sha256Encode(const std::string& decoded_string, bool upperCase);
|
||||||
|
@ -58,7 +58,9 @@ public:
|
||||||
int rsaGetSize();
|
int rsaGetSize();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string getMachineKey();
|
std::string _encrypt(const std::string& decrypted_string, bool useMachineUUID);
|
||||||
|
std::string _decrypt(const std::string& encrypted_string, bool useMachineUUID);
|
||||||
|
std::string getCryptKey(bool useMachineUUID);
|
||||||
boost::uuids::uuid m_machineUUID;
|
boost::uuids::uuid m_machineUUID;
|
||||||
RSA *m_rsa;
|
RSA *m_rsa;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue