You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

epimafia.cpp 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #include "epimafia.h"
  2. #include <QDir>
  3. #ifdef Q_OS_WIN
  4. QSettings Epimafia::m_settings("epimafia.ini", QSettings::IniFormat);
  5. #else
  6. #ifdef Q_OS_LINUX
  7. QSettings Epimafia::m_settings(QDir::homePath() + "/.epimafia.ini", QSettings::IniFormat);
  8. #else
  9. #error "OS specific"
  10. #endif
  11. #endif
  12. unsigned char Epimafia::m_aesKey[] =
  13. {
  14. 0x55, 0x15, 0x58, 0x5d, 0x65, 0xf5, 0xe9, 0x15,
  15. 0x65, 0x43, 0x85, 0xa9, 0xb9, 0x98, 0x9e, 0xc9,
  16. 0xd0, 0x0a, 0x93, 0x15, 0x98, 0xff, 0xfe, 0x42
  17. };
  18. EpiUsers Epimafia::loadUsers()
  19. {
  20. m_settings.sync();
  21. EpiUsers users;
  22. m_settings.beginGroup("Users");
  23. foreach(QString login, m_settings.childGroups())
  24. {
  25. EpiUser u;
  26. m_settings.beginGroup(login);
  27. u.setLoginAes(QByteArray::fromHex(login.toStdString().c_str()));
  28. u.setPasswordAes(m_settings.value("password").toByteArray());
  29. u.setPwd_numAes(m_settings.value("pwd_num").toByteArray());
  30. u.setPwd_pppAes(m_settings.value("pwd_ppp").toByteArray());
  31. u.setPwd_socksAes(m_settings.value("pwd_socks").toByteArray());
  32. m_settings.endGroup();
  33. users.append(u);
  34. }
  35. m_settings.endGroup();
  36. return users;
  37. }
  38. void Epimafia::saveUser(EpiUser user)
  39. {
  40. saveUsers(EpiUsers() << user);
  41. }
  42. void Epimafia::saveUsers(EpiUsers users)
  43. {
  44. m_settings.sync();
  45. m_settings.beginGroup("Users");
  46. foreach(EpiUser user, users)
  47. {
  48. if(!user.hasLogin())
  49. user.setLogin(getUnknownUser());
  50. m_settings.beginGroup(QString(user.getLoginAes().toHex()));
  51. m_settings.setValue("password", user.getPasswordAes());
  52. m_settings.setValue("pwd_num", user.getPwd_numAes());
  53. m_settings.setValue("pwd_ppp", user.getPwd_pppAes());
  54. m_settings.setValue("pwd_socks", user.getPwd_socksAes());
  55. m_settings.endGroup();
  56. }
  57. m_settings.endGroup();
  58. }
  59. void Epimafia::removeUser(EpiUser user)
  60. {
  61. m_settings.sync();
  62. m_settings.beginGroup("Users");
  63. m_settings.remove(user.getLoginAes().toHex());
  64. m_settings.endGroup();
  65. }
  66. QByteArray Epimafia::aesEncrypt_16(QByteArray data)
  67. {
  68. unsigned char* enc_out = (unsigned char *)malloc(16*sizeof(char));
  69. AES_KEY enc_key;
  70. AES_set_encrypt_key(m_aesKey, 128, &enc_key);
  71. AES_encrypt((const unsigned char*)data.constData(), enc_out, &enc_key);
  72. QByteArray aes((const char*)enc_out, 16);
  73. delete enc_out;
  74. return aes;
  75. }
  76. QByteArray Epimafia::aesDecrypt_16(QByteArray aes)
  77. {
  78. unsigned char* dec_out = (unsigned char *)malloc(16*sizeof(char));
  79. memset(dec_out, 0, 16);
  80. AES_KEY dec_key;
  81. AES_set_decrypt_key(m_aesKey,128,&dec_key);
  82. AES_decrypt((const unsigned char*)aes.constData(), dec_out, &dec_key);
  83. int s = 16;
  84. for(int i = 15; i >= 0 && dec_out[i] == 0; --i)
  85. --s;
  86. QByteArray data((char *)dec_out, s);
  87. delete dec_out;
  88. return data;
  89. }
  90. QString Epimafia::getUnknownUser()
  91. {
  92. bool end = false;
  93. if(m_settings.group() != "Users")
  94. {
  95. end = true;
  96. m_settings.beginGroup("Users");
  97. }
  98. QStringList users = m_settings.childGroups();
  99. for(int i = 0; i < users.size(); ++i)
  100. users.replace(i, Epimafia::aesDecrypt(QByteArray::fromHex(users.at(i).toStdString().c_str())));
  101. int i = 1;
  102. while(users.contains("UnknownUser" + QString::number(i)))
  103. ++i;
  104. if(end)
  105. m_settings.endGroup();
  106. return "UnknownUser" + QString::number(i);
  107. }
  108. QByteArray Epimafia::aesEncrypt(QString str)
  109. {
  110. QByteArray aes;
  111. QByteArray data = str.toUtf8();
  112. if(data.size() % 16 != 0)
  113. data += QByteArray(16 - (data.size() % 16), 0);
  114. for(int i = 0; i != data.size(); i += 16)
  115. aes += aesEncrypt_16(data.mid(i, 16));
  116. return aes;
  117. }
  118. QString Epimafia::aesDecrypt(QByteArray aes)
  119. {
  120. QByteArray data;
  121. for(int i = 0; i < aes.size(); i += 16)
  122. data += aesDecrypt_16(aes.mid(i, i + 16));
  123. return QString::fromUtf8(data.constData(), data.size());
  124. }
  125. QDebug operator <<(QDebug dbg, const EpiUser & user)
  126. {
  127. dbg.space()<<"("<<user.getLogin()<<user.getPassword()<<user.getPwd_num()<<user.getPwd_ppp()<<user.getPwd_socks()<<")";
  128. return dbg.maybeSpace();
  129. }
  130. void EpiUser::setLoginAes(QByteArray login)
  131. {
  132. m_login = login;
  133. }
  134. void EpiUser::setLogin(QString login)
  135. {
  136. m_login = Epimafia::aesEncrypt(login);
  137. }
  138. QString EpiUser::getLogin() const
  139. {
  140. return Epimafia::aesDecrypt(m_login);
  141. }
  142. QByteArray EpiUser::getLoginAes() const
  143. {
  144. return m_login;
  145. }
  146. bool EpiUser::hasLogin() const
  147. {
  148. return !m_login.isEmpty();
  149. }
  150. bool EpiUser::isUnknown() const
  151. {
  152. return getLogin().contains(QRegExp("^UnknownUser([0-9]+)$"));
  153. }
  154. void EpiUser::setPasswordAes(QByteArray password)
  155. {
  156. m_password = password;
  157. }
  158. void EpiUser::setPassword(QString password)
  159. {
  160. m_password = Epimafia::aesEncrypt(password);
  161. }
  162. QString EpiUser::getPassword() const
  163. {
  164. return Epimafia::aesDecrypt(m_password);
  165. }
  166. QByteArray EpiUser::getPasswordAes() const
  167. {
  168. return m_password;
  169. }
  170. bool EpiUser::hasPassword() const
  171. {
  172. return !m_password.isEmpty();
  173. }
  174. void EpiUser::setPwd_numAes(QByteArray pwd_num)
  175. {
  176. m_pwd_num = pwd_num;
  177. }
  178. void EpiUser::setPwd_num(QString pwd_num)
  179. {
  180. m_pwd_num = Epimafia::aesEncrypt(pwd_num);
  181. }
  182. QString EpiUser::getPwd_num() const
  183. {
  184. return Epimafia::aesDecrypt(m_pwd_num);
  185. }
  186. QByteArray EpiUser::getPwd_numAes() const
  187. {
  188. return m_pwd_num;
  189. }
  190. bool EpiUser::hasPwd_num() const
  191. {
  192. return !m_pwd_num.isEmpty();
  193. }
  194. void EpiUser::setPwd_pppAes(QByteArray pwd_ppp)
  195. {
  196. m_pwd_ppp = pwd_ppp;
  197. }
  198. void EpiUser::setPwd_ppp(QString pwd_ppp)
  199. {
  200. m_pwd_ppp = Epimafia::aesEncrypt(pwd_ppp);
  201. }
  202. QString EpiUser::getPwd_ppp() const
  203. {
  204. return Epimafia::aesDecrypt(m_pwd_ppp);
  205. }
  206. QByteArray EpiUser::getPwd_pppAes() const
  207. {
  208. return m_pwd_ppp;
  209. }
  210. bool EpiUser::hasPwd_ppp() const
  211. {
  212. return !m_pwd_ppp.isEmpty();
  213. }
  214. void EpiUser::setPwd_socksAes(QByteArray pwd_socks)
  215. {
  216. m_pwd_socks = pwd_socks;
  217. }
  218. void EpiUser::setPwd_socks(QString pwd_socks)
  219. {
  220. m_pwd_socks = Epimafia::aesEncrypt(pwd_socks);
  221. }
  222. QString EpiUser::getPwd_socks() const
  223. {
  224. return Epimafia::aesDecrypt(m_pwd_socks);
  225. }
  226. QByteArray EpiUser::getPwd_socksAes() const
  227. {
  228. return m_pwd_socks;
  229. }
  230. bool EpiUser::hasPwd_socks() const
  231. {
  232. return !m_pwd_socks.isEmpty();
  233. }
  234. QVariant EpiUser::toVariant() const
  235. {
  236. QVariant v;
  237. v.setValue(*this);
  238. return v;
  239. }