diff --git a/String.cpp b/String.cpp index 9b25cd77..a1388027 100644 --- a/String.cpp +++ b/String.cpp @@ -473,6 +473,16 @@ CString CString::Format(const CString& sFormatStr, ...) { return ""; } +bool CString::Base64Encode(unsigned int uWrap) { + CString sCopy(*this); + return sCopy.Base64Encode(*this, uWrap); +} + +unsigned long CString::Base64Decode() { + CString sCopy(*this); + return sCopy.Base64Decode(*this); +} + CString CString::Base64Encode_n(unsigned int uWrap) const { CString sRet; Base64Encode(sRet, uWrap); @@ -611,6 +621,51 @@ CString CString::MD5() const { return (const char*) CMD5(*this); } +CString CString::Encrypt_n(const CString& sPass, const CString& sIvec) { + CString sRet; + sRet.Encrypt(sPass, sIvec); + return sRet; +} + +CString CString::Decrypt_n(const CString& sPass, const CString& sIvec) { + CString sRet; + sRet.Decrypt(sPass, sIvec); + return sRet; +} + +void CString::Encrypt(const CString& sPass, const CString& sIvec) { + Crypt(sPass, true, sIvec); +} + +void CString::Decrypt(const CString& sPass, const CString& sIvec) { + Crypt(sPass, false, sIvec); +} + +void CString::Crypt(const CString& sPass, bool bEncrypt, const CString& sIvec) { + unsigned char szIvec[8] = {0,0,0,0,0,0,0,0}; + BF_KEY bKey; + + if (sIvec.length() >= 8) { + memcpy(szIvec, sIvec.data(), 8); + } + + BF_set_key(&bKey, sPass.length(), (unsigned char*) sPass.data()); + unsigned int uPad = (length() % 8); + + if (uPad) { + uPad = 8 - uPad; + append(uPad, '\0'); + } + + size_t uLen = length(); + unsigned char* szBuff = (unsigned char*) malloc(uLen); + BF_cbc_encrypt((const unsigned char*) data(), szBuff, uLen, &bKey, szIvec, ((bEncrypt) ? BF_ENCRYPT : BF_DECRYPT)); + + clear(); + append((const char*) szBuff, uLen); + free(szBuff); +} + CString CString::ToString(char c) { stringstream s; s << c; return s.str(); } CString CString::ToString(unsigned char c) { stringstream s; s << c; return s.str(); } CString CString::ToString(short i) { stringstream s; s << i; return s.str(); } diff --git a/String.h b/String.h index ecada522..56409025 100644 --- a/String.h +++ b/String.h @@ -74,13 +74,21 @@ public: CString Token(unsigned int uPos, bool bRest = false, const CString& sSep = " ") const; VCString Split(const CString& sDelim, bool bKeepEmpty = true) const; unsigned int Split(const CString& sDelim, VCString& vsRet, bool bAllowEmpty = true) const; - static CString Format(const CString& sFormatStr, ...); - CString Base64Decode_n() const; - CString Base64Encode_n(unsigned int uWrap = 0) const; - unsigned long Base64Decode(CString& sRet) const; - bool Base64Encode(CString& sRet, unsigned int uWrap = 0) const; + CString MD5() const; + unsigned long Base64Decode(CString& sRet) const; + unsigned long Base64Decode(); + CString Base64Decode_n() const; + bool Base64Encode(CString& sRet, unsigned int uWrap = 0) const; + bool Base64Encode(unsigned int uWrap = 0); + CString Base64Encode_n(unsigned int uWrap = 0) const; + + CString Encrypt_n(const CString& sPass, const CString& sIvec = ""); + CString Decrypt_n(const CString& sPass, const CString& sIvec = ""); + void Encrypt(const CString& sPass, const CString& sIvec = ""); + void Decrypt(const CString& sPass, const CString& sIvec = ""); + void Crypt(const CString& sPass, bool bEncrypt, const CString& sIvec = ""); static CString ToString(char c); static CString ToString(unsigned char c);