#include "epimafia.h" #include #ifdef Q_OS_WIN QSettings Epimafia::m_settings("epimafia.ini", QSettings::IniFormat); #else #ifdef Q_OS_LINUX QSettings Epimafia::m_settings(QDir::homePath() + "/.epimafia.ini", QSettings::IniFormat); #else #error "OS specific" #endif #endif unsigned char Epimafia::m_aesKey[] = { 0x55, 0x15, 0x58, 0x5d, 0x65, 0xf5, 0xe9, 0x15, 0x65, 0x43, 0x85, 0xa9, 0xb9, 0x98, 0x9e, 0xc9, 0xd0, 0x0a, 0x93, 0x15, 0x98, 0xff, 0xfe, 0x42 }; EpiUsers Epimafia::loadUsers() { m_settings.sync(); EpiUsers users; m_settings.beginGroup("Users"); foreach(QString login, m_settings.childGroups()) { EpiUser u; m_settings.beginGroup(login); u.setLoginAes(QByteArray::fromHex(login.toStdString().c_str())); u.setPasswordAes(m_settings.value("password").toByteArray()); u.setPwd_numAes(m_settings.value("pwd_num").toByteArray()); u.setPwd_pppAes(m_settings.value("pwd_ppp").toByteArray()); u.setPwd_socksAes(m_settings.value("pwd_socks").toByteArray()); m_settings.endGroup(); users.append(u); } m_settings.endGroup(); return users; } void Epimafia::saveUser(EpiUser user) { saveUsers(EpiUsers() << user); } void Epimafia::saveUsers(EpiUsers users) { m_settings.sync(); m_settings.beginGroup("Users"); foreach(EpiUser user, users) { if(!user.hasLogin()) user.setLogin(getUnknownUser()); m_settings.beginGroup(QString(user.getLoginAes().toHex())); m_settings.setValue("password", user.getPasswordAes()); m_settings.setValue("pwd_num", user.getPwd_numAes()); m_settings.setValue("pwd_ppp", user.getPwd_pppAes()); m_settings.setValue("pwd_socks", user.getPwd_socksAes()); m_settings.endGroup(); } m_settings.endGroup(); } void Epimafia::removeUser(EpiUser user) { m_settings.sync(); m_settings.beginGroup("Users"); m_settings.remove(user.getLoginAes().toHex()); m_settings.endGroup(); } QByteArray Epimafia::aesEncrypt_16(QByteArray data) { unsigned char* enc_out = (unsigned char *)malloc(16*sizeof(char)); AES_KEY enc_key; AES_set_encrypt_key(m_aesKey, 128, &enc_key); AES_encrypt((const unsigned char*)data.constData(), enc_out, &enc_key); QByteArray aes((const char*)enc_out, 16); delete enc_out; return aes; } QByteArray Epimafia::aesDecrypt_16(QByteArray aes) { unsigned char* dec_out = (unsigned char *)malloc(16*sizeof(char)); memset(dec_out, 0, 16); AES_KEY dec_key; AES_set_decrypt_key(m_aesKey,128,&dec_key); AES_decrypt((const unsigned char*)aes.constData(), dec_out, &dec_key); int s = 16; for(int i = 15; i >= 0 && dec_out[i] == 0; --i) --s; QByteArray data((char *)dec_out, s); delete dec_out; return data; } QString Epimafia::getUnknownUser() { bool end = false; if(m_settings.group() != "Users") { end = true; m_settings.beginGroup("Users"); } QStringList users = m_settings.childGroups(); for(int i = 0; i < users.size(); ++i) users.replace(i, Epimafia::aesDecrypt(QByteArray::fromHex(users.at(i).toStdString().c_str()))); int i = 1; while(users.contains("UnknownUser" + QString::number(i))) ++i; if(end) m_settings.endGroup(); return "UnknownUser" + QString::number(i); } QByteArray Epimafia::aesEncrypt(QString str) { QByteArray aes; QByteArray data = str.toUtf8(); if(data.size() % 16 != 0) data += QByteArray(16 - (data.size() % 16), 0); for(int i = 0; i != data.size(); i += 16) aes += aesEncrypt_16(data.mid(i, 16)); return aes; } QString Epimafia::aesDecrypt(QByteArray aes) { QByteArray data; for(int i = 0; i < aes.size(); i += 16) data += aesDecrypt_16(aes.mid(i, i + 16)); return QString::fromUtf8(data.constData(), data.size()); } QDebug operator <<(QDebug dbg, const EpiUser & user) { dbg.space()<<"("<