Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

CryptoBusiness.cpp 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // Created by robin on 2/26/17.
  3. //
  4. #include <openssl/aes.h>
  5. #include <cstring>
  6. #include "CryptoBusiness.h"
  7. std::string CryptoBusiness::_hex = "0123456789ABCDEF";
  8. CryptoBusiness::CryptoBusiness()
  9. {
  10. }
  11. std::string CryptoBusiness::getKey() const
  12. {
  13. return _key;
  14. }
  15. void CryptoBusiness::setKey(std::string key)
  16. {
  17. _key = key;
  18. }
  19. std::string CryptoBusiness::encrypt16(std::string data)
  20. {
  21. unsigned char enc_out[16 * sizeof(char)];
  22. AES_KEY enc_key;
  23. AES_set_encrypt_key((const unsigned char*)_key.c_str(), 128, &enc_key);
  24. AES_encrypt((const unsigned char*)data.c_str(), enc_out, &enc_key);
  25. return std::string((char*)enc_out, 16 * sizeof(char));
  26. }
  27. std::string CryptoBusiness::encrypt(std::string data)
  28. {
  29. std::string out;
  30. if (data.length() % 16 != 0)
  31. {
  32. data += std::string(16 - data.length() % 16, 0);
  33. }
  34. for (size_t i = 0; i < data.length(); i += 16)
  35. {
  36. out += encrypt16(data.substr(i, i + 16));
  37. }
  38. return out;
  39. }
  40. std::string CryptoBusiness::decrypt16(std::string data)
  41. {
  42. unsigned char dec_out[16 * sizeof(char)];
  43. memset(dec_out, 0, 16);
  44. AES_KEY dec_key;
  45. AES_set_decrypt_key((const unsigned char*)_key.c_str(), 128, &dec_key);
  46. AES_decrypt((const unsigned char*)data.c_str(), dec_out, &dec_key);
  47. int s = 16;
  48. for(int i = 15; i >= 0 && dec_out[i] == 0; --i)
  49. --s;
  50. return std::string((char*)dec_out, s);
  51. }
  52. std::string CryptoBusiness::decrypt(std::string data)
  53. {
  54. std::string out;
  55. for (size_t i = 0; i < data.length(); i += 16)
  56. {
  57. out += decrypt16(data.substr(i, i + 16));
  58. }
  59. return out;
  60. }
  61. std::string CryptoBusiness::toHex(std::string data)
  62. {
  63. size_t len = data.length();
  64. std::string output;
  65. output.reserve(2 * len);
  66. for (size_t i = 0; i < len; ++i)
  67. {
  68. const unsigned char c = data[i];
  69. output.push_back(_hex[c >> 4]);
  70. output.push_back(_hex[c & 15]);
  71. }
  72. return output;
  73. }
  74. std::string CryptoBusiness::fromHex(std::string data)
  75. {
  76. size_t len = data.length();
  77. std::string output;
  78. output.reserve(len / 2);
  79. for (size_t i = 0; i < len; i += 2)
  80. {
  81. char a = data[i];
  82. const char* p = std::lower_bound(_hex.c_str(), _hex.c_str() + 16, a);
  83. char b = data[i + 1];
  84. const char* q = std::lower_bound(_hex.c_str(), _hex.c_str() + 16, b);
  85. output.push_back(((p - _hex.c_str()) << 4) | (q - _hex.c_str()));
  86. }
  87. return output;
  88. }
  89. std::string CryptoBusiness::encryptToHex(std::string data)
  90. {
  91. auto encrypted = encrypt(data);
  92. return toHex(encrypted);
  93. }
  94. std::string CryptoBusiness::decryptFromHex(std::string data)
  95. {
  96. auto d = fromHex(data);
  97. return decrypt(d);
  98. }