123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- #include <kopano/platform.h>
- #include <string>
- #include <kopano/stringutil.h>
- #include <kopano/charset/convert.h>
- #include <cassert>
- #include "SymmetricCrypt.h"
- namespace KC {
- bool SymmetricIsCrypted(const char *c)
- {
- return strncmp(c, "{1}:", 4) == 0 || strncmp(c, "{2}:", 4) == 0;
- }
- bool SymmetricIsCrypted(const wchar_t *c)
- {
- return wcsncmp(c, L"{1}:", 4) == 0 || wcsncmp(c, L"{2}:", 4) == 0;
- }
- static std::string SymmetricDecryptBlob(unsigned int ulAlg, const std::string &strXORed)
- {
- std::string strRaw = strXORed;
- size_t z = strRaw.size();
-
- assert(ulAlg == 1 || ulAlg == 2);
- for (unsigned int i = 0; i < z; ++i)
- strRaw[i] ^= 0xA5;
-
-
-
- if (ulAlg == 1)
- strRaw = convert_to<std::string>("UTF-8", strRaw, rawsize(strRaw), "WINDOWS-1252");
-
- return strRaw;
- }
- std::string SymmetricDecrypt(const char *strCrypted)
- {
- if (!SymmetricIsCrypted(strCrypted))
- return "";
-
- return SymmetricDecryptBlob(strCrypted[1] - '0',
- base64_decode(convert_to<std::string>(strCrypted + 4)));
- }
- std::string SymmetricDecrypt(const wchar_t *wstrCrypted)
- {
- if (!SymmetricIsCrypted(wstrCrypted))
- return "";
-
- return SymmetricDecryptBlob(wstrCrypted[1] - '0',
- base64_decode(convert_to<std::string>(wstrCrypted + 4)));
- }
- }
|