Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

epimafia.cpp 6.2KB

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