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.

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