📄 warcrypttext.h
字号:
/** */#ifndef WAR_CRYPT_TEXT_H#define WAR_CRYPT_TEXT_H#if WAR_CRYPTO/* SYSTEM INCLUDES */#if WAR_CRYPTO && !defined(WAR_OPENSSL_EVP_H_INCLUDED)# define WAR_OPENSSL_EVP_H_INCLUDED# include <openssl/evp.h>#endif/* PROJECT INCLUDES */#ifndef WAR_EXCEPTION_H# include "WarException.h"#endif#ifndef WAR_COLLECTOR_H# include "WarCollector.h"#endif/* LOCAL INCLUDES *//* FORWARD REFERENCES */#ifdef __cplusplusextern "C" {#endif/****************** BEGIN OLD STYLE C spesific ********//****************** END OLD STYLE C spesific **********/#ifdef __cplusplus }#endif/****************** BEGIN C++ spesific ****************/#ifdef __cplusplusclass WarCryptText {public: typedef WarCollector<char> passwd_t; typedef unsigned char * byteptr_t; typedef std::vector<unsigned char> buffer_t; // LIFECYCLE /** * Default constructor. */ WarCryptText(war_ccstr_t secretPasswd = NULL); /** * Copy constructor. * * @param from The value to copy to this object. */ WarCryptText(const WarCryptText& from); /** * Destructor. */ ~WarCryptText(); // OPERATORS /** * Assignment operator. * * @param from THe value to assign to this object. * * @return A reference to this object. */ WarCryptText& operator = (const WarCryptText& from); // OPERATIONS /** Set the password */ void SetPassword(war_ccstr_t newPasswd) { mPasswd = newPasswd; } /** High-level encryption, from a text string, to * a hex-string with a 4 char header that identifies * the encryption method (version) used. */ template <class charT> void Encrypt(std::basic_string<charT>& toHexStr, const std::basic_string<charT>& fromPlainStr) const throw(WarException) { size_t bytes_to_encrypt = fromPlainStr.size() * sizeof(TCHAR); buffer_t encr_buffer; EncryptBytes(encr_buffer, (byteptr_t)fromPlainStr.c_str(), bytes_to_encrypt); bytes_to_encrypt = encr_buffer.size(); vector<TCHAR> hex_bytes((bytes_to_encrypt * 2) + 5); charT *pp = (charT *)&hex_bytes[0]; // Header "0x01" *pp++ = '0'; *pp++ = 'x'; *pp++ = '0'; *pp++ = '1'; byteptr_t p = (byteptr_t)&encr_buffer[0]; for(size_t index = 0; index < bytes_to_encrypt; index++, p++) { static const unsigned char hexmap[] = {"0123456789abcdef"}; *pp++ = hexmap[(((*p & 0xf0) >> 4) & 0x0f)]; *pp++ = hexmap[(*p & 0x0f)]; } *pp = 0; toHexStr = (const charT *)&hex_bytes[0]; } /** High-level decryption, from a hex-string with a 4 char * header that identifies the encryption method (version) used * to a text string. * */ template <class charT> void Decrypt(std::basic_string<charT>& toPlainStr, const std::basic_string<charT>& fromHexStr) const throw(WarException) { toPlainStr.erase(); size_t num_chars = fromHexStr.size(); if (num_chars < 4) WarThrow(WarError(WAR_ERR_INVALID_ARGUMENT), NULL); num_chars -= 4; // skip header if (!num_chars) return; // Nothing to do // Hex digits must be aligned to two chars if (num_chars % 2) WarThrow(WarError(WAR_ERR_INVALID_ARGUMENT), NULL); size_t bytes_to_decrypt = (num_chars / 2); // Check header const charT *ph = fromHexStr.c_str(); if ((*ph++ != '0') || (*ph++ != 'x') || (*ph++ != '0') || (*ph++ != '1')) WarThrow(WarError(WAR_ERR_INVALID_ARGUMENT), NULL); vector<char> encrypted_bytes(bytes_to_decrypt); byteptr_t encr_value = (byteptr_t)&encrypted_bytes[0]; // Decode the hex string for(unsigned int index = 0 ; index < bytes_to_decrypt ; index++, encr_value++) { *encr_value = ((Atoh(*ph++) << 4) & 0xf0) | (Atoh(*ph++) & 0x0f); } encr_value = (byteptr_t)&encrypted_bytes[0]; buffer_t decr_buf; toPlainStr.resize(bytes_to_decrypt / sizeof(TCHAR)); byteptr_t plain_value = (byteptr_t)&toPlainStr[0]; DecryptBytes(decr_buf, encr_value, bytes_to_decrypt); decr_buf.resize(decr_buf.size() + sizeof(TCHAR)); charT *term_p = (charT *)&decr_buf[0]; term_p[(decr_buf.size() / sizeof(TCHAR)) - 1] = 0; toPlainStr = (const charT *)&decr_buf[0]; memset(&decr_buf[0], 0, decr_buf.size()); } /* Low-level encrypt function. Block cipher. */ void EncryptBytes(buffer_t& toBuf, const byteptr_t fromBuf, const size_t bufferLength) const throw(WarException); /* Low-level Decrypt function. Block cipher.*/ void DecryptBytes(buffer_t& toBuf, const byteptr_t fromBuf, const size_t bufferLength) const throw(WarException); // CALLBACK // ACCESS // INQUIRY protected:private: inline unsigned char Atoh(unsigned int val) const throw(WarException) { if ((val >= '0') && (val <= '9')) return val - '0'; if ((val >= 'a') && (val <= 'f')) return (val - 'a') + 10; if ((val >= 'A') && (val <= 'F')) return (val - 'A') + 10; WarThrow(WarError(WAR_ERR_INVALID_ARGUMENT), NULL); } passwd_t mPasswd;};/* INLINE METHODS *//* EXTERNAL REFERENCES */#endif /* __cplusplus *//****************** END C++ spesific ******************/#endif /* WAR_CRYPTO */#endif /* WAR_CRYPT_TEXT_H_ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -