123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- #include "epimafia.h"
- #include <QDir>
-
- #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()<<"("<<user.getLogin()<<user.getPassword()<<user.getPwd_num()<<user.getPwd_ppp()<<user.getPwd_socks()<<")";
- return dbg.maybeSpace();
- }
-
-
- void EpiUser::setLoginAes(QByteArray login)
- {
- m_login = login;
- }
-
- void EpiUser::setLogin(QString login)
- {
- m_login = Epimafia::aesEncrypt(login);
- }
-
- QString EpiUser::getLogin() const
- {
- return Epimafia::aesDecrypt(m_login);
- }
-
- QByteArray EpiUser::getLoginAes() const
- {
- return m_login;
- }
-
- bool EpiUser::hasLogin() const
- {
- return !m_login.isEmpty();
- }
-
- bool EpiUser::isUnknown() const
- {
- return getLogin().contains(QRegExp("^UnknownUser([0-9]+)$"));
- }
-
- void EpiUser::setPasswordAes(QByteArray password)
- {
- m_password = password;
- }
-
- void EpiUser::setPassword(QString password)
- {
- m_password = Epimafia::aesEncrypt(password);
- }
-
- QString EpiUser::getPassword() const
- {
- return Epimafia::aesDecrypt(m_password);
- }
-
- QByteArray EpiUser::getPasswordAes() const
- {
- return m_password;
- }
-
- bool EpiUser::hasPassword() const
- {
- return !m_password.isEmpty();
- }
-
- void EpiUser::setPwd_numAes(QByteArray pwd_num)
- {
- m_pwd_num = pwd_num;
- }
-
- void EpiUser::setPwd_num(QString pwd_num)
- {
- m_pwd_num = Epimafia::aesEncrypt(pwd_num);
- }
-
- QString EpiUser::getPwd_num() const
- {
- return Epimafia::aesDecrypt(m_pwd_num);
- }
-
- QByteArray EpiUser::getPwd_numAes() const
- {
- return m_pwd_num;
- }
-
- bool EpiUser::hasPwd_num() const
- {
- return !m_pwd_num.isEmpty();
- }
-
- void EpiUser::setPwd_pppAes(QByteArray pwd_ppp)
- {
- m_pwd_ppp = pwd_ppp;
- }
-
- void EpiUser::setPwd_ppp(QString pwd_ppp)
- {
- m_pwd_ppp = Epimafia::aesEncrypt(pwd_ppp);
- }
-
- QString EpiUser::getPwd_ppp() const
- {
- return Epimafia::aesDecrypt(m_pwd_ppp);
- }
-
- QByteArray EpiUser::getPwd_pppAes() const
- {
- return m_pwd_ppp;
- }
-
- bool EpiUser::hasPwd_ppp() const
- {
- return !m_pwd_ppp.isEmpty();
- }
-
- void EpiUser::setPwd_socksAes(QByteArray pwd_socks)
- {
- m_pwd_socks = pwd_socks;
- }
-
- void EpiUser::setPwd_socks(QString pwd_socks)
- {
- m_pwd_socks = Epimafia::aesEncrypt(pwd_socks);
- }
-
- QString EpiUser::getPwd_socks() const
- {
- return Epimafia::aesDecrypt(m_pwd_socks);
- }
-
- QByteArray EpiUser::getPwd_socksAes() const
- {
- return m_pwd_socks;
- }
-
- bool EpiUser::hasPwd_socks() const
- {
- return !m_pwd_socks.isEmpty();
- }
-
- QVariant EpiUser::toVariant() const
- {
- QVariant v;
- v.setValue(*this);
- return v;
- }
|