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