添加aes加密方法,base64加密算法

master
a7458969 2020-01-05 01:28:43 +08:00
parent 493835ed0a
commit 8ab9824936
22 changed files with 1506 additions and 93 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.idea/

View File

@ -11,7 +11,25 @@
</configurations>
</component>
<component name="ChangeListManager">
<list default="true" id="0facce0d-c642-4d80-b2fb-daf5f3e68dff" name="Default Changelist" comment="" />
<list default="true" id="0facce0d-c642-4d80-b2fb-daf5f3e68dff" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/src/encrypt/aes.cpp" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/encrypt/aes.h" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/encrypt/base64.cpp" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/encrypt/base64.h" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/pattern/adapter.h" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/pattern/stratergy.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/CMakeLists.txt" beforeDir="false" afterPath="$PROJECT_DIR$/CMakeLists.txt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/Image/debug.h" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/Image/loger.h" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/Image/utils.h" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/libGeneral.a" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/inc/utils.h" beforeDir="false" afterPath="$PROJECT_DIR$/inc/utils.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/obj/inc/utils.h" beforeDir="false" afterPath="$PROJECT_DIR$/obj/inc/utils.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/pattern/Observer.cpp" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/pattern/signleton.cpp" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/utils.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/src/utils.cpp" afterDir="false" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -22,10 +40,15 @@
<option name="formatViaClangd" value="false" />
</component>
<component name="ExecutionTargetManager" SELECTED_TARGET="CMakeBuildProfile:Debug" />
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="OCFindUsagesOptions" text="true" ivars="false" properties="true" derivedClasses="false" />
<component name="ProjectId" id="1UhntPy4Y6cJ81thpskTMk9Asng" />
<component name="PropertiesComponent">
<property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" />
<property name="nodejs_npm_path_reset_for_default_project" value="true" />
<property name="settings.editor.selected.configurable" value="CPPToolchains" />
@ -64,10 +87,32 @@
<workItem from="1576304090241" duration="2459000" />
<workItem from="1576585606642" duration="7534000" />
<workItem from="1576913674335" duration="7707000" />
<workItem from="1576932562738" duration="10349000" />
<workItem from="1576983828513" duration="3804000" />
<workItem from="1577012242357" duration="1291000" />
<workItem from="1577695713801" duration="735000" />
<workItem from="1577715047417" duration="4424000" />
<workItem from="1577775991319" duration="2037000" />
<workItem from="1577933629796" duration="16000" />
<workItem from="1577933660908" duration="635000" />
<workItem from="1578153114620" duration="3459000" />
</task>
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="1" />
</component>
<component name="Vcs.Log.Tabs.Properties">
<option name="TAB_STATES">
<map>
<entry key="MAIN">
<value>
<State>
<option name="COLUMN_ORDER" />
</State>
</value>
</entry>
</map>
</option>
</component>
</project>

View File

