RabbitCommon v2.2.6
Loading...
Searching...
No Matches
RabbitCommonEncrypt.cpp
1// Author Kang Lin <kl222@126.com>
2
3#include "RabbitCommonEncrypt.h"
4#include "Log/Log.h"
5#include <string.h>
6
7#if defined (HAVE_OPENSSL)
8#include "EvpAES.h"
9#endif
10
11namespace RabbitCommon {
12
13static Q_LOGGING_CATEGORY(log, "RabbitCommon.Encrypt")
14
15#define PASSWORD_LENGTH 16
16
17static const unsigned char Key[] =
18{
19 0x56, 0x9a, 0x28, 0x12, 0xfa, 0x59, 0xa1, 0x58,
20 0x65, 0x7c, 0xc8, 0x92, 0x1a, 0x09, 0x10, 0x8a
21};
22
23CEncrypt::CEncrypt(const char* pszPassword)
24{
25 SetPassword(pszPassword);
26}
27
28CEncrypt::~CEncrypt()
29{
30}
31
32int CEncrypt::SetPassword(const char* pszPassword)
33{
34 size_t nLen = PASSWORD_LENGTH;
35 if(nLen > strlen(pszPassword))
36 nLen = strlen(pszPassword);
37
38 m_szPassword.resize(PASSWORD_LENGTH, 0);
39 memcpy(&m_szPassword[0], Key, PASSWORD_LENGTH);
40 for(int i = 0; i < nLen; i++)
41 {
42 m_szPassword[i] ^= pszPassword[i];
43 }
44
45 return 0;
46}
47
48int CEncrypt::Encode(const char* pIn, const int& inLen, char** pOut, int& outLen)
49{
50 int nRet = 0;
51 QString szIn = QString::fromStdString(std::string(pIn, inLen));
52 QByteArray szOut;
53 nRet = Encode(szIn, szOut);
54 if(nRet) return nRet;
55 outLen = szOut.length();
56 *pOut = new char[outLen];
57 memcpy(*pOut, szOut.data(), outLen);
58 return nRet;
59}
60
61int CEncrypt::Encode(const QString& szIn, QByteArray& szOut)
62{
63 int nRet = 0;
64#if defined (HAVE_OPENSSL)
65 EvpAES aes;
66 QByteArray in = szIn.toUtf8();
67 bool b = aes.ecb_encrypt(in, szOut,
68 QByteArray::fromStdString(m_szPassword));
69
70// bool b = aes.cbc_encrypt(in, szOut,
71// QByteArray::fromStdString(m_szPassword),
72// QByteArray::fromRawData((const char*)Key, PASSWORD_LENGTH));
73 if(b) return 0;
74 else return -1;
75#else
76 szOut = szIn.toUtf8();
77#endif
78 return nRet;
79}
80
81int CEncrypt::Dencode(const QByteArray &szIn, QString &szOut)
82{
83 int nRet = 0;
84#if defined (HAVE_OPENSSL)
85 EvpAES aes;
86 QByteArray out;
87 bool b = aes.ecb_encrypt(szIn, out,
88 QByteArray::fromStdString(m_szPassword),
89 false);
90// bool b = aes.cbc_encrypt(szIn, out,
91// QByteArray::fromStdString(m_szPassword),
92// QByteArray::fromRawData((const char*)Key, PASSWORD_LENGTH),
93// false);
94 if(b)
95 {
96 szOut = out;
97 return 0;
98 } else
99 return -1;
100#else
101 szOut = szIn;
102#endif
103 return nRet;
104}
105
106int CEncrypt::Dencode(const char* pIn, const int& inLen, char** pOut, int& outLen)
107{
108 int nRet = 0;
109 QByteArray in(pIn, inLen);
110 QString out;
111 nRet = Dencode(in, out);
112 if(nRet) return nRet;
113 outLen = out.length();
114 *pOut = new char[outLen];
115 memcpy(*pOut, out.data(), outLen);
116 return nRet;
117}
118
119int CEncrypt::Dencode(const char* pIn, const int& inLen, std::string& szOut)
120{
121 char *pOut = NULL;
122 int nOutLen = 0;
123 int nRet = 0;
124 nRet = Dencode(pIn, inLen, &pOut, nOutLen);
125 if(nRet)
126 {
127 qCritical(log) << "Dencode error";
128 return nRet;
129 }
130
131 if(pOut)
132 {
133 szOut = pOut;
134 delete [] pOut;
135 }
136 return nRet;
137}
138
139} // namespace RabbitCommon
140
The EvpAES class.
Definition EvpAES.h:21
int Encode(const char *pIn, const int &inLen, char **pOut, int &outLen)
int Dencode(const char *pIn, const int &inLen, char **pOut, int &outLen)