@ -6,7 +6,7 @@ INCLUDE_DIRECTORIES (inc)
aux_source_directory(src DIRSRCS)
aux_source_directory(src/pattern PaternSrc)
add_library(General ${DIRSRCS} ${PaternSrc} src/pattern/signleton.h src/pattern/Observer.h)
add_library(General ${DIRSRCS} ${PaternSrc} src/pattern/signleton.h src/pattern/Observer.h src/pattern/stratergy.h src/pattern/adapter.h src/encrypt/base64.cpp src/encrypt/base64.h src/encrypt/aes.cpp src/encrypt/aes.h)
set(COPYITEM inc)
file(GLOB INCLUDES ${PROJECT_SOURCE_DIR}/inc/*)
file(COPY ${INCLUDES} DESTINATION ${LIBRARY_OUTPUT_PATH}/inc
@ -16,3 +16,7 @@ message(status ${INCLUDES})
file(GLOB PatternINCLUDES ${PROJECT_SOURCE_DIR}/src/pattern/*.h)
file(COPY ${PatternINCLUDES} DESTINATION ${LIBRARY_OUTPUT_PATH}/inc
FILE_PERMISSIONS OWNER_READ OWNER_WRITE GROUP_WRITE GROUP_READ WORLD_READ)
file(GLOB EncryptINCLUDES ${PROJECT_SOURCE_DIR}/src/encrypt/*.h)
file(COPY ${EncryptINCLUDES} DESTINATION ${LIBRARY_OUTPUT_PATH}/inc
FILE_PERMISSIONS OWNER_READ OWNER_WRITE GROUP_WRITE GROUP_READ WORLD_READ)

View File

@ -1,16 +0,0 @@
//
// Created by admin on 2019/4/11.
//
#ifndef __DEBUG__
#define __DEBUG__
#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
using namespace std;
int dumpObj(void *dst,int rowNum,int num,bool ifAsii,char *out);
int PrintDumpObjvoid (void *dst,int rowNum,int num,bool ifAsii);
#endif //SHARE_PRT_TEST_DEBUG_H

View File

@ -1,43 +0,0 @@
//
// Created by bt110 on 2019/8/19.
//
#ifndef CPP11FEATURETEST_LOGER_H
#define CPP11FEATURETEST_LOGER_H
#include <stdio.h>
#include <string>
#include <time.h>
#include <fstream>
#include <iostream>
using namespace std;
namespace Loger{
typedef enum Mode{
Mode_Daily, // 每天保存一次日志
MODE_Monthly, // 每个月保存一次日志
MODE_Weekly // 每周保存一次日志
}ESaveMode;
typedef class _C_Loger {
private:
string mCurrentPath;
FILE *mFile; // 日志文件
ESaveMode mMode; // 工作模式
string mCurrentDate; // 当天
int error;
public:
_C_Loger(FILE *p);
_C_Loger(string path);
int Debug();
int Log();
int LogFile();
void operator+(const string&);
void operator<<(const string&);
}Loger;
}
#endif //CPP11FEATURETEST_LOGER_H

View File

@ -1,15 +0,0 @@
//
// Created by 29019 on 2019/5/2.
//
#ifndef CUTILS_UTILS_H
#define CUTILS_UTILS_H
#include "utils.h"
#include <string>
#include "debug.h"
using namespace std;
string itos(int x);
#endif //CUTILS_UTILS_H

Binary file not shown.

View File

@ -8,8 +8,10 @@
#include "utils.h"
#include <string>
#include "debug.h"
using namespace std;
#include <sys/types.h>
#include <unistd.h>
using namespace std;
#define RELEASE_MEMORY(x) \
if(nullptr == x) \
@ -17,7 +19,6 @@ using namespace std;
delete(x); \
x = nullptr;\
}
string itos(int x);
#endif //CUTILS_UTILS_H

7
obj/inc/adapter.h Normal file
View File

@ -0,0 +1,7 @@
//
// Created by 29019 on 2019/12/30.
//
#ifndef GENERAL_ADAPTER_H
#define GENERAL_ADAPTER_H
#endif //GENERAL_ADAPTER_H

67
obj/inc/aes.h Normal file
View File

@ -0,0 +1,67 @@
//
// Created by 29019 on 2020/1/4.
//
#ifndef GENERAL_AES_H
#define GENERAL_AES_H
#include <exception>
#include <cstring>
#include <string>
#define BLOCK_SIZE 16
using namespace std;
class AES
{
public:
enum
{
ECB = 0, CBC = 1, CFB = 2
};
private:
enum
{
DEFAULT_BLOCK_SIZE = 16
};
enum
{
MAX_BLOCK_SIZE = 32, MAX_ROUNDS = 14, MAX_KC = 8, MAX_BC = 8
};
public:
AES();
virtual ~AES();
private:
//Key Initialization Flag
bool m_bKeyInit;
//Encryption (m_Ke) round key
int m_Ke[MAX_ROUNDS + 1][MAX_BC];
//Decryption (m_Kd) round key
int m_Kd[MAX_ROUNDS + 1][MAX_BC];
//Key Length
int m_keylength;
//Block Size
int m_blockSize;
//Number of Rounds
int m_iROUNDS;
//Chain Block
char m_chain0[MAX_BLOCK_SIZE];
char m_chain[MAX_BLOCK_SIZE];
//Auxiliary private use buffers
int tk[MAX_KC];
int a[MAX_BC];
int t[MAX_BC];
private:
void Xor(char* buff, char const* chain);
void DefEncryptBlock(char const* in, char* result);
void DefDecryptBlock(char const* in, char* result);
void EncryptBlock(char const* in, char* result);
void DecryptBlock(char const* in, char* result);
public:
void MakeKey(char const* key, char const* chain, int keylength =
DEFAULT_BLOCK_SIZE, int blockSize = DEFAULT_BLOCK_SIZE);
void Encrypt(char const* in, char* result, size_t n, int iMode = ECB);
void Decrypt(char const* in, char* result, size_t n, int iMode = ECB);
};
#endif //GENERAL_AES_H

11
obj/inc/base64.h Normal file
View File

@ -0,0 +1,11 @@
//
// Created by 29019 on 2020/1/4.
//
#ifndef GENERAL_BASE64_H
#define GENERAL_BASE64_H
#include <string>
std::string base64_encode(unsigned char const* , unsigned int len);
std::string base64_decode(std::string const& s);
#endif //GENERAL_BASE64_H

19
obj/inc/stratergy.h Normal file
View File

@ -0,0 +1,19 @@
//
// Created by 29019 on 2019/12/30.
//
#ifndef GENERAL_STRATERGY_H
#include <functional>
using namespace std;
template <typename T> class Stratergy{
public:
virtual int Handler(T){
return 0;
}
private:
};
#define GENERAL_STRATERGY_H
#endif //GENERAL_STRATERGY_H

View File

@ -8,8 +8,10 @@
#include "utils.h"
#include <string>
#include "debug.h"
using namespace std;
#include <sys/types.h>
#include <unistd.h>
using namespace std;
#define RELEASE_MEMORY(x) \
if(nullptr == x) \
@ -17,7 +19,6 @@ using namespace std;
delete(x); \
x = nullptr;\
}
string itos(int x);
#endif //CUTILS_UTILS_H

1136
src/encrypt/aes.cpp Normal file

File diff suppressed because it is too large Load Diff

67
src/encrypt/aes.h Normal file
View File

@ -0,0 +1,67 @@
//
// Created by 29019 on 2020/1/4.
//
#ifndef GENERAL_AES_H
#define GENERAL_AES_H
#include <exception>
#include <cstring>
#include <string>
#define BLOCK_SIZE 16
using namespace std;
class AES
{
public:
enum
{
ECB = 0, CBC = 1, CFB = 2
};
private:
enum
{
DEFAULT_BLOCK_SIZE = 16
};
enum
{
MAX_BLOCK_SIZE = 32, MAX_ROUNDS = 14, MAX_KC = 8, MAX_BC = 8
};
public:
AES();
virtual ~AES();
private:
//Key Initialization Flag
bool m_bKeyInit;
//Encryption (m_Ke) round key
int m_Ke[MAX_ROUNDS + 1][MAX_BC];
//Decryption (m_Kd) round key
int m_Kd[MAX_ROUNDS + 1][MAX_BC];
//Key Length
int m_keylength;
//Block Size
int m_blockSize;
//Number of Rounds
int m_iROUNDS;
//Chain Block
char m_chain0[MAX_BLOCK_SIZE];
char m_chain[MAX_BLOCK_SIZE];
//Auxiliary private use buffers
int tk[MAX_KC];
int a[MAX_BC];
int t[MAX_BC];
private:
void Xor(char* buff, char const* chain);
void DefEncryptBlock(char const* in, char* result);
void DefDecryptBlock(char const* in, char* result);
void EncryptBlock(char const* in, char* result);
void DecryptBlock(char const* in, char* result);
public:
void MakeKey(char const* key, char const* chain, int keylength =
DEFAULT_BLOCK_SIZE, int blockSize = DEFAULT_BLOCK_SIZE);
void Encrypt(char const* in, char* result, size_t n, int iMode = ECB);
void Decrypt(char const* in, char* result, size_t n, int iMode = ECB);
};
#endif //GENERAL_AES_H

102
src/encrypt/base64.cpp Normal file
View File

@ -0,0 +1,102 @@
//
// Created by 29019 on 2020/1/4.
//
#include "base64.h"
#include "base64.h"
#include <iostream>
#include <ctype.h>
static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
static inline bool is_base64(unsigned char c) {
return (isalnum(c) || (c == '+') || (c == '/'));
}
std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
std::string ret;
int i = 0;
int j = 0;
unsigned char char_array_3[3];
unsigned char char_array_4[4];
while (in_len--) {
char_array_3[i++] = *(bytes_to_encode++);
if (i == 3) {
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for(i = 0; (i <4) ; i++)
ret += base64_chars[char_array_4[i]];
i = 0;
}
}
if (i)
{
for(j = i; j < 3; j++)
char_array_3[j] = '\0';
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (j = 0; (j < i + 1); j++)
ret += base64_chars[char_array_4[j]];
while((i++ < 3))
ret += '=';
}
return ret;
}
std::string base64_decode(std::string const& encoded_string) {
int in_len = encoded_string.size();
int i = 0;
int j = 0;
int in_ = 0;
unsigned char char_array_4[4], char_array_3[3];
std::string ret;
while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
char_array_4[i++] = encoded_string[in_]; in_++;
if (i ==4) {
for (i = 0; i <4; i++)
char_array_4[i] = base64_chars.find(char_array_4[i]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; (i < 3); i++)
ret += char_array_3[i];
i = 0;
}
}
if (i) {
for (j = i; j <4; j++)
char_array_4[j] = 0;
for (j = 0; j <4; j++)
char_array_4[j] = base64_chars.find(char_array_4[j]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
}
return ret;
}

11
src/encrypt/base64.h Normal file
View File

@ -0,0 +1,11 @@
//
// Created by 29019 on 2020/1/4.
//
#ifndef GENERAL_BASE64_H
#define GENERAL_BASE64_H
#include <string>
std::string base64_encode(unsigned char const* , unsigned int len);
std::string base64_decode(std::string const& s);
#endif //GENERAL_BASE64_H

View File

@ -1,7 +0,0 @@
//
// Created by 29019 on 2019/12/21.
//
#include "Observer.h"

7
src/pattern/adapter.h Normal file
View File

@ -0,0 +1,7 @@
//
// Created by 29019 on 2019/12/30.
//
#ifndef GENERAL_ADAPTER_H
#define GENERAL_ADAPTER_H
#endif //GENERAL_ADAPTER_H

View File

@ -1,5 +0,0 @@
//
// Created by 29019 on 2019/12/17.
//
#include "signleton.h"

19
src/pattern/stratergy.h Normal file
View File

@ -0,0 +1,19 @@
//
// Created by 29019 on 2019/12/30.
//
#ifndef GENERAL_STRATERGY_H
#include <functional>
using namespace std;
template <typename T> class Stratergy{
public:
virtual int Handler(T){
return 0;
}
private:
};
#define GENERAL_STRATERGY_H
#endif //GENERAL_STRATERGY_H

View File

@ -2,6 +2,7 @@
// Created by 29019 on 2019/5/2.
//
#include "utils.h"
string itos(int x){
char buf[100] = {0};
itoa(x,buf,10);