├── test ├── crypt_java ├── AESUtil.class ├── CfgUtil.class ├── Coder.class ├── DBUtil.class ├── Hello.class ├── libdeclass.so ├── shell.sh ├── CfgUtil$1.class ├── Coder (2).class ├── DateUtil.class ├── EdocOperationCmd.class ├── EdocOperationCmd (2).class ├── test.sh └── declass.cpp ├── crypto_proc ├── crypt_java ├── Makefile ├── main.cpp ├── encrypto.h ├── lock.h ├── ngx_3des_crypto.h ├── mydes.h ├── mydes.cpp ├── ngx_3des_crypto.cpp └── encrypto.cpp ├── jvmti_agent ├── libdeclass.so ├── shell.sh └── declass.cpp └── README.md /test/crypt_java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tuyungang/Encrypt-Decrypt_Java_class_proc/HEAD/test/crypt_java -------------------------------------------------------------------------------- /test/AESUtil.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tuyungang/Encrypt-Decrypt_Java_class_proc/HEAD/test/AESUtil.class -------------------------------------------------------------------------------- /test/CfgUtil.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tuyungang/Encrypt-Decrypt_Java_class_proc/HEAD/test/CfgUtil.class -------------------------------------------------------------------------------- /test/Coder.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tuyungang/Encrypt-Decrypt_Java_class_proc/HEAD/test/Coder.class -------------------------------------------------------------------------------- /test/DBUtil.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tuyungang/Encrypt-Decrypt_Java_class_proc/HEAD/test/DBUtil.class -------------------------------------------------------------------------------- /test/Hello.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tuyungang/Encrypt-Decrypt_Java_class_proc/HEAD/test/Hello.class -------------------------------------------------------------------------------- /test/libdeclass.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tuyungang/Encrypt-Decrypt_Java_class_proc/HEAD/test/libdeclass.so -------------------------------------------------------------------------------- /test/shell.sh: -------------------------------------------------------------------------------- 1 | gcc -I${JAVA_HOME}/include/ -I${JAVA_HOME}/include/linux declass.cpp -shared -o libdeclass.so -fPIC -------------------------------------------------------------------------------- /test/CfgUtil$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tuyungang/Encrypt-Decrypt_Java_class_proc/HEAD/test/CfgUtil$1.class -------------------------------------------------------------------------------- /test/Coder (2).class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tuyungang/Encrypt-Decrypt_Java_class_proc/HEAD/test/Coder (2).class -------------------------------------------------------------------------------- /test/DateUtil.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tuyungang/Encrypt-Decrypt_Java_class_proc/HEAD/test/DateUtil.class -------------------------------------------------------------------------------- /crypto_proc/crypt_java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tuyungang/Encrypt-Decrypt_Java_class_proc/HEAD/crypto_proc/crypt_java -------------------------------------------------------------------------------- /jvmti_agent/libdeclass.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tuyungang/Encrypt-Decrypt_Java_class_proc/HEAD/jvmti_agent/libdeclass.so -------------------------------------------------------------------------------- /jvmti_agent/shell.sh: -------------------------------------------------------------------------------- 1 | g++ -I${JAVA_HOME}/include/ -I${JAVA_HOME}/include/linux declass.cpp -shared -o libdeclass.so -fPIC 2 | -------------------------------------------------------------------------------- /test/EdocOperationCmd.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tuyungang/Encrypt-Decrypt_Java_class_proc/HEAD/test/EdocOperationCmd.class -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Encrypt-Decrypt_Java_class_proc 2 | 1. 对java的编译后的.class文件进行加密,后然通过JVMTI接口agent解密,虚拟机加载通过接口加载解密后的.class文件获取相关类,从而达到类文件无法反编译。 3 | -------------------------------------------------------------------------------- /test/EdocOperationCmd (2).class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tuyungang/Encrypt-Decrypt_Java_class_proc/HEAD/test/EdocOperationCmd (2).class -------------------------------------------------------------------------------- /test/test.sh: -------------------------------------------------------------------------------- 1 | basepath=$(cd `dirname $0`; pwd) 2 | agentpath=$basepath/libdeclass.so 3 | encoderpath=$basepath/encoder.so 4 | cmdpath=$agentpath=$encoderpath 5 | echo $cmdpath 6 | java -agentpath:$cmdpath Hello 7 | -------------------------------------------------------------------------------- /crypto_proc/Makefile: -------------------------------------------------------------------------------- 1 | CXX = g++ 2 | #FFMPEG = /usr/local 3 | #INCLUDE_PATH = $(FFMPEG)/include/ 4 | #INCLUDE += 5 | #LIB_PATH = $(FFMPEG)/lib/ 6 | #CFLAGS = -I$(FFMPEG)/include -L$(FFMPEG)/lib 7 | #CFLAGS = -I/include 8 | #CXXFLAGS = -ldl -lpthread -lz -lm 9 | #CXXFLAGS = -lcrypto 10 | TARGET = crypt_java 11 | DIRS = . 12 | OBJS_FILES = $(foreach dir, $(DIRS), $(wildcard $(dir)/*.cpp)) 13 | OBJS = $(patsubst %.cpp, %.o, $(OBJS_FILES)) 14 | RM = rm -f 15 | 16 | #LKFLAGS = 17 | #INSTALLDIR = /mnt/ 18 | #install: 19 | #CP = cp 20 | #$(CP) $(TARGET) $(INSTALLDIR) 21 | #VPATH = path1:path2... 22 | #$(wildcard *.c) #搜索当前目前下匹配*.c的所有文件并返回文件列表 23 | #$(patsubst %.c, %.o, $(wildcard *.c)) #将搜索到的与*.c匹配的所有以.c后缀的文件替换成以.o后缀的文件 24 | #$(foreach dir, $(DIRS), $(wildcard $(dir)/*.c)) #循环$(DIRS)中的目录(以空格分隔)依次赋给变量dir,然后搜索该目录下匹配*.c的所有文件并返回文件列表 25 | 26 | all:$(TARGET) 27 | 28 | $(TARGET): $(OBJS) 29 | $(CXX) $^ -o $@ #$(CXXFLAGS) 30 | 31 | $(OBJS): %.o:%.cpp 32 | $(CXX) -g -c $< -o $@ #$(CFLAGS) 33 | 34 | #test.o: test.cpp 35 | # $(CXX) $(CFLAGS) $^ 36 | 37 | clean: 38 | @echo "cleaning project" 39 | -$(RM) $(OBJS) 40 | @echo "clean completed" 41 | .PHONY: clean 42 | -------------------------------------------------------------------------------- /crypto_proc/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "encrypto.h" 7 | 8 | using namespace std; 9 | 10 | int main() 11 | { 12 | CCRYPTO *enc = CCRYPTO::GetInstance(); 13 | //初始化 14 | int index = 1; 15 | enc->Crypt_init_context(NULL, index); 16 | //用户交互 17 | int ret = enc->User_command_prompt(); 18 | switch (ret) 19 | { 20 | case 1: 21 | cout << "************************************************" << endl; 22 | cout << "** 加密 **" << endl; 23 | cout << "************************************************" << endl; 24 | //加密 25 | enc->Encrypto_data(); 26 | break; 27 | case 2: 28 | cout << "************************************************" << endl; 29 | cout << "** 解密 **" << endl; 30 | cout << "************************************************" << endl; 31 | //解密 32 | enc->Decrypto_data(NULL); 33 | break; 34 | default: 35 | cout << "退出!" << endl; 36 | break; 37 | } 38 | //回收资源 39 | enc->Crypt_recyle_res(); 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /crypto_proc/encrypto.h: -------------------------------------------------------------------------------- 1 | #ifndef _TU_CRYPTO_H 2 | #define _TU_CRYPTO_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include "lock.h" 23 | #include "mydes.h" 24 | 25 | //#include "openssl/aes.h" 26 | 27 | #define MAX_PATH 1024 28 | //#define MAX_FILE_NAME 128 29 | 30 | using namespace std; 31 | 32 | typedef struct 33 | { 34 | char name[MAX_PATH]; 35 | int type; 36 | }FILE_ENC_STRUCT, *pFILE_ENC_STRUCT; 37 | 38 | //class CThreadMutex; 39 | class CCRYPTO 40 | { 41 | private: 42 | CCRYPTO(); 43 | 44 | public: 45 | ~CCRYPTO(); 46 | class freeInstance 47 | { 48 | public: 49 | ~freeInstance() 50 | { 51 | if (NULL != CCRYPTO::m_pInstance) { 52 | delete CCRYPTO::m_pInstance; 53 | } 54 | } 55 | }; 56 | static freeInstance _free; 57 | private: 58 | static CCRYPTO *m_pInstance; 59 | 60 | static CThreadMutex *m_lock; 61 | CThreadMutex m_ReadyFileMaplock; 62 | 63 | char g_CurrentPath[MAX_PATH]; 64 | 65 | map m_ReadyFileMap; 66 | 67 | Des *Crypt_3Des; 68 | 69 | private: 70 | //void Encrypto_3des(char *in, long size, char *out, int *out_len); 71 | //void Decrypto_3des(char *in, int size, char *out); 72 | //void Encrypto_aes(char *in, long size, char *out, unsigned int *out_len); 73 | //void Decrypto_aes(char *in, unsigned int size, char *out); 74 | string Encrypto_des(string in); 75 | string Decrypto_des(string in); 76 | string Decrypto_base64(string const& encoded_string); 77 | string Encrypto_base64(unsigned char const* bytes_to_encode, unsigned int in_len); 78 | void ListDir(string path); 79 | 80 | public: 81 | static CCRYPTO* GetInstance(); 82 | void Crypt_init_context(char *dirPath, int &index); 83 | int User_command_prompt(); 84 | //bool Encrypto_java(); 85 | //bool Decrypto_java(); 86 | void Encrypto_data(); 87 | void Decrypto_data(char *dirPath); 88 | int Crypt_recyle_res(); 89 | }; 90 | 91 | #endif 92 | -------------------------------------------------------------------------------- /crypto_proc/lock.h: -------------------------------------------------------------------------------- 1 | #ifndef THREADLOCK_H 2 | #define THREADLOCK_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | class CSemaphore 9 | { 10 | public: 11 | CSemaphore() 12 | { 13 | if( sem_init( &m_sem, 0, 0 ) != 0 ) 14 | { 15 | throw std::exception(); 16 | } 17 | } 18 | ~CSemaphore() 19 | { 20 | sem_destroy( &m_sem ); 21 | } 22 | bool wait() 23 | { 24 | return sem_wait( &m_sem ) == 0; 25 | } 26 | bool post() 27 | { 28 | return sem_post( &m_sem ) == 0; 29 | } 30 | 31 | private: 32 | sem_t m_sem; 33 | }; 34 | 35 | 36 | class CCondition 37 | { 38 | public: 39 | CCondition() 40 | { 41 | if( pthread_mutex_init( &m_mutex, NULL ) != 0) 42 | { 43 | throw std::exception(); 44 | } 45 | if ( pthread_cond_init( &m_cond, NULL ) != 0 ) 46 | { 47 | pthread_mutex_destroy( &m_mutex ); 48 | throw std::exception(); 49 | } 50 | } 51 | ~CCondition() 52 | { 53 | pthread_mutex_destroy( &m_mutex ); 54 | pthread_cond_destroy( &m_cond ); 55 | } 56 | bool wait() 57 | { 58 | int ret = 0; 59 | pthread_mutex_lock( &m_mutex ); 60 | ret = pthread_cond_wait( &m_cond, &m_mutex ); 61 | pthread_mutex_unlock( &m_mutex ); 62 | return ret == 0; 63 | } 64 | bool signal() 65 | { 66 | return pthread_cond_signal( &m_cond ) == 0; 67 | } 68 | 69 | private: 70 | pthread_mutex_t m_mutex; 71 | pthread_cond_t m_cond; 72 | }; 73 | 74 | class CThreadMutex 75 | { 76 | public: 77 | CThreadMutex() 78 | { 79 | if( pthread_mutex_init( &m_mutex, NULL ) != 0) 80 | { 81 | throw std::exception(); 82 | } 83 | } 84 | ~CThreadMutex() 85 | { 86 | pthread_mutex_destroy( &m_mutex ); 87 | } 88 | bool lock() 89 | { 90 | return pthread_mutex_lock( &m_mutex ) == 0; 91 | } 92 | bool unlock() 93 | { 94 | return pthread_mutex_unlock( &m_mutex ) == 0; 95 | } 96 | 97 | private: 98 | pthread_mutex_t m_mutex; 99 | }; 100 | 101 | #endif 102 | -------------------------------------------------------------------------------- /crypto_proc/ngx_3des_crypto.h: -------------------------------------------------------------------------------- 1 | #ifndef _DES_CRYPTO_H 2 | #define _DES_CRYPTO_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define DES_LONG unsigned long 12 | #define LEN_OF_KEY 24 13 | 14 | #define ITERATIONS 16 15 | 16 | #define c2l(c,l) (l =((DES_LONG)(*((c)++))) , \ 17 | l|=((DES_LONG)(*((c)++)))<< 8L, \ 18 | l|=((DES_LONG)(*((c)++)))<<16L, \ 19 | l|=((DES_LONG)(*((c)++)))<<24L) 20 | 21 | 22 | #define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \ 23 | *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \ 24 | *((c)++)=(unsigned char)(((l)>>16L)&0xff), \ 25 | *((c)++)=(unsigned char)(((l)>>24L)&0xff)) 26 | 27 | #define ROTATE(a,n) (((a)>>(n))+((a)<<(32-(n)))) 28 | 29 | #define LOAD_DATA_tmp(a,b,c,d,e,f) LOAD_DATA(a,b,c,d,e,f,g) 30 | #define LOAD_DATA(R,S,u,t,E0,E1,tmp) \ 31 | u=R^s[S ]; \ 32 | t=R^s[S+1] 33 | 34 | #define D_ENCRYPT(LL,R,S) {\ 35 | LOAD_DATA_tmp(R,S,u,t,E0,E1); \ 36 | t=ROTATE(t,4); \ 37 | LL^=\ 38 | TDES_SPtrans[0][(u>> 2L)&0x3f]^ \ 39 | TDES_SPtrans[2][(u>>10L)&0x3f]^ \ 40 | TDES_SPtrans[4][(u>>18L)&0x3f]^ \ 41 | TDES_SPtrans[6][(u>>26L)&0x3f]^ \ 42 | TDES_SPtrans[1][(t>> 2L)&0x3f]^ \ 43 | TDES_SPtrans[3][(t>>10L)&0x3f]^ \ 44 | TDES_SPtrans[5][(t>>18L)&0x3f]^ \ 45 | TDES_SPtrans[7][(t>>26L)&0x3f]; } 46 | 47 | #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\ 48 | (b)^=(t),\ 49 | (a)^=((t)<<(n))) 50 | 51 | #define IP(l,r) \ 52 | { \ 53 | register DES_LONG tt; \ 54 | PERM_OP(r,l,tt, 4,0x0f0f0f0fL); \ 55 | PERM_OP(l,r,tt,16,0x0000ffffL); \ 56 | PERM_OP(r,l,tt, 2,0x33333333L); \ 57 | PERM_OP(l,r,tt, 8,0x00ff00ffL); \ 58 | PERM_OP(r,l,tt, 1,0x55555555L); \ 59 | } 60 | 61 | #define FP(l,r) \ 62 | { \ 63 | register DES_LONG tt; \ 64 | PERM_OP(l,r,tt, 1,0x55555555L); \ 65 | PERM_OP(r,l,tt, 8,0x00ff00ffL); \ 66 | PERM_OP(l,r,tt, 2,0x33333333L); \ 67 | PERM_OP(r,l,tt,16,0x0000ffffL); \ 68 | PERM_OP(l,r,tt, 4,0x0f0f0f0fL); \ 69 | } 70 | 71 | //extern const DES_LONG TDES_SPtrans[8][64]; 72 | 73 | 74 | #define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\ 75 | (a)=(a)^(t)^(t>>(16-(n)))) 76 | 77 | 78 | typedef unsigned char DES_cblock[8]; 79 | typedef /* const */ unsigned char const_DES_cblock[8]; 80 | 81 | 82 | typedef struct DES_ks { 83 | union { 84 | DES_cblock cblock; 85 | /* 86 | * * make sure things are correct size on machines with 8 byte longs 87 | * */ 88 | DES_LONG deslong[2]; 89 | } ks[16]; 90 | } DES_key_schedule; 91 | 92 | 93 | # define DES_ENCRYPT 1 94 | # define DES_DECRYPT 0 95 | 96 | 97 | 98 | void DES_ecb3_encrypt(const_DES_cblock *input, DES_cblock *output, 99 | DES_key_schedule *ks1, DES_key_schedule *ks2, 100 | DES_key_schedule *ks3, int enc); 101 | 102 | 103 | void TDES_encrypt1(DES_LONG *data, DES_key_schedule *ks, int enc); 104 | 105 | void TDES_encrypt2(DES_LONG *data, DES_key_schedule *ks, int enc); 106 | 107 | void TDES_encrypt3(DES_LONG *data, DES_key_schedule *ks1, 108 | DES_key_schedule *ks2, DES_key_schedule *ks3); 109 | void TDES_decrypt3(DES_LONG *data, DES_key_schedule *ks1, 110 | DES_key_schedule *ks2, DES_key_schedule *ks3); 111 | 112 | void TDES_set_key_unchecked(const_DES_cblock *key, DES_key_schedule *schedule); 113 | 114 | 115 | #endif 116 | 117 | -------------------------------------------------------------------------------- /test/declass.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | jvmtiEnv* m_pJvmTI = NULL; 10 | 11 | 12 | /** 13 | * 加载函数回调 14 | */ 15 | void JNICALL cbClassFileLoadHook(jvmtiEnv *jvmti_env, 16 | JNIEnv *jni_env, 17 | jclass class_being_redefined, 18 | jobject loader, 19 | const char *name, 20 | jobject protection_domain, 21 | jint class_data_len, 22 | const unsigned char* class_data, 23 | jint *new_class_data_len, 24 | unsigned char** new_class_data) { 25 | 26 | printf("class name=%s len=%d,\n", name,class_data_len); 27 | 28 | 29 | unsigned char** b1; 30 | 31 | jint len=0,i=0,k=0; 32 | len=class_data_len%2==0?class_data_len/2:class_data_len/2+1; 33 | 34 | jvmti_env->Allocate(class_data_len, new_class_data); 35 | jvmti_env->Allocat(b1,len); 36 | jvmti_env->Allocat(buf,class_data_len); 37 | memcpy(*b1,class_data,len); 38 | 39 | for(i=0;iGetEnv((void **) &jvmti, JVMTI_VERSION_1_1); 77 | if (result != JNI_OK) { 78 | printf("ERROR: Unable to access JVMTI!\n"); 79 | return 1; 80 | } 81 | m_pJvmTI = jvmti; 82 | jvmtiCapabilities capabilities; 83 | // Clear the capabilities structure and set the ones you need. 84 | (void) memset(&capabilities, 0, sizeof(capabilities)); 85 | 86 | capabilities.can_generate_all_class_hook_events = 1; 87 | capabilities.can_tag_objects = 1; 88 | capabilities.can_generate_object_free_events = 1; 89 | capabilities.can_get_source_file_name = 1; 90 | capabilities.can_get_line_numbers = 1; 91 | capabilities.can_generate_vm_object_alloc_events = 1; 92 | // Request these capabilities for this JVM TI environment. 93 | error = jvmti->AddCapabilities(&capabilities); 94 | if (error != JVMTI_ERROR_NONE) { 95 | printf("ERROR: Unable to AddCapabilities JVMTI!\n"); 96 | return error; 97 | } 98 | 99 | //定义要监听的模块,这里定义了监听JVM初始化完毕模块 100 | jvmtiEventCallbacks callbacks; 101 | // Clear the callbacks structure and set the ones you want. 102 | (void) memset(&callbacks, 0, sizeof(callbacks)); 103 | callbacks.ClassFileLoadHook = &cbClassFileLoadHook; 104 | error = jvmti->SetEventCallbacks(&callbacks, (jint) sizeof(callbacks)); 105 | if (error != JVMTI_ERROR_NONE) { 106 | printf("ERROR: Unable to SetEventCallbacks JVMTI!\n"); 107 | return error; 108 | } 109 | // For each of the above callbacks, enable this event. 110 | error = jvmti->SetEventNotificationMode(JVMTI_ENABLE, 111 | JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, (jthread) NULL); 112 | if (error != JVMTI_ERROR_NONE) { 113 | printf("ERROR: Unable to SetEventNotificationMode JVMTI!\n"); 114 | return error; 115 | } 116 | return JNI_OK; // Indicates to the VM that the agent loaded OK. 117 | 118 | } 119 | -------------------------------------------------------------------------------- /jvmti_agent/declass.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | using namespace std; 15 | 16 | jvmtiEnv* m_pJvmTI = NULL; 17 | 18 | static const std::string base64_chars = 19 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 20 | "abcdefghijklmnopqrstuvwxyz" 21 | "0123456789+/"; 22 | 23 | static inline bool is_base64(unsigned char c) 24 | { 25 | return (isalnum(c) || (c == '+') || (c == '/')); 26 | } 27 | 28 | std::string Decrypto_base64(std::string const& encoded_string) 29 | { 30 | int in_len = encoded_string.size(); 31 | int i = 0; 32 | int j = 0; 33 | int in_ = 0; 34 | unsigned char char_array_4[4], char_array_3[3]; 35 | std::string ret; 36 | 37 | while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) { 38 | char_array_4[i++] = encoded_string[in_]; in_++; 39 | if (i ==4) { 40 | for (i = 0; i <4; i++) 41 | char_array_4[i] = base64_chars.find(char_array_4[i]); 42 | 43 | char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); 44 | char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); 45 | char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; 46 | 47 | for (i = 0; (i < 3); i++) 48 | ret += char_array_3[i]; 49 | i = 0; 50 | } 51 | } 52 | 53 | if (i) { 54 | for (j = i; j <4; j++) 55 | char_array_4[j] = 0; 56 | 57 | for (j = 0; j <4; j++) 58 | char_array_4[j] = base64_chars.find(char_array_4[j]); 59 | 60 | char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); 61 | char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); 62 | char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; 63 | 64 | for (j = 0; (j < i - 1); j++) ret += char_array_3[j]; 65 | } 66 | 67 | return ret; 68 | } 69 | 70 | /** 71 | * 加载函数回调 72 | */ 73 | void JNICALL cbClassFileLoadHook(jvmtiEnv *jvmti_env, 74 | JNIEnv *jni_env, 75 | jclass class_being_redefined, 76 | jobject loader, 77 | const char *name, 78 | jobject protection_domain, 79 | jint class_data_len, 80 | const unsigned char* class_data, 81 | jint *new_class_data_len, 82 | unsigned char** new_class_data) { 83 | 84 | printf("class name=%s, len=%d\n", name, class_data_len); 85 | /* 86 | long nFileFlag = 0; 87 | memcpy(&nFileFlag, class_data, sizeof(long)); 88 | if (ntohl(nFileFlag) == 0x1100) { 89 | char *enc_buf = (char*)malloc(class_data_len - sizeof(long) + 1); 90 | memset(enc_buf, 0, class_data_len - sizeof(long) + 1); 91 | memcpy(enc_buf, class_data + sizeof(long), class_data_len - sizeof(long)); 92 | string enc_in(enc_buf); 93 | //TODO:decrypto 94 | string dec_out = Decrypto_base64(enc_in); 95 | free(enc_buf); 96 | enc_buf = NULL; 97 | 98 | *new_class_data_len = (jint)dec_out.size(); 99 | jvmti_env->Allocate(*new_class_data_len, new_class_data); 100 | unsigned char *tmp_buf = *new_class_data; 101 | memcpy(tmp_buf, dec_out.c_str(), *new_class_data_len); 102 | } 103 | */ 104 | } 105 | 106 | /** 107 | * java类加载 108 | */ 109 | JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *vm, char *options, void *reserved) { 110 | jvmtiEnv *jvmti; 111 | jvmtiError error; 112 | // Create the JVM TI environment (jvmti). 113 | jint result = vm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1); 114 | if (result != JNI_OK) { 115 | printf("ERROR: Unable to access JVMTI!\n"); 116 | return 1; 117 | } 118 | m_pJvmTI = jvmti; 119 | jvmtiCapabilities capabilities; 120 | // Clear the capabilities structure and set the ones you need. 121 | (void) memset(&capabilities, 0, sizeof(capabilities)); 122 | 123 | capabilities.can_generate_all_class_hook_events = 1; 124 | capabilities.can_tag_objects = 1; 125 | capabilities.can_generate_object_free_events = 1; 126 | capabilities.can_get_source_file_name = 1; 127 | capabilities.can_get_line_numbers = 1; 128 | capabilities.can_generate_vm_object_alloc_events = 1; 129 | // Request these capabilities for this JVM TI environment. 130 | error = jvmti->AddCapabilities(&capabilities); 131 | if (error != JVMTI_ERROR_NONE) { 132 | printf("ERROR: Unable to AddCapabilities JVMTI!\n"); 133 | return error; 134 | } 135 | 136 | //定义要监听的模块,这里定义了监听JVM初始化完毕模块 137 | jvmtiEventCallbacks callbacks; 138 | // Clear the callbacks structure and set the ones you want. 139 | (void) memset(&callbacks, 0, sizeof(callbacks)); 140 | callbacks.ClassFileLoadHook = &cbClassFileLoadHook; 141 | error = jvmti->SetEventCallbacks(&callbacks, (jint) sizeof(callbacks)); 142 | if (error != JVMTI_ERROR_NONE) { 143 | printf("ERROR: Unable to SetEventCallbacks JVMTI!\n"); 144 | return error; 145 | } 146 | // For each of the above callbacks, enable this event. 147 | error = jvmti->SetEventNotificationMode(JVMTI_ENABLE, 148 | JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, (jthread) NULL); 149 | if (error != JVMTI_ERROR_NONE) { 150 | printf("ERROR: Unable to SetEventNotificationMode JVMTI!\n"); 151 | return error; 152 | } 153 | return JNI_OK; // Indicates to the VM that the agent loaded OK. 154 | 155 | } 156 | -------------------------------------------------------------------------------- /crypto_proc/mydes.h: -------------------------------------------------------------------------------- 1 | #ifndef _DES_H_ 2 | #define _DES_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | //为了提高程序效率,把这两个位操作功能定义在宏。 12 | 13 | //读取缓冲区的指定位. 14 | #define GET_BIT(p_array, bit_index) \ 15 | ((p_array[(bit_index) >> 3] >> (7 - ((bit_index) & 0x07))) & 0x01) 16 | 17 | //设置缓冲区的指定位. 18 | #define SET_BIT(p_array, bit_index, bit_val) \ 19 | if (1 == (bit_val)) \ 20 | {\ 21 | p_array[(bit_index) >> 3] |= 0x01 << (7 - ((bit_index) & 0x07));\ 22 | }\ 23 | else\ 24 | {\ 25 | p_array[(bit_index) >> 3] &= ~(0x01 << (7 - ((bit_index) & 0x07)));\ 26 | } 27 | 28 | //加解密标识,这两个标识涉及到对表的读取位置, 29 | //必须保证DES_ENCRYPT = 0 DES_DECRYPT = 1 30 | typedef enum { 31 | DES_ENCRYPT = 0, DES_DECRYPT = 1 32 | } DES_MODE; 33 | 34 | //key 35 | typedef enum { 36 | //1key模式 等同于des 37 | KEY_16, 38 | //2key模式 key1 = key3 39 | KEY_32, 40 | //3key模式 41 | KEY_48, 42 | KEY_ERROR 43 | } KEYMODE; 44 | 45 | class Des { 46 | private: 47 | std::string deskey; 48 | private: 49 | //字符输出处理 50 | void print_binary(char * tip, unsigned char * buff, unsigned char bits); 51 | void print_hex(char * tip, unsigned char * buff, unsigned char bytes); 52 | /////////////////////////////////////////////////////////////// 53 | // 函 数 名 : des 54 | // 函数功能 : DES加解密 55 | // 处理过程 : 根据标准的DES加密算法用输入的64位密钥对64位密文进行加/解密 56 | // 并将加/解密结果存储到p_output里 57 | // 时 间 : 2006年9月2日 58 | // 返 回 值 : 59 | // 参数说明 : const char * p_data 输入, 加密时输入明文, 解密时输入密文, 64位(8字节) 60 | // const char * p_key 输入, 密钥, 64位(8字节) 61 | // char * p_output 输出, 加密时输出密文, 解密时输入明文, 64位(8字节) 62 | // unsigned char mode 0 加密 1 解密 63 | /////////////////////////////////////////////////////////////// 64 | void des(const char * p_data, const char * p_key, const char * p_output, 65 | DES_MODE mode); 66 | //对两块大小相同的内存区进行异或 67 | //异或结果保存到第一块内存 68 | //unsigned char * p_buf_1 内存区1 69 | //const unsigned char * p_buf_2 内存区2 70 | //unsigned char bytes 内存区大小(单位:字节) 71 | void Xor(unsigned char * p_buf_1, const unsigned char * p_buf_2, 72 | unsigned char bytes); 73 | //将缓冲区从第bit_start位到第bit_end进行循环左移 74 | //offset只能是1,2 75 | void move_left(unsigned char * p_input, unsigned char bit_start, 76 | unsigned char bit_end, unsigned char offset); 77 | //将缓冲区从第bit_start位到第bit_end进行循环右移 78 | //offset只能是1,2 79 | void move_right(unsigned char * p_input, unsigned char bit_start, 80 | unsigned char bit_end, unsigned char offset); 81 | //缓冲区移位 82 | //offset大于0时左移 83 | void move_bits(unsigned char * p_input, unsigned char bit_start, 84 | unsigned char bit_end, char offset); 85 | //通用置换函数, bits <= 64 86 | //p_input与p_output不能指向同一个地址,否则置换会出错。 87 | void Permutation(unsigned char * p_input, unsigned char * p_output, 88 | const unsigned char * Table, unsigned char bits); 89 | //获取从bit_s为起始的第1, 6 位组成行 90 | unsigned char S_GetLine(unsigned char * p_data_ext, unsigned char bit_s); 91 | //获取从bit_s为起始的第2,3,4,5位组成列 92 | unsigned char S_GetRow(unsigned char * p_data_ext, unsigned char bit_s); 93 | 94 | /************************************************************************ 95 | *功能:字符串转换为16进制表示,每两位为一组 96 | ************************************************************************/ 97 | unsigned char* str_to_hex(const std::string& str); 98 | /************************************************************************ 99 | *功能:获得3DES分组的key 100 | ************************************************************************/ 101 | unsigned char *getKey1(std::string key); 102 | unsigned char *getKey2(std::string key); 103 | unsigned char *getKey3(std::string key); 104 | /************************************************************************ 105 | *功能:数组清空 106 | ************************************************************************/ 107 | void clearBuff(char *buff, int length); 108 | /*编码 109 | DataByte 110 | [in]输入的数据长度,以字节为单位 111 | */ 112 | std::string base64Encode(const unsigned char* Data, int DataByte); 113 | /*解码 114 | DataByte 115 | [in]输入的数据长度,以字节为单位 116 | OutByte 117 | [out]输出的数据长度,以字节为单位,请不要通过返回值计算 118 | 输出数据的长度 119 | */ 120 | std::string base64Decode(const char* Data, int DataByte, int& OutByte); 121 | 122 | //功能:URL字符转换 123 | unsigned char ToHex(unsigned char x); 124 | unsigned char FromHex(unsigned char x); 125 | //功能:URL-ENCODE编码 126 | std::string UrlEncode(const std::string& str); 127 | //功能:URL-DECODE解码 128 | std::string UrlDecode(const std::string& str); 129 | 130 | //替换字符串 131 | std::string replaceAllSubStr(string &str, string &oldStr, string &newStr); 132 | 133 | //检查key的长度,返回加密模式 134 | KEYMODE checkKey(); 135 | KEYMODE checkKey(std::string key); 136 | 137 | public: 138 | Des() {}; 139 | Des(std::string key) { 140 | deskey = key; 141 | }; 142 | 143 | ~Des() { 144 | deskey = nullptr; 145 | }; 146 | /************************************************************************ 147 | *功能:3DES-ECB加密 148 | ************************************************************************/ 149 | std::string desEncrypt(std::string str); 150 | 151 | /************************************************************************ 152 | *功能:3DES-ECB解密 153 | ************************************************************************/ 154 | std::string desDecrypt(std::string str); 155 | 156 | }; 157 | #endif //#ifndef _DES_H_ 158 | -------------------------------------------------------------------------------- /crypto_proc/mydes.cpp: -------------------------------------------------------------------------------- 1 | #include "mydes.h" 2 | 3 | const unsigned char Table_IP[64] = { 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 4 | 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 5 | 24, 16, 8, 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 6 | 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7 }; 7 | 8 | const unsigned char Table_InverseIP[64] = { 40, 8, 48, 16, 56, 24, 64, 32, 39, 9 | 7, 47, 15, 55, 23, 63, 31, 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 10 | 53, 21, 61, 29, 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 11 | 59, 27, 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25 }; 12 | 13 | const unsigned char Table_E[48] = { 32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9, 14 | 10, 11, 12, 13, 12, 13, 14, 15, 16, 17, 16, 17, 18, 19, 20, 21, 20, 21, 15 | 22, 23, 24, 25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1 }; 16 | 17 | const unsigned char Table_PC1[56] = { 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 18 | 34, 26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, 63, 19 | 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 20 | 37, 29, 21, 13, 5, 28, 20, 12, 4 }; 21 | 22 | const signed char Table_Move[2][16] = { 23 | { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 }, 24 | { 0, -1, -2, -2, -2, -2, -2, -2, -1, -2, -2, -2, -2, -2, -2, -1 } }; 25 | 26 | const unsigned char Table_PC2[48] = { 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 27 | 10, 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, 41, 52, 31, 37, 47, 55, 28 | 30, 40, 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32 }; 29 | 30 | const unsigned char Table_SBOX[8][4][16] = { 31 | 32 | 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, 0, 15, 7, 4, 14, 33 | 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 34 | 9, 7, 3, 10, 5, 0, 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13, 35 | 36 | 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, 3, 13, 4, 7, 15, 37 | 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 38 | 12, 6, 9, 3, 2, 15, 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 39 | 9, 40 | 41 | 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, 13, 7, 0, 9, 3, 4, 42 | 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 43 | 12, 5, 10, 14, 7, 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12, 44 | 45 | 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, 13, 8, 11, 5, 6, 46 | 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 47 | 3, 14, 5, 2, 8, 4, 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14, 48 | 49 | 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, 14, 11, 2, 12, 4, 50 | 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 51 | 12, 5, 6, 3, 0, 14, 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 52 | 3, 53 | 54 | 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, 10, 15, 4, 2, 7, 55 | 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 56 | 10, 1, 13, 11, 6, 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13, 57 | 58 | 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, 13, 0, 11, 7, 4, 59 | 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 60 | 6, 8, 0, 5, 9, 2, 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12, 61 | 62 | 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, 1, 15, 13, 8, 10, 63 | 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 64 | 13, 15, 3, 5, 8, 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 }; 65 | 66 | const unsigned char Table_P[32] = { 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 67 | 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 68 | 25 }; 69 | 70 | KEYMODE Des::checkKey() 71 | { 72 | if (deskey.length() != 0) { 73 | if (deskey.length() != 32 && deskey.length() != 48) { 74 | printf("key输入不正确,请输入32位key或者48位key"); 75 | return KEY_ERROR; 76 | } 77 | 78 | if (deskey.length() % 2 != 0) { 79 | printf("请检查输入的位数是否是偶数位"); 80 | return KEY_ERROR; 81 | } 82 | 83 | if (deskey.length() == 32) { 84 | return KEY_32; 85 | } 86 | 87 | if (deskey.length() == 48) { 88 | return KEY_48; 89 | } 90 | } else { 91 | return KEY_ERROR; 92 | } 93 | return KEY_ERROR; 94 | } 95 | 96 | KEYMODE Des::checkKey(std::string key) 97 | { 98 | deskey = key; 99 | 100 | if (deskey.length() != 32 && deskey.length() != 48) { 101 | printf("key输入不正确,请输入32位key或者48位key"); 102 | return KEY_ERROR; 103 | } 104 | 105 | if (deskey.length() % 2 != 0) { 106 | printf("请检查输入的位数是否是偶数位"); 107 | return KEY_ERROR; 108 | } 109 | 110 | if (deskey.length() == 32) { 111 | return KEY_32; 112 | } 113 | 114 | if (deskey.length() == 48) { 115 | return KEY_48; 116 | } 117 | return KEY_ERROR; 118 | } 119 | 120 | void Des::print_binary(char * tip, unsigned char * buff, unsigned char bits) 121 | { 122 | unsigned char bit_index = 0; 123 | 124 | printf("\r\n****** start %s ******\r\n", tip); 125 | 126 | for (bit_index = 0; bit_index < bits; bit_index++) { 127 | printf("%d", (buff[bit_index >> 3] >> (7 - (bit_index % 8))) & 0x01); 128 | 129 | if ((bit_index + 1) % 4 == 0) { 130 | printf(" "); 131 | } 132 | 133 | if ((bit_index + 1) % 64 == 0) { 134 | printf("\r\n"); 135 | } 136 | } 137 | 138 | printf("\r\n****** end %s ******\r\n", tip); 139 | } 140 | 141 | void Des::print_hex(char * tip, unsigned char * buff, unsigned char bytes) 142 | { 143 | unsigned char byte_index = 0; 144 | 145 | printf("\r\n %s", tip); 146 | 147 | for (byte_index = 0; byte_index < bytes; byte_index++) { 148 | 149 | } 150 | } 151 | 152 | void Des::Xor(unsigned char * p_buf_1, const unsigned char * p_buf_2, unsigned char bytes) 153 | { 154 | while (bytes > 0) { 155 | bytes--; 156 | 157 | p_buf_1[bytes] ^= p_buf_2[bytes]; 158 | } 159 | } 160 | 161 | void Des::move_left(unsigned char * p_input, unsigned char bit_start, unsigned char bit_end, unsigned char offset) 162 | { 163 | unsigned char b_val = 0; 164 | unsigned char b_tmp1 = 0; 165 | unsigned char b_tmp2 = 0; 166 | 167 | 168 | b_tmp1 = GET_BIT(p_input, bit_start); 169 | b_tmp2 = GET_BIT(p_input, bit_start + 1); 170 | 171 | 172 | for (; bit_start <= (bit_end - offset); bit_start++) { 173 | b_val = GET_BIT(p_input, bit_start + offset); 174 | SET_BIT(p_input, bit_start, b_val); 175 | } 176 | 177 | 178 | if (1 == offset) { 179 | SET_BIT(p_input, bit_end, b_tmp1); 180 | } else { 181 | SET_BIT(p_input, bit_end, b_tmp2); 182 | SET_BIT(p_input, bit_end - 1, b_tmp1); 183 | } 184 | } 185 | 186 | void Des::move_right(unsigned char * p_input, unsigned char bit_start, unsigned char bit_end, unsigned char offset) 187 | { 188 | unsigned char b_val = 0; 189 | unsigned char b_tmp1 = 0; 190 | unsigned char b_tmp2 = 0; 191 | 192 | 193 | b_tmp1 = GET_BIT(p_input, bit_end); 194 | b_tmp2 = GET_BIT(p_input, bit_end - 1); 195 | 196 | 197 | for (; bit_end >= (bit_start + offset); bit_end--) { 198 | b_val = GET_BIT(p_input, bit_end - offset); 199 | SET_BIT(p_input, bit_end, b_val); 200 | } 201 | 202 | 203 | if (1 == offset) { 204 | SET_BIT(p_input, bit_start, b_tmp1); 205 | } else { 206 | SET_BIT(p_input, bit_start, b_tmp2); 207 | SET_BIT(p_input, bit_start + 1, b_tmp1); 208 | } 209 | } 210 | 211 | 212 | void Des::move_bits(unsigned char * p_input, unsigned char bit_start, unsigned char bit_end, char offset) 213 | { 214 | if (0 < offset) 215 | { 216 | move_left(p_input, bit_start, bit_end, offset); 217 | } else if (0 > offset) 218 | { 219 | move_right(p_input, bit_start, bit_end, -offset); 220 | } 221 | } 222 | 223 | void Des::Permutation(unsigned char * p_input, unsigned char * p_output, const unsigned char * Table, unsigned char bits) 224 | { 225 | unsigned char b_val = false; 226 | unsigned char bit_index = 0; 227 | 228 | for (bit_index = 0; bit_index < bits; bit_index++) { 229 | b_val = GET_BIT(p_input, Table[bit_index] - 1); 230 | 231 | SET_BIT(p_output, bit_index, b_val); 232 | } 233 | } 234 | 235 | unsigned char Des::S_GetLine(unsigned char * p_data_ext, unsigned char bit_s) 236 | { 237 | return (GET_BIT(p_data_ext, bit_s + 0) << 1) + GET_BIT(p_data_ext, bit_s + 5); 238 | } 239 | 240 | unsigned char Des::S_GetRow(unsigned char * p_data_ext, unsigned char bit_s) 241 | { 242 | unsigned char row; 243 | 244 | row = GET_BIT(p_data_ext, bit_s + 1); 245 | row <<= 1; 246 | row += GET_BIT(p_data_ext, bit_s + 2); 247 | row <<= 1; 248 | row += GET_BIT(p_data_ext, bit_s + 3); 249 | row <<= 1; 250 | row += GET_BIT(p_data_ext, bit_s + 4); 251 | 252 | return row; 253 | } 254 | 255 | void Des::des(const char * p_data, const char * p_key, const char * p_output, 256 | DES_MODE mode) 257 | { 258 | unsigned char loop = 0; //16轮运算的循环计数器 259 | unsigned char key_tmp[8]; //密钥运算时存储中间结果 260 | unsigned char sub_key[6]; //用于存储子密钥 261 | 262 | unsigned char * p_left; 263 | const char* p_right; 264 | 265 | unsigned char p_right_ext[8]; //R[i]经过扩展置换生成的48位数据(6字节), 及最终结果的存储 266 | unsigned char p_right_s[4]; //经过S_BOX置换后的32位数据(4字节) 267 | 268 | unsigned char s_loop = 0; //S_BOX置换的循环计数器 269 | 270 | 271 | Permutation((unsigned char*) p_key, key_tmp, Table_PC1, 56); 272 | 273 | 274 | Permutation((unsigned char*) p_data, (unsigned char*) p_output, Table_IP, 275 | 64); 276 | 277 | p_left = (unsigned char*) p_output; //L0 278 | p_right = &p_output[4]; //R0 279 | 280 | for (loop = 0; loop < 16; loop++) { 281 | 282 | move_bits(key_tmp, 0, 27, Table_Move[mode][loop]); 283 | move_bits(key_tmp, 28, 55, Table_Move[mode][loop]); 284 | 285 | 286 | Permutation(key_tmp, sub_key, Table_PC2, 48); 287 | 288 | 289 | Permutation((unsigned char*) p_right, p_right_ext, Table_E, 48); 290 | 291 | 292 | Xor(p_right_ext, sub_key, 6); 293 | 294 | 295 | for (s_loop = 0; s_loop < 4; s_loop++) { 296 | unsigned char s_line = 0; 297 | unsigned char s_row = 0; 298 | unsigned char s_bit = s_loop * 12; 299 | 300 | s_line = S_GetLine(p_right_ext, s_bit); 301 | s_row = S_GetRow(p_right_ext, s_bit); 302 | 303 | p_right_s[s_loop] = Table_SBOX[s_loop * 2][s_line][s_row]; 304 | 305 | s_bit += 6; 306 | 307 | s_line = S_GetLine(p_right_ext, s_bit); 308 | s_row = S_GetRow(p_right_ext, s_bit); 309 | 310 | p_right_s[s_loop] <<= 4; 311 | p_right_s[s_loop] += Table_SBOX[(s_loop * 2) + 1][s_line][s_row]; 312 | } 313 | 314 | Permutation(p_right_s, p_right_ext, Table_P, 32); 315 | 316 | Xor(p_right_ext, p_left, 4); 317 | 318 | memcpy(p_left, (unsigned char*) p_right, 4); 319 | memcpy((unsigned char*) p_right, p_right_ext, 4); 320 | } 321 | 322 | memcpy(&p_right_ext[4], p_left, 4); 323 | memcpy(p_right_ext, p_right, 4); 324 | 325 | Permutation(p_right_ext, (unsigned char*) p_output, Table_InverseIP, 64); 326 | } 327 | 328 | unsigned char* Des::str_to_hex(const std::string& str) 329 | { 330 | unsigned char *buff = NULL; 331 | int size = sizeof(unsigned char *) * str.length() / 2; 332 | buff = (unsigned char*) malloc(size); 333 | memset((void*) buff, 0, size); 334 | 335 | std::stringstream ss; 336 | ss << std::hex; 337 | for (int i = 0; i != str.size() / 2; ++i) { 338 | int val = 0; 339 | ss << str.substr(i * 2, 2); 340 | ss >> val; 341 | buff[i] = val; 342 | ss.clear(); 343 | } 344 | return buff; 345 | } 346 | 347 | unsigned char *Des::getKey1(std::string key) 348 | { 349 | unsigned char *key1; 350 | key1 = str_to_hex(key.substr(0, 16)); 351 | return key1; 352 | } 353 | 354 | unsigned char *Des::getKey2(std::string key) 355 | { 356 | unsigned char *key2; 357 | key2 = str_to_hex(key.substr(16, 32)); 358 | return key2; 359 | } 360 | 361 | unsigned char *Des::getKey3(std::string key) 362 | { 363 | unsigned char *key3; 364 | key3 = str_to_hex(key.substr(32, 48)); 365 | return key3; 366 | } 367 | 368 | void Des::clearBuff(char *buff, int length) 369 | { 370 | for (int i = 0; i < length; i++) { 371 | buff[i] = '\0'; 372 | } 373 | } 374 | 375 | std::string Des::base64Encode(const unsigned char* Data, int DataByte) 376 | { 377 | const char EncodeTable[] = 378 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 379 | 380 | std::string strEncode; 381 | unsigned char Tmp[4] = { 0 }; 382 | int LineLength = 0; 383 | for (int i = 0; i < (int) (DataByte / 3); i++) { 384 | Tmp[1] = *Data++; 385 | Tmp[2] = *Data++; 386 | Tmp[3] = *Data++; 387 | strEncode += EncodeTable[Tmp[1] >> 2]; 388 | strEncode += EncodeTable[((Tmp[1] << 4) | (Tmp[2] >> 4)) & 0x3F]; 389 | strEncode += EncodeTable[((Tmp[2] << 2) | (Tmp[3] >> 6)) & 0x3F]; 390 | strEncode += EncodeTable[Tmp[3] & 0x3F]; 391 | if (LineLength += 4, LineLength == 76) { 392 | strEncode += "\r\n"; 393 | LineLength = 0; 394 | } 395 | } 396 | 397 | int Mod = DataByte % 3; 398 | if (Mod == 1) { 399 | Tmp[1] = *Data++; 400 | strEncode += EncodeTable[(Tmp[1] & 0xFC) >> 2]; 401 | strEncode += EncodeTable[((Tmp[1] & 0x03) << 4)]; 402 | strEncode += "=="; 403 | } else if (Mod == 2) { 404 | Tmp[1] = *Data++; 405 | Tmp[2] = *Data++; 406 | strEncode += EncodeTable[(Tmp[1] & 0xFC) >> 2]; 407 | strEncode += 408 | EncodeTable[((Tmp[1] & 0x03) << 4) | ((Tmp[2] & 0xF0) >> 4)]; 409 | strEncode += EncodeTable[((Tmp[2] & 0x0F) << 2)]; 410 | strEncode += "="; 411 | } 412 | 413 | return strEncode; 414 | } 415 | 416 | std::string Des::base64Decode(const char* Data, int DataByte, int& OutByte) 417 | { 418 | 419 | const char DecodeTable[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 420 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 421 | 0, 0, 0, 0, 422 | 62, // '+' 423 | 0, 0, 0, 424 | 63, // '/' 425 | 52, 53, 54, 55, 56, 57, 58, 59, 60, 426 | 61, // '0'-'9' 427 | 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 428 | 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 429 | 25, // 'A'-'Z' 430 | 0, 0, 0, 0, 0, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 431 | 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // 'a'-'z' 432 | }; 433 | 434 | std::string strDecode; 435 | int nValue; 436 | int i = 0; 437 | while (i < DataByte) { 438 | if (*Data != '\r' && *Data != '\n') { 439 | nValue = DecodeTable[*Data++] << 18; 440 | nValue += DecodeTable[*Data++] << 12; 441 | strDecode += (nValue & 0x00FF0000) >> 16; 442 | OutByte++; 443 | if (*Data != '=') { 444 | nValue += DecodeTable[*Data++] << 6; 445 | strDecode += (nValue & 0x0000FF00) >> 8; 446 | OutByte++; 447 | if (*Data != '=') { 448 | nValue += DecodeTable[*Data++]; 449 | strDecode += nValue & 0x000000FF; 450 | OutByte++; 451 | } 452 | } 453 | i += 4; 454 | } else // 回车换行,跳过 455 | { 456 | Data++; 457 | i++; 458 | } 459 | } 460 | return strDecode; 461 | } 462 | 463 | unsigned char Des::ToHex(unsigned char x) 464 | { 465 | return x > 9 ? x + 55 : x + 48; 466 | } 467 | 468 | unsigned char Des::FromHex(unsigned char x) 469 | { 470 | unsigned char y = NULL; 471 | if (x >= 'A' && x <= 'Z') 472 | y = x - 'A' + 10; 473 | else if (x >= 'a' && x <= 'z') 474 | y = x - 'a' + 10; 475 | else if (x >= '0' && x <= '9') 476 | y = x - '0'; 477 | return y; 478 | } 479 | 480 | std::string Des::UrlEncode(const std::string& str) 481 | { 482 | std::string strTemp = ""; 483 | size_t length = str.length(); 484 | for (size_t i = 0; i < length; i++) { 485 | if (isalnum((unsigned char) str[i]) || (str[i] == '-') 486 | || (str[i] == '_') || (str[i] == '.') || (str[i] == '~')) 487 | strTemp += str[i]; 488 | else if (str[i] == ' ') 489 | strTemp += "+"; 490 | else { 491 | strTemp += '%'; 492 | strTemp += ToHex((unsigned char) str[i] >> 4); 493 | strTemp += ToHex((unsigned char) str[i] % 16); 494 | } 495 | } 496 | return strTemp; 497 | } 498 | 499 | std::string Des::UrlDecode(const std::string& str) 500 | { 501 | std::string strTemp = ""; 502 | size_t length = str.length(); 503 | for (size_t i = 0; i < length; i++) { 504 | if (str[i] == '+') 505 | strTemp += ' '; 506 | else if (str[i] == '%') { 507 | 508 | unsigned char high = FromHex((unsigned char) str[++i]); 509 | unsigned char low = FromHex((unsigned char) str[++i]); 510 | strTemp += high * 16 + low; 511 | } else 512 | strTemp += str[i]; 513 | } 514 | return strTemp; 515 | } 516 | 517 | 518 | std::string Des::replaceAllSubStr(string &str, string &oldStr, string &newStr) 519 | { 520 | while (true) { 521 | string::size_type pos(0); 522 | if ((pos = str.find(oldStr)) != string::npos) { 523 | str.replace(pos, oldStr.length(), newStr); 524 | } else { 525 | break; 526 | } 527 | } 528 | return str; 529 | } 530 | 531 | 532 | std::string Des::desEncrypt(std::string str) 533 | { 534 | auto mode = checkKey(deskey); 535 | if (mode == KEY_ERROR) { 536 | return nullptr; 537 | } 538 | 539 | int block = 0; 540 | int len = str.length(); 541 | std::string encode = ""; 542 | std::string result = ""; 543 | 544 | char *key1 = NULL; 545 | char *key2 = NULL; 546 | char *key3 = NULL; 547 | if (mode == KEY_16) { 548 | key1 = (char*) getKey1(deskey); 549 | key2 = (char*) getKey1(deskey); 550 | key3 = (char*) getKey1(deskey); 551 | } 552 | if (mode == KEY_32) { 553 | key1 = (char*) getKey1(deskey); 554 | key2 = (char*) getKey2(deskey); 555 | key3 = (char*) getKey1(deskey); 556 | } 557 | if (mode == KEY_48) { 558 | key1 = (char*) getKey1(deskey); 559 | key2 = (char*) getKey2(deskey); 560 | key3 = (char*) getKey3(deskey); 561 | } 562 | 563 | if (len % 8 == 0) { 564 | block = len / 8; 565 | } else { 566 | block = len / 8 + 1; 567 | } 568 | 569 | unsigned char *result1 = NULL; 570 | int size = sizeof(unsigned char *) * 8 * block + 1; 571 | result1 = (unsigned char*) malloc(size); 572 | memset(result1, 0, size); 573 | 574 | char en_out_put[9] = { 0 }; 575 | char en_data[9] = { 0 }; 576 | 577 | for (int i = 0; i < block; i++) { 578 | clearBuff(en_data, 9); 579 | if (i == block - 1) { 580 | if (len % 8 == 0) { 581 | memcpy(en_data, str.c_str() + (i * 8), 8); 582 | } else { 583 | memcpy(en_data, str.c_str() + (i * 8), len % 8); 584 | } 585 | } else { 586 | memcpy(en_data, str.c_str() + (i * 8), 8); 587 | } 588 | 589 | des(en_data, key1, en_out_put, DES_ENCRYPT); 590 | des(en_out_put, key2, en_data, DES_DECRYPT); 591 | des(en_data, key3, en_out_put, DES_ENCRYPT); 592 | 593 | for (int n = 0; n < 8; n++) { 594 | result1[(i * 8) + n] = en_out_put[n]; 595 | } 596 | } 597 | encode = base64Encode((unsigned char*) result1, block * 8); 598 | 599 | free(key1); 600 | free(key2); 601 | free(key3); 602 | 603 | free(result1); 604 | 605 | return encode; 606 | } 607 | 608 | std::string Des::desDecrypt(std::string str) 609 | { 610 | auto mode = checkKey(deskey); 611 | if (mode == KEY_ERROR) { 612 | return nullptr; 613 | } 614 | 615 | char *key1 = NULL; 616 | char *key2 = NULL; 617 | char *key3 = NULL; 618 | if (mode == KEY_16) { 619 | key1 = (char*) getKey1(deskey); 620 | key2 = (char*) getKey1(deskey); 621 | key3 = (char*) getKey1(deskey); 622 | } 623 | if (mode == KEY_32) { 624 | key1 = (char*) getKey1(deskey); 625 | key2 = (char*) getKey2(deskey); 626 | key3 = (char*) getKey1(deskey); 627 | } 628 | if (mode == KEY_48) { 629 | key1 = (char*) getKey1(deskey); 630 | key2 = (char*) getKey2(deskey); 631 | key3 = (char*) getKey3(deskey); 632 | } 633 | 634 | int OutByte = 0; 635 | std::string decode = ""; 636 | std::string result = ""; 637 | 638 | decode = base64Decode(str.c_str(), str.length(), OutByte); 639 | 640 | 641 | if(OutByte % 8 != 0) { 642 | 643 | return "isWrongInput"; 644 | } 645 | 646 | int block = 0; 647 | block = OutByte / 8; 648 | 649 | 650 | char en_out_put[9] = { 0 }; 651 | char en_data[9] = { 0 }; 652 | 653 | for (int i = 0; i < block; i++) { 654 | memcpy(en_out_put, decode.c_str() + (i * 8), 8); 655 | des(en_out_put, key3, en_data, DES_DECRYPT); 656 | des(en_data, key2, en_out_put, DES_ENCRYPT); 657 | des(en_out_put, key1, en_data, DES_DECRYPT); 658 | result += en_data; 659 | } 660 | 661 | free(key1); 662 | free(key2); 663 | free(key3); 664 | 665 | return result; 666 | } 667 | -------------------------------------------------------------------------------- /crypto_proc/ngx_3des_crypto.cpp: -------------------------------------------------------------------------------- 1 | #include "ngx_3des_crypto.h" 2 | 3 | extern const DES_LONG TDES_SPtrans[8][64]; 4 | 5 | static const DES_LONG des_skb[8][64] = { 6 | { 7 | /* for C bits (numbered as per FIPS 46) 1 2 3 4 5 6 */ 8 | 0x00000000L, 0x00000010L, 0x20000000L, 0x20000010L, 9 | 0x00010000L, 0x00010010L, 0x20010000L, 0x20010010L, 10 | 0x00000800L, 0x00000810L, 0x20000800L, 0x20000810L, 11 | 0x00010800L, 0x00010810L, 0x20010800L, 0x20010810L, 12 | 0x00000020L, 0x00000030L, 0x20000020L, 0x20000030L, 13 | 0x00010020L, 0x00010030L, 0x20010020L, 0x20010030L, 14 | 0x00000820L, 0x00000830L, 0x20000820L, 0x20000830L, 15 | 0x00010820L, 0x00010830L, 0x20010820L, 0x20010830L, 16 | 0x00080000L, 0x00080010L, 0x20080000L, 0x20080010L, 17 | 0x00090000L, 0x00090010L, 0x20090000L, 0x20090010L, 18 | 0x00080800L, 0x00080810L, 0x20080800L, 0x20080810L, 19 | 0x00090800L, 0x00090810L, 0x20090800L, 0x20090810L, 20 | 0x00080020L, 0x00080030L, 0x20080020L, 0x20080030L, 21 | 0x00090020L, 0x00090030L, 0x20090020L, 0x20090030L, 22 | 0x00080820L, 0x00080830L, 0x20080820L, 0x20080830L, 23 | 0x00090820L, 0x00090830L, 0x20090820L, 0x20090830L, 24 | }, 25 | { 26 | /* for C bits (numbered as per FIPS 46) 7 8 10 11 12 13 */ 27 | 0x00000000L, 0x02000000L, 0x00002000L, 0x02002000L, 28 | 0x00200000L, 0x02200000L, 0x00202000L, 0x02202000L, 29 | 0x00000004L, 0x02000004L, 0x00002004L, 0x02002004L, 30 | 0x00200004L, 0x02200004L, 0x00202004L, 0x02202004L, 31 | 0x00000400L, 0x02000400L, 0x00002400L, 0x02002400L, 32 | 0x00200400L, 0x02200400L, 0x00202400L, 0x02202400L, 33 | 0x00000404L, 0x02000404L, 0x00002404L, 0x02002404L, 34 | 0x00200404L, 0x02200404L, 0x00202404L, 0x02202404L, 35 | 0x10000000L, 0x12000000L, 0x10002000L, 0x12002000L, 36 | 0x10200000L, 0x12200000L, 0x10202000L, 0x12202000L, 37 | 0x10000004L, 0x12000004L, 0x10002004L, 0x12002004L, 38 | 0x10200004L, 0x12200004L, 0x10202004L, 0x12202004L, 39 | 0x10000400L, 0x12000400L, 0x10002400L, 0x12002400L, 40 | 0x10200400L, 0x12200400L, 0x10202400L, 0x12202400L, 41 | 0x10000404L, 0x12000404L, 0x10002404L, 0x12002404L, 42 | 0x10200404L, 0x12200404L, 0x10202404L, 0x12202404L, 43 | }, 44 | { 45 | /* for C bits (numbered as per FIPS 46) 14 15 16 17 19 20 */ 46 | 0x00000000L, 0x00000001L, 0x00040000L, 0x00040001L, 47 | 0x01000000L, 0x01000001L, 0x01040000L, 0x01040001L, 48 | 0x00000002L, 0x00000003L, 0x00040002L, 0x00040003L, 49 | 0x01000002L, 0x01000003L, 0x01040002L, 0x01040003L, 50 | 0x00000200L, 0x00000201L, 0x00040200L, 0x00040201L, 51 | 0x01000200L, 0x01000201L, 0x01040200L, 0x01040201L, 52 | 0x00000202L, 0x00000203L, 0x00040202L, 0x00040203L, 53 | 0x01000202L, 0x01000203L, 0x01040202L, 0x01040203L, 54 | 0x08000000L, 0x08000001L, 0x08040000L, 0x08040001L, 55 | 0x09000000L, 0x09000001L, 0x09040000L, 0x09040001L, 56 | 0x08000002L, 0x08000003L, 0x08040002L, 0x08040003L, 57 | 0x09000002L, 0x09000003L, 0x09040002L, 0x09040003L, 58 | 0x08000200L, 0x08000201L, 0x08040200L, 0x08040201L, 59 | 0x09000200L, 0x09000201L, 0x09040200L, 0x09040201L, 60 | 0x08000202L, 0x08000203L, 0x08040202L, 0x08040203L, 61 | 0x09000202L, 0x09000203L, 0x09040202L, 0x09040203L, 62 | }, 63 | { 64 | /* for C bits (numbered as per FIPS 46) 21 23 24 26 27 28 */ 65 | 0x00000000L, 0x00100000L, 0x00000100L, 0x00100100L, 66 | 0x00000008L, 0x00100008L, 0x00000108L, 0x00100108L, 67 | 0x00001000L, 0x00101000L, 0x00001100L, 0x00101100L, 68 | 0x00001008L, 0x00101008L, 0x00001108L, 0x00101108L, 69 | 0x04000000L, 0x04100000L, 0x04000100L, 0x04100100L, 70 | 0x04000008L, 0x04100008L, 0x04000108L, 0x04100108L, 71 | 0x04001000L, 0x04101000L, 0x04001100L, 0x04101100L, 72 | 0x04001008L, 0x04101008L, 0x04001108L, 0x04101108L, 73 | 0x00020000L, 0x00120000L, 0x00020100L, 0x00120100L, 74 | 0x00020008L, 0x00120008L, 0x00020108L, 0x00120108L, 75 | 0x00021000L, 0x00121000L, 0x00021100L, 0x00121100L, 76 | 0x00021008L, 0x00121008L, 0x00021108L, 0x00121108L, 77 | 0x04020000L, 0x04120000L, 0x04020100L, 0x04120100L, 78 | 0x04020008L, 0x04120008L, 0x04020108L, 0x04120108L, 79 | 0x04021000L, 0x04121000L, 0x04021100L, 0x04121100L, 80 | 0x04021008L, 0x04121008L, 0x04021108L, 0x04121108L, 81 | }, 82 | { 83 | /* for D bits (numbered as per FIPS 46) 1 2 3 4 5 6 */ 84 | 0x00000000L, 0x10000000L, 0x00010000L, 0x10010000L, 85 | 0x00000004L, 0x10000004L, 0x00010004L, 0x10010004L, 86 | 0x20000000L, 0x30000000L, 0x20010000L, 0x30010000L, 87 | 0x20000004L, 0x30000004L, 0x20010004L, 0x30010004L, 88 | 0x00100000L, 0x10100000L, 0x00110000L, 0x10110000L, 89 | 0x00100004L, 0x10100004L, 0x00110004L, 0x10110004L, 90 | 0x20100000L, 0x30100000L, 0x20110000L, 0x30110000L, 91 | 0x20100004L, 0x30100004L, 0x20110004L, 0x30110004L, 92 | 0x00001000L, 0x10001000L, 0x00011000L, 0x10011000L, 93 | 0x00001004L, 0x10001004L, 0x00011004L, 0x10011004L, 94 | 0x20001000L, 0x30001000L, 0x20011000L, 0x30011000L, 95 | 0x20001004L, 0x30001004L, 0x20011004L, 0x30011004L, 96 | 0x00101000L, 0x10101000L, 0x00111000L, 0x10111000L, 97 | 0x00101004L, 0x10101004L, 0x00111004L, 0x10111004L, 98 | 0x20101000L, 0x30101000L, 0x20111000L, 0x30111000L, 99 | 0x20101004L, 0x30101004L, 0x20111004L, 0x30111004L, 100 | }, 101 | { 102 | /* for D bits (numbered as per FIPS 46) 8 9 11 12 13 14 */ 103 | 0x00000000L, 0x08000000L, 0x00000008L, 0x08000008L, 104 | 0x00000400L, 0x08000400L, 0x00000408L, 0x08000408L, 105 | 0x00020000L, 0x08020000L, 0x00020008L, 0x08020008L, 106 | 0x00020400L, 0x08020400L, 0x00020408L, 0x08020408L, 107 | 0x00000001L, 0x08000001L, 0x00000009L, 0x08000009L, 108 | 0x00000401L, 0x08000401L, 0x00000409L, 0x08000409L, 109 | 0x00020001L, 0x08020001L, 0x00020009L, 0x08020009L, 110 | 0x00020401L, 0x08020401L, 0x00020409L, 0x08020409L, 111 | 0x02000000L, 0x0A000000L, 0x02000008L, 0x0A000008L, 112 | 0x02000400L, 0x0A000400L, 0x02000408L, 0x0A000408L, 113 | 0x02020000L, 0x0A020000L, 0x02020008L, 0x0A020008L, 114 | 0x02020400L, 0x0A020400L, 0x02020408L, 0x0A020408L, 115 | 0x02000001L, 0x0A000001L, 0x02000009L, 0x0A000009L, 116 | 0x02000401L, 0x0A000401L, 0x02000409L, 0x0A000409L, 117 | 0x02020001L, 0x0A020001L, 0x02020009L, 0x0A020009L, 118 | 0x02020401L, 0x0A020401L, 0x02020409L, 0x0A020409L, 119 | }, 120 | { 121 | /* for D bits (numbered as per FIPS 46) 16 17 18 19 20 21 */ 122 | 0x00000000L, 0x00000100L, 0x00080000L, 0x00080100L, 123 | 0x01000000L, 0x01000100L, 0x01080000L, 0x01080100L, 124 | 0x00000010L, 0x00000110L, 0x00080010L, 0x00080110L, 125 | 0x01000010L, 0x01000110L, 0x01080010L, 0x01080110L, 126 | 0x00200000L, 0x00200100L, 0x00280000L, 0x00280100L, 127 | 0x01200000L, 0x01200100L, 0x01280000L, 0x01280100L, 128 | 0x00200010L, 0x00200110L, 0x00280010L, 0x00280110L, 129 | 0x01200010L, 0x01200110L, 0x01280010L, 0x01280110L, 130 | 0x00000200L, 0x00000300L, 0x00080200L, 0x00080300L, 131 | 0x01000200L, 0x01000300L, 0x01080200L, 0x01080300L, 132 | 0x00000210L, 0x00000310L, 0x00080210L, 0x00080310L, 133 | 0x01000210L, 0x01000310L, 0x01080210L, 0x01080310L, 134 | 0x00200200L, 0x00200300L, 0x00280200L, 0x00280300L, 135 | 0x01200200L, 0x01200300L, 0x01280200L, 0x01280300L, 136 | 0x00200210L, 0x00200310L, 0x00280210L, 0x00280310L, 137 | 0x01200210L, 0x01200310L, 0x01280210L, 0x01280310L, 138 | }, 139 | { 140 | /* for D bits (numbered as per FIPS 46) 22 23 24 25 27 28 */ 141 | 0x00000000L, 0x04000000L, 0x00040000L, 0x04040000L, 142 | 0x00000002L, 0x04000002L, 0x00040002L, 0x04040002L, 143 | 0x00002000L, 0x04002000L, 0x00042000L, 0x04042000L, 144 | 0x00002002L, 0x04002002L, 0x00042002L, 0x04042002L, 145 | 0x00000020L, 0x04000020L, 0x00040020L, 0x04040020L, 146 | 0x00000022L, 0x04000022L, 0x00040022L, 0x04040022L, 147 | 0x00002020L, 0x04002020L, 0x00042020L, 0x04042020L, 148 | 0x00002022L, 0x04002022L, 0x00042022L, 0x04042022L, 149 | 0x00000800L, 0x04000800L, 0x00040800L, 0x04040800L, 150 | 0x00000802L, 0x04000802L, 0x00040802L, 0x04040802L, 151 | 0x00002800L, 0x04002800L, 0x00042800L, 0x04042800L, 152 | 0x00002802L, 0x04002802L, 0x00042802L, 0x04042802L, 153 | 0x00000820L, 0x04000820L, 0x00040820L, 0x04040820L, 154 | 0x00000822L, 0x04000822L, 0x00040822L, 0x04040822L, 155 | 0x00002820L, 0x04002820L, 0x00042820L, 0x04042820L, 156 | 0x00002822L, 0x04002822L, 0x00042822L, 0x04042822L, 157 | } 158 | }; 159 | 160 | const DES_LONG TDES_SPtrans[8][64] = { 161 | { 162 | /* nibble 0 */ 163 | 0x02080800L, 0x00080000L, 0x02000002L, 0x02080802L, 164 | 0x02000000L, 0x00080802L, 0x00080002L, 0x02000002L, 165 | 0x00080802L, 0x02080800L, 0x02080000L, 0x00000802L, 166 | 0x02000802L, 0x02000000L, 0x00000000L, 0x00080002L, 167 | 0x00080000L, 0x00000002L, 0x02000800L, 0x00080800L, 168 | 0x02080802L, 0x02080000L, 0x00000802L, 0x02000800L, 169 | 0x00000002L, 0x00000800L, 0x00080800L, 0x02080002L, 170 | 0x00000800L, 0x02000802L, 0x02080002L, 0x00000000L, 171 | 0x00000000L, 0x02080802L, 0x02000800L, 0x00080002L, 172 | 0x02080800L, 0x00080000L, 0x00000802L, 0x02000800L, 173 | 0x02080002L, 0x00000800L, 0x00080800L, 0x02000002L, 174 | 0x00080802L, 0x00000002L, 0x02000002L, 0x02080000L, 175 | 0x02080802L, 0x00080800L, 0x02080000L, 0x02000802L, 176 | 0x02000000L, 0x00000802L, 0x00080002L, 0x00000000L, 177 | 0x00080000L, 0x02000000L, 0x02000802L, 0x02080800L, 178 | 0x00000002L, 0x02080002L, 0x00000800L, 0x00080802L, 179 | }, 180 | { 181 | /* nibble 1 */ 182 | 0x40108010L, 0x00000000L, 0x00108000L, 0x40100000L, 183 | 0x40000010L, 0x00008010L, 0x40008000L, 0x00108000L, 184 | 0x00008000L, 0x40100010L, 0x00000010L, 0x40008000L, 185 | 0x00100010L, 0x40108000L, 0x40100000L, 0x00000010L, 186 | 0x00100000L, 0x40008010L, 0x40100010L, 0x00008000L, 187 | 0x00108010L, 0x40000000L, 0x00000000L, 0x00100010L, 188 | 0x40008010L, 0x00108010L, 0x40108000L, 0x40000010L, 189 | 0x40000000L, 0x00100000L, 0x00008010L, 0x40108010L, 190 | 0x00100010L, 0x40108000L, 0x40008000L, 0x00108010L, 191 | 0x40108010L, 0x00100010L, 0x40000010L, 0x00000000L, 192 | 0x40000000L, 0x00008010L, 0x00100000L, 0x40100010L, 193 | 0x00008000L, 0x40000000L, 0x00108010L, 0x40008010L, 194 | 0x40108000L, 0x00008000L, 0x00000000L, 0x40000010L, 195 | 0x00000010L, 0x40108010L, 0x00108000L, 0x40100000L, 196 | 0x40100010L, 0x00100000L, 0x00008010L, 0x40008000L, 197 | 0x40008010L, 0x00000010L, 0x40100000L, 0x00108000L, 198 | }, 199 | { 200 | /* nibble 2 */ 201 | 0x04000001L, 0x04040100L, 0x00000100L, 0x04000101L, 202 | 0x00040001L, 0x04000000L, 0x04000101L, 0x00040100L, 203 | 0x04000100L, 0x00040000L, 0x04040000L, 0x00000001L, 204 | 0x04040101L, 0x00000101L, 0x00000001L, 0x04040001L, 205 | 0x00000000L, 0x00040001L, 0x04040100L, 0x00000100L, 206 | 0x00000101L, 0x04040101L, 0x00040000L, 0x04000001L, 207 | 0x04040001L, 0x04000100L, 0x00040101L, 0x04040000L, 208 | 0x00040100L, 0x00000000L, 0x04000000L, 0x00040101L, 209 | 0x04040100L, 0x00000100L, 0x00000001L, 0x00040000L, 210 | 0x00000101L, 0x00040001L, 0x04040000L, 0x04000101L, 211 | 0x00000000L, 0x04040100L, 0x00040100L, 0x04040001L, 212 | 0x00040001L, 0x04000000L, 0x04040101L, 0x00000001L, 213 | 0x00040101L, 0x04000001L, 0x04000000L, 0x04040101L, 214 | 0x00040000L, 0x04000100L, 0x04000101L, 0x00040100L, 215 | 0x04000100L, 0x00000000L, 0x04040001L, 0x00000101L, 216 | 0x04000001L, 0x00040101L, 0x00000100L, 0x04040000L, 217 | }, 218 | { 219 | /* nibble 3 */ 220 | 0x00401008L, 0x10001000L, 0x00000008L, 0x10401008L, 221 | 0x00000000L, 0x10400000L, 0x10001008L, 0x00400008L, 222 | 0x10401000L, 0x10000008L, 0x10000000L, 0x00001008L, 223 | 0x10000008L, 0x00401008L, 0x00400000L, 0x10000000L, 224 | 0x10400008L, 0x00401000L, 0x00001000L, 0x00000008L, 225 | 0x00401000L, 0x10001008L, 0x10400000L, 0x00001000L, 226 | 0x00001008L, 0x00000000L, 0x00400008L, 0x10401000L, 227 | 0x10001000L, 0x10400008L, 0x10401008L, 0x00400000L, 228 | 0x10400008L, 0x00001008L, 0x00400000L, 0x10000008L, 229 | 0x00401000L, 0x10001000L, 0x00000008L, 0x10400000L, 230 | 0x10001008L, 0x00000000L, 0x00001000L, 0x00400008L, 231 | 0x00000000L, 0x10400008L, 0x10401000L, 0x00001000L, 232 | 0x10000000L, 0x10401008L, 0x00401008L, 0x00400000L, 233 | 0x10401008L, 0x00000008L, 0x10001000L, 0x00401008L, 234 | 0x00400008L, 0x00401000L, 0x10400000L, 0x10001008L, 235 | 0x00001008L, 0x10000000L, 0x10000008L, 0x10401000L, 236 | }, 237 | { 238 | /* nibble 4 */ 239 | 0x08000000L, 0x00010000L, 0x00000400L, 0x08010420L, 240 | 0x08010020L, 0x08000400L, 0x00010420L, 0x08010000L, 241 | 0x00010000L, 0x00000020L, 0x08000020L, 0x00010400L, 242 | 0x08000420L, 0x08010020L, 0x08010400L, 0x00000000L, 243 | 0x00010400L, 0x08000000L, 0x00010020L, 0x00000420L, 244 | 0x08000400L, 0x00010420L, 0x00000000L, 0x08000020L, 245 | 0x00000020L, 0x08000420L, 0x08010420L, 0x00010020L, 246 | 0x08010000L, 0x00000400L, 0x00000420L, 0x08010400L, 247 | 0x08010400L, 0x08000420L, 0x00010020L, 0x08010000L, 248 | 0x00010000L, 0x00000020L, 0x08000020L, 0x08000400L, 249 | 0x08000000L, 0x00010400L, 0x08010420L, 0x00000000L, 250 | 0x00010420L, 0x08000000L, 0x00000400L, 0x00010020L, 251 | 0x08000420L, 0x00000400L, 0x00000000L, 0x08010420L, 252 | 0x08010020L, 0x08010400L, 0x00000420L, 0x00010000L, 253 | 0x00010400L, 0x08010020L, 0x08000400L, 0x00000420L, 254 | 0x00000020L, 0x00010420L, 0x08010000L, 0x08000020L, 255 | }, 256 | { 257 | /* nibble 5 */ 258 | 0x80000040L, 0x00200040L, 0x00000000L, 0x80202000L, 259 | 0x00200040L, 0x00002000L, 0x80002040L, 0x00200000L, 260 | 0x00002040L, 0x80202040L, 0x00202000L, 0x80000000L, 261 | 0x80002000L, 0x80000040L, 0x80200000L, 0x00202040L, 262 | 0x00200000L, 0x80002040L, 0x80200040L, 0x00000000L, 263 | 0x00002000L, 0x00000040L, 0x80202000L, 0x80200040L, 264 | 0x80202040L, 0x80200000L, 0x80000000L, 0x00002040L, 265 | 0x00000040L, 0x00202000L, 0x00202040L, 0x80002000L, 266 | 0x00002040L, 0x80000000L, 0x80002000L, 0x00202040L, 267 | 0x80202000L, 0x00200040L, 0x00000000L, 0x80002000L, 268 | 0x80000000L, 0x00002000L, 0x80200040L, 0x00200000L, 269 | 0x00200040L, 0x80202040L, 0x00202000L, 0x00000040L, 270 | 0x80202040L, 0x00202000L, 0x00200000L, 0x80002040L, 271 | 0x80000040L, 0x80200000L, 0x00202040L, 0x00000000L, 272 | 0x00002000L, 0x80000040L, 0x80002040L, 0x80202000L, 273 | 0x80200000L, 0x00002040L, 0x00000040L, 0x80200040L, 274 | }, 275 | { 276 | /* nibble 6 */ 277 | 0x00004000L, 0x00000200L, 0x01000200L, 0x01000004L, 278 | 0x01004204L, 0x00004004L, 0x00004200L, 0x00000000L, 279 | 0x01000000L, 0x01000204L, 0x00000204L, 0x01004000L, 280 | 0x00000004L, 0x01004200L, 0x01004000L, 0x00000204L, 281 | 0x01000204L, 0x00004000L, 0x00004004L, 0x01004204L, 282 | 0x00000000L, 0x01000200L, 0x01000004L, 0x00004200L, 283 | 0x01004004L, 0x00004204L, 0x01004200L, 0x00000004L, 284 | 0x00004204L, 0x01004004L, 0x00000200L, 0x01000000L, 285 | 0x00004204L, 0x01004000L, 0x01004004L, 0x00000204L, 286 | 0x00004000L, 0x00000200L, 0x01000000L, 0x01004004L, 287 | 0x01000204L, 0x00004204L, 0x00004200L, 0x00000000L, 288 | 0x00000200L, 0x01000004L, 0x00000004L, 0x01000200L, 289 | 0x00000000L, 0x01000204L, 0x01000200L, 0x00004200L, 290 | 0x00000204L, 0x00004000L, 0x01004204L, 0x01000000L, 291 | 0x01004200L, 0x00000004L, 0x00004004L, 0x01004204L, 292 | 0x01000004L, 0x01004200L, 0x01004000L, 0x00004004L, 293 | }, 294 | { 295 | /* nibble 7 */ 296 | 0x20800080L, 0x20820000L, 0x00020080L, 0x00000000L, 297 | 0x20020000L, 0x00800080L, 0x20800000L, 0x20820080L, 298 | 0x00000080L, 0x20000000L, 0x00820000L, 0x00020080L, 299 | 0x00820080L, 0x20020080L, 0x20000080L, 0x20800000L, 300 | 0x00020000L, 0x00820080L, 0x00800080L, 0x20020000L, 301 | 0x20820080L, 0x20000080L, 0x00000000L, 0x00820000L, 302 | 0x20000000L, 0x00800000L, 0x20020080L, 0x20800080L, 303 | 0x00800000L, 0x00020000L, 0x20820000L, 0x00000080L, 304 | 0x00800000L, 0x00020000L, 0x20000080L, 0x20820080L, 305 | 0x00020080L, 0x20000000L, 0x00000000L, 0x00820000L, 306 | 0x20800080L, 0x20020080L, 0x20020000L, 0x00800080L, 307 | 0x20820000L, 0x00000080L, 0x00800080L, 0x20020000L, 308 | 0x20820080L, 0x00800000L, 0x20800000L, 0x20000080L, 309 | 0x00820000L, 0x00020080L, 0x20020080L, 0x20800000L, 310 | 0x00000080L, 0x20820000L, 0x00820080L, 0x00000000L, 311 | 0x20000000L, 0x20800080L, 0x00020000L, 0x00820080L, 312 | } 313 | }; 314 | 315 | 316 | 317 | void TDES_set_key_unchecked(const_DES_cblock *key, DES_key_schedule *schedule) 318 | { 319 | static const int shifts2[16] = 320 | { 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0 }; 321 | register DES_LONG c, d, t, s, t2; 322 | register const unsigned char *in; 323 | register DES_LONG *k; 324 | register int i; 325 | 326 | 327 | k = &schedule->ks->deslong[0]; 328 | in = &(*key)[0]; 329 | 330 | c2l(in, c); 331 | c2l(in, d); 332 | 333 | /* 334 | * * do PC1 in 47 simple operations :-) Thanks to John Fletcher 335 | * * (john_fletcher@lccmail.ocf.llnl.gov) for the inspiration. :-) 336 | * */ 337 | PERM_OP(d, c, t, 4, 0x0f0f0f0fL); 338 | HPERM_OP(c, t, -2, 0xcccc0000L); 339 | HPERM_OP(d, t, -2, 0xcccc0000L); 340 | PERM_OP(d, c, t, 1, 0x55555555L); 341 | PERM_OP(c, d, t, 8, 0x00ff00ffL); 342 | PERM_OP(d, c, t, 1, 0x55555555L); 343 | d = (((d & 0x000000ffL) << 16L) | (d & 0x0000ff00L) | 344 | ((d & 0x00ff0000L) >> 16L) | ((c & 0xf0000000L) >> 4L)); 345 | c &= 0x0fffffffL; 346 | 347 | for (i = 0; i < ITERATIONS; i++) { 348 | if (shifts2[i]) { 349 | c = ((c >> 2L) | (c << 26L)); 350 | d = ((d >> 2L) | (d << 26L)); 351 | } else { 352 | c = ((c >> 1L) | (c << 27L)); 353 | d = ((d >> 1L) | (d << 27L)); 354 | } 355 | c &= 0x0fffffffL; 356 | d &= 0x0fffffffL; 357 | /* 358 | * * could be a few less shifts but I am to lazy at this point in time 359 | * * to investigate 360 | * */ 361 | s = des_skb[0][(c) & 0x3f] | 362 | des_skb[1][((c >> 6L) & 0x03) | ((c >> 7L) & 0x3c)] | 363 | des_skb[2][((c >> 13L) & 0x0f) | ((c >> 14L) & 0x30)] | 364 | des_skb[3][((c >> 20L) & 0x01) | ((c >> 21L) & 0x06) | 365 | ((c >> 22L) & 0x38)]; 366 | t = des_skb[4][(d) & 0x3f] | 367 | des_skb[5][((d >> 7L) & 0x03) | ((d >> 8L) & 0x3c)] | 368 | des_skb[6][(d >> 15L) & 0x3f] | 369 | des_skb[7][((d >> 21L) & 0x0f) | ((d >> 22L) & 0x30)]; 370 | 371 | /* table contained 0213 4657 */ 372 | t2 = ((t << 16L) | (s & 0x0000ffffL)) & 0xffffffffL; 373 | *(k++) = ROTATE(t2, 30) & 0xffffffffL; 374 | 375 | t2 = ((s >> 16L) | (t & 0xffff0000L)); 376 | *(k++) = ROTATE(t2, 26) & 0xffffffffL; 377 | } 378 | } 379 | 380 | 381 | void DES_ecb3_encrypt(const_DES_cblock *input, DES_cblock *output, 382 | DES_key_schedule *ks1, DES_key_schedule *ks2, 383 | DES_key_schedule *ks3, int enc) 384 | { 385 | register DES_LONG l0, l1; 386 | DES_LONG ll[2]; 387 | const unsigned char *in = &(*input)[0]; 388 | unsigned char *out = &(*output)[0]; 389 | 390 | c2l(in, l0); 391 | c2l(in, l1); 392 | ll[0] = l0; 393 | ll[1] = l1; 394 | 395 | if (enc) 396 | TDES_encrypt3(ll, ks1, ks2, ks3); 397 | else 398 | TDES_decrypt3(ll, ks1, ks2, ks3); 399 | l0 = ll[0]; 400 | l1 = ll[1]; 401 | l2c(l0, out); 402 | l2c(l1, out); 403 | } 404 | 405 | 406 | void TDES_encrypt1(DES_LONG *data, DES_key_schedule *ks, int enc) 407 | { 408 | register DES_LONG l, r, t, u; 409 | /* 410 | * #ifdef DES_PTR 411 | * register const unsigned char *des_SP = (const unsigned char *)DES_SPtrans; 412 | * #endif 413 | * #ifndef DES_UNROLL 414 | * 415 | * #endif*/ 416 | register int i; 417 | register DES_LONG *s; 418 | 419 | r = data[0]; 420 | l = data[1]; 421 | 422 | IP(r, l); 423 | /* 424 | * * Things have been modified so that the initial rotate is done outside 425 | * * the loop. This required the DES_SPtrans values in sp.h to be rotated 426 | * * 1 bit to the right. One perl script later and things have a 5% speed 427 | * * up on a sparc2. Thanks to Richard Outerbridge 428 | * * <71755.204@CompuServe.COM> for pointing this out. 429 | * */ 430 | /* clear the top bits on machines with 8byte longs */ 431 | /* shift left by 2 */ 432 | r = ROTATE(r, 29) & 0xffffffffL; 433 | l = ROTATE(l, 29) & 0xffffffffL; 434 | 435 | s = ks->ks->deslong; 436 | /* 437 | * * I don't know if it is worth the effort of loop unrolling the inner 438 | * * loop 439 | * */ 440 | if (enc) { 441 | 442 | for (i = 0; i < 32; i += 4) { 443 | D_ENCRYPT(l, r, i + 0); /* 1 */ 444 | D_ENCRYPT(r, l, i + 2); /* 2 */ 445 | } 446 | } else { 447 | 448 | for (i = 30; i > 0; i -= 4) { 449 | D_ENCRYPT(l, r, i - 0); /* 16 */ 450 | D_ENCRYPT(r, l, i - 2); /* 15 */ 451 | } 452 | } 453 | 454 | /* rotate and clear the top bits on machines with 8byte longs */ 455 | l = ROTATE(l, 3) & 0xffffffffL; 456 | r = ROTATE(r, 3) & 0xffffffffL; 457 | 458 | FP(r, l); 459 | data[0] = l; 460 | data[1] = r; 461 | l = r = t = u = 0; 462 | } 463 | 464 | void TDES_encrypt2(DES_LONG *data, DES_key_schedule *ks, int enc) 465 | { 466 | register DES_LONG l, r, t, u; 467 | /* 468 | * #ifdef DES_PTR 469 | * register const unsigned char *des_SP = (const unsigned char *)DES_SPtrans; 470 | * #endif 471 | * #ifndef DES_UNROLL 472 | * register int i; 473 | * #endif*/ 474 | register int i; 475 | register DES_LONG *s; 476 | 477 | r = data[0]; 478 | l = data[1]; 479 | 480 | /* 481 | * * Things have been modified so that the initial rotate is done outside 482 | * * the loop. This required the DES_SPtrans values in sp.h to be rotated 483 | * * 1 bit to the right. One perl script later and things have a 5% speed 484 | * * up on a sparc2. Thanks to Richard Outerbridge 485 | * * <71755.204@CompuServe.COM> for pointing this out. 486 | * */ 487 | /* clear the top bits on machines with 8byte longs */ 488 | r = ROTATE(r, 29) & 0xffffffffL; 489 | l = ROTATE(l, 29) & 0xffffffffL; 490 | 491 | s = ks->ks->deslong; 492 | /* 493 | * * I don't know if it is worth the effort of loop unrolling the inner 494 | * * loop 495 | * */ 496 | if (enc) { 497 | 498 | for (i = 0; i < 32; i += 4) { 499 | D_ENCRYPT(l, r, i + 0); /* 1 */ 500 | D_ENCRYPT(r, l, i + 2); /* 2 */ 501 | } 502 | 503 | } else { 504 | 505 | for (i = 30; i > 0; i -= 4) { 506 | D_ENCRYPT(l, r, i - 0); /* 16 */ 507 | D_ENCRYPT(r, l, i - 2); /* 15 */ 508 | } 509 | 510 | } 511 | /* rotate and clear the top bits on machines with 8byte longs */ 512 | data[0] = ROTATE(l, 3) & 0xffffffffL; 513 | data[1] = ROTATE(r, 3) & 0xffffffffL; 514 | l = r = t = u = 0; 515 | } 516 | 517 | void TDES_encrypt3(DES_LONG *data, DES_key_schedule *ks1, 518 | DES_key_schedule *ks2, DES_key_schedule *ks3) 519 | { 520 | register DES_LONG l, r; 521 | 522 | l = data[0]; 523 | r = data[1]; 524 | IP(l, r); 525 | data[0] = l; 526 | data[1] = r; 527 | TDES_encrypt2((DES_LONG *)data, ks1, DES_ENCRYPT); 528 | TDES_encrypt2((DES_LONG *)data, ks2, DES_DECRYPT); 529 | TDES_encrypt2((DES_LONG *)data, ks3, DES_ENCRYPT); 530 | l = data[0]; 531 | r = data[1]; 532 | FP(r, l); 533 | data[0] = l; 534 | data[1] = r; 535 | } 536 | 537 | void TDES_decrypt3(DES_LONG *data, DES_key_schedule *ks1, 538 | DES_key_schedule *ks2, DES_key_schedule *ks3) 539 | { 540 | register DES_LONG l, r; 541 | 542 | l = data[0]; 543 | r = data[1]; 544 | IP(l, r); 545 | data[0] = l; 546 | data[1] = r; 547 | TDES_encrypt2((DES_LONG *)data, ks3, DES_DECRYPT); 548 | TDES_encrypt2((DES_LONG *)data, ks2, DES_ENCRYPT); 549 | TDES_encrypt2((DES_LONG *)data, ks1, DES_DECRYPT); 550 | l = data[0]; 551 | r = data[1]; 552 | FP(r, l); 553 | data[0] = l; 554 | data[1] = r; 555 | } 556 | -------------------------------------------------------------------------------- /crypto_proc/encrypto.cpp: -------------------------------------------------------------------------------- 1 | #include "encrypto.h" 2 | //#include "lock.h" 3 | //#include "ngx_3des_crypto.h" 4 | 5 | CCRYPTO* CCRYPTO::m_pInstance = NULL; 6 | CThreadMutex* CCRYPTO::m_lock = NULL; 7 | 8 | static const std::string base64_chars = 9 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 10 | "abcdefghijklmnopqrstuvwxyz" 11 | "0123456789+/"; 12 | 13 | static inline bool is_base64(unsigned char c) 14 | { 15 | return (isalnum(c) || (c == '+') || (c == '/')); 16 | } 17 | 18 | CCRYPTO::CCRYPTO() 19 | { 20 | memset(g_CurrentPath, '\0', MAX_PATH); 21 | const char *TRIPLE_DES_KEY = "000000000000000000000000000000000000000000000000"; 22 | Crypt_3Des = new Des(TRIPLE_DES_KEY); 23 | } 24 | 25 | CCRYPTO::~CCRYPTO() 26 | { 27 | 28 | } 29 | 30 | CCRYPTO* CCRYPTO::GetInstance() 31 | { 32 | if (m_pInstance == NULL) { 33 | m_lock->lock(); 34 | if (m_pInstance == NULL) { 35 | m_pInstance = new CCRYPTO(); 36 | } 37 | m_lock->unlock(); 38 | } 39 | 40 | return m_pInstance; 41 | } 42 | 43 | void CCRYPTO::ListDir(string path) 44 | { 45 | 46 | } 47 | 48 | void CCRYPTO::Crypt_init_context(char *dirPath, int &index) 49 | { 50 | char *pRet = NULL; 51 | DIR *dirp = NULL; 52 | struct dirent *direntp = NULL; 53 | string childPath; 54 | string filePath; 55 | string curPath; 56 | childPath.clear(); 57 | if (dirPath == NULL) { 58 | memset(g_CurrentPath, 0, MAX_PATH); 59 | pRet = getcwd(g_CurrentPath, MAX_PATH); 60 | if (pRet == NULL) { 61 | 62 | } 63 | childPath += g_CurrentPath; 64 | if ((dirp = opendir(g_CurrentPath)) == NULL) { 65 | 66 | } 67 | } else { 68 | childPath += dirPath; 69 | if ((dirp = opendir(dirPath)) == NULL) { 70 | cout << dirPath << endl; 71 | 72 | } 73 | } 74 | 75 | childPath += "/"; 76 | while ((direntp = readdir(dirp)) != NULL) { 77 | if (strcmp(direntp->d_name, ".") == 0 || 78 | strcmp(direntp->d_name, "..") == 0 || 79 | strcmp(direntp->d_name, "crypt_java") == 0) { 80 | continue; 81 | } 82 | if (direntp->d_type & DT_DIR) { 83 | curPath.clear(); 84 | curPath = childPath; 85 | curPath += direntp->d_name; 86 | Crypt_init_context(const_cast(curPath.c_str()), index); 87 | 88 | } else if (direntp->d_type & DT_REG) { 89 | //if (strstr((*it).second->name, ".class") != NULL) { 90 | FILE_ENC_STRUCT *p = (FILE_ENC_STRUCT*)malloc(sizeof(FILE_ENC_STRUCT)); 91 | p->type = 0; 92 | memset(p->name, 0, MAX_PATH); 93 | filePath.clear(); 94 | filePath = childPath; 95 | filePath += direntp->d_name; 96 | strcpy(p->name, filePath.c_str()); 97 | m_ReadyFileMap.insert(map::value_type(index, p)); 98 | index++; 99 | //} 100 | } 101 | 102 | /* 103 | FILE_ENC_STRUCT *p = (FILE_ENC_STRUCT*)malloc(sizeof(FILE_ENC_STRUCT)); 104 | p->type = 0; 105 | memset(p->name, 0, 128); 106 | strcpy(p->name, direntp->d_name); 107 | m_ReadyFileMap.insert(map::value_type(i, p)); 108 | i++; 109 | */ 110 | } 111 | if (direntp == NULL) { 112 | closedir(dirp); 113 | } 114 | } 115 | 116 | int CCRYPTO::User_command_prompt() 117 | { 118 | int nRet = 0; 119 | int nRun = 0; 120 | cout << "功能:" << endl; 121 | cout << "1 加密" << endl; 122 | cout << "2 解密" << endl; 123 | cout << "0 退出" << endl; 124 | cout << "请选择数字:"; 125 | cin >> nRet; 126 | if (nRet == 1) { 127 | cout << "请选择加密运行方式:" << endl;; 128 | cout << "1 手动(人工选择需要加密的文件)" << endl; 129 | cout << "2 自动(自动对所有.class文件加密)" << endl; 130 | cout << "请选择数字:"; 131 | cin >> nRun; 132 | if (nRun == 1) { 133 | map::iterator it; 134 | cout << "请按数字选择您想要加密的文件:" << endl; 135 | for (it = m_ReadyFileMap.begin(); it != m_ReadyFileMap.end(); it++) { 136 | cout << (*it).first << " " << (*it).second->name << endl; 137 | } 138 | int cmd; 139 | while (1) { 140 | cout << "请选择数字(0是选择完毕):"; 141 | cin >> cmd; 142 | it = m_ReadyFileMap.begin(); 143 | it = m_ReadyFileMap.find(cmd); 144 | if (it != m_ReadyFileMap.end()) { 145 | if ((*it).second->type == 0) { 146 | if (strstr((*it).second->name, ".class") != NULL) { 147 | (m_ReadyFileMap[cmd])->type = 2; 148 | } else { 149 | (m_ReadyFileMap[cmd])->type = 1; 150 | } 151 | } else { 152 | cout << "已选择,请重新选择!" << endl; 153 | continue; 154 | } 155 | } 156 | else { 157 | if (cmd != 0) { 158 | cout << "请重新选择!" << endl; 159 | } 160 | } 161 | if (cmd == 0) { 162 | cout << "选择完毕" << endl; 163 | break; 164 | } 165 | } 166 | } 167 | else { 168 | map::iterator it; 169 | for (it = m_ReadyFileMap.begin(); it != m_ReadyFileMap.end(); it++) { 170 | if ((*it).second->type == 0) { 171 | if (strstr((*it).second->name, ".class") != NULL) { 172 | (m_ReadyFileMap[(*it).first])->type = 2; 173 | } 174 | } 175 | } 176 | } 177 | } else if (nRet == 2) { 178 | 179 | } 180 | else { 181 | } 182 | 183 | return nRet; 184 | } 185 | 186 | int CCRYPTO::Crypt_recyle_res() 187 | { 188 | if (m_ReadyFileMap.empty()) { 189 | return 0; 190 | } 191 | map::iterator it; 192 | for (it = m_ReadyFileMap.begin(); it != m_ReadyFileMap.end(); it++) { 193 | free(it->second); 194 | } 195 | m_ReadyFileMap.clear(); 196 | 197 | return 0; 198 | } 199 | 200 | void CCRYPTO::Encrypto_data() 201 | { 202 | long nSize; 203 | map::iterator it; 204 | for (it = m_ReadyFileMap.begin(); it != m_ReadyFileMap.end(); it++) { 205 | if ((*it).second->type != 0) { 206 | char sTmpPath[1024] = {0}; 207 | sprintf(sTmpPath, "%s", (*it).second->name); 208 | FILE *pFile = NULL; 209 | pFile = fopen(sTmpPath, "rb"); 210 | fseek(pFile, 0, SEEK_END); 211 | nSize = 0; 212 | nSize = ftell(pFile); 213 | rewind(pFile); 214 | long nFileFlag = 0; 215 | fread(&nFileFlag, sizeof(long), 1, pFile); 216 | if (ntohl(nFileFlag) == 0x1100) { 217 | cout << (*it).second->name << " 已加密过" << endl; 218 | fclose(pFile); 219 | continue; 220 | } 221 | rewind(pFile); 222 | 223 | char *pBuffer = (char*)malloc(nSize + 1); 224 | memset(pBuffer, 0, nSize + 1); 225 | string encoded; 226 | if ((*it).second->type == 1) { 227 | long nLen = 0; 228 | long nRSize = nSize; 229 | while (nLen < nSize) { 230 | long nRlen = fread(pBuffer + nLen, sizeof(char), nRSize, pFile); 231 | if (nRlen > 0) { 232 | nRSize -= nRlen; 233 | nLen += nRlen; 234 | } else 235 | break; 236 | } 237 | if (nLen < nSize) { 238 | free(pBuffer); 239 | fclose(pFile); 240 | continue; 241 | } 242 | fclose(pFile); 243 | 244 | string src_str(pBuffer); 245 | //TODO:3des 246 | encoded = Encrypto_des(src_str); 247 | 248 | } else { 249 | char ch; 250 | int i = 0; 251 | while (!feof(pFile)) { 252 | pBuffer[i] = fgetc(pFile); 253 | i++; 254 | } 255 | /* 256 | while(EOF != (ch=fgetc(pFile))) { 257 | pBuffer[i] = ch; 258 | i++; 259 | } 260 | */ 261 | fclose(pFile); 262 | 263 | int count = i; 264 | i = 0; 265 | string src_str; 266 | while (i < count - 1) { 267 | src_str += pBuffer[i]; 268 | i++; 269 | } 270 | 271 | //TODO:base64 272 | encoded = Encrypto_base64(reinterpret_cast(src_str.c_str()), src_str.length()); 273 | 274 | } 275 | 276 | if (encoded.empty()) { 277 | 278 | } 279 | char *enc_buf = (char*)malloc(encoded.size() + 1); 280 | memset(enc_buf, 0, encoded.size() + 1); 281 | memcpy(enc_buf, encoded.c_str(), encoded.size()); 282 | 283 | pFile = NULL; 284 | pFile = fopen(sTmpPath, "wb"); 285 | fseek(pFile, 0, SEEK_SET); 286 | long fileFlag = htonl(0x1100); 287 | fwrite(&fileFlag, sizeof(long), 1, pFile); 288 | long nLen = 0; 289 | long nWFileSize = encoded.size(); 290 | while (nLen < encoded.size()) { 291 | long nWlen = fwrite(enc_buf + nLen, sizeof(char), nWFileSize, pFile); 292 | if (nWlen <= 0) { 293 | break; 294 | } 295 | nWFileSize -= nWlen; 296 | nLen += nWlen; 297 | } 298 | free(enc_buf); 299 | free(pBuffer); 300 | enc_buf = NULL; 301 | pBuffer = NULL; 302 | fclose(pFile); 303 | cout << (*it).second->name << " 文件加密成功!" << endl; 304 | } 305 | } 306 | } 307 | 308 | void CCRYPTO::Decrypto_data(char *dirPath) 309 | { 310 | DIR *dirp; 311 | char *pRet = NULL; 312 | struct dirent *direntp; 313 | string childPath, curPath; 314 | childPath.clear(); 315 | if (dirPath == NULL) { 316 | memset(g_CurrentPath, 0, MAX_PATH); 317 | pRet = getcwd(g_CurrentPath, MAX_PATH); 318 | if (pRet == NULL) { 319 | 320 | } 321 | childPath += g_CurrentPath; 322 | if ((dirp = opendir(g_CurrentPath)) == NULL) { 323 | 324 | } 325 | } else { 326 | childPath += dirPath; 327 | if ((dirp = opendir(dirPath)) == NULL) { 328 | cout << dirPath << endl; 329 | 330 | } 331 | } 332 | 333 | childPath += "/"; 334 | while ((direntp = readdir(dirp)) != NULL) { 335 | if (strcmp(direntp->d_name, ".") == 0 || 336 | strcmp(direntp->d_name, "..") == 0 || 337 | strcmp(direntp->d_name, "crypt_java") == 0) { 338 | continue; 339 | } 340 | if (direntp->d_type & DT_DIR) { 341 | curPath.clear(); 342 | curPath = childPath; 343 | curPath += direntp->d_name; 344 | Decrypto_data(const_cast(curPath.c_str())); 345 | 346 | } else if (direntp->d_type & DT_REG) { 347 | if (strstr(direntp->d_name, ".class") == NULL) { 348 | continue; 349 | } 350 | 351 | char sTmpPath[1024] = {0}; 352 | sprintf(sTmpPath, "%s%s", childPath.c_str(), direntp->d_name); 353 | FILE *pFile = NULL; 354 | pFile = fopen(sTmpPath, "rb"); 355 | fseek(pFile, 0, SEEK_END); 356 | long nFileSize = ftell(pFile); 357 | rewind(pFile); 358 | long nFileFlag = 0; 359 | fread(&nFileFlag, sizeof(long), 1, pFile); 360 | if (ntohl(nFileFlag) != 0x1100) { 361 | fclose(pFile); 362 | continue; 363 | } 364 | nFileSize = nFileSize - sizeof(long); 365 | char *pBuffer = (char*)malloc(nFileSize + 1); 366 | memset(pBuffer, 0, nFileSize + 1); 367 | long nLen = 0; 368 | long nRSize = nFileSize; 369 | while (nLen < nFileSize) { 370 | int nRlen = fread(pBuffer + nLen, sizeof(char), nRSize, pFile); 371 | if (nRlen > 0) { 372 | nRSize -= nRlen; 373 | nLen += nRlen; 374 | } else 375 | break; 376 | } 377 | fclose(pFile); 378 | 379 | string enc_str(pBuffer); 380 | string dec_out; 381 | if (strstr(direntp->d_name, ".class") == NULL) { 382 | //TODO:dec_3des 383 | dec_out = Decrypto_des(enc_str); 384 | 385 | char *dec_buf = (char*)malloc(dec_out.size() + 1); 386 | memset(dec_buf, 0, dec_out.size() + 1); 387 | memcpy(dec_buf, dec_out.c_str(), dec_out.size()); 388 | 389 | pFile = NULL; 390 | pFile = fopen(sTmpPath, "wb"); 391 | nLen = 0; 392 | long nWSize = dec_out.size(); 393 | while (nLen < dec_out.size()) { 394 | long nWlen = fwrite(dec_buf + nLen, sizeof(char), nWSize, pFile); 395 | if (nWlen <= 0) { 396 | break; 397 | } 398 | nWSize -= nWlen; 399 | nLen += nWlen; 400 | } 401 | free(dec_buf); 402 | dec_buf = NULL; 403 | 404 | } else { 405 | //TODO:dec_base64 406 | dec_out = Decrypto_base64(enc_str); 407 | //ofstream out("out.txt"); 408 | //out << dec_out << endl; 409 | 410 | char *dec_buf = (char*)malloc(dec_out.size() + 1); 411 | memset(dec_buf, 0, dec_out.size() + 1); 412 | memcpy(dec_buf, dec_out.c_str(), dec_out.size()); 413 | 414 | pFile = NULL; 415 | pFile = fopen(sTmpPath, "wb"); 416 | fseek(pFile, 0, SEEK_SET); 417 | nLen = 0; 418 | long nWSize = dec_out.size(); 419 | while (nLen < dec_out.size()) { 420 | long nWlen = fwrite(dec_buf + nLen, sizeof(char), nWSize, pFile); 421 | if (nWlen <= 0) { 422 | break; 423 | } 424 | nWSize -= nWlen; 425 | nLen += nWlen; 426 | } 427 | free(dec_buf); 428 | dec_buf = NULL; 429 | 430 | } 431 | 432 | free(pBuffer); 433 | pBuffer = NULL; 434 | fclose(pFile); 435 | cout << sTmpPath << " 文件解密成功!" << endl; 436 | } 437 | } 438 | if (direntp == NULL) { 439 | closedir(dirp); 440 | } 441 | } 442 | 443 | string CCRYPTO::Encrypto_des(string in) 444 | { 445 | return Crypt_3Des->desEncrypt(in); 446 | } 447 | 448 | string CCRYPTO::Decrypto_des(string in) 449 | { 450 | return Crypt_3Des->desDecrypt(in); 451 | } 452 | 453 | string CCRYPTO::Encrypto_base64(unsigned char const* bytes_to_encode, unsigned int in_len) 454 | { 455 | std::string ret; 456 | int i = 0; 457 | int j = 0; 458 | unsigned char char_array_3[3]; 459 | unsigned char char_array_4[4]; 460 | 461 | while (in_len--) { 462 | char_array_3[i++] = *(bytes_to_encode++); 463 | if (i == 3) { 464 | char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; 465 | char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); 466 | char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); 467 | char_array_4[3] = char_array_3[2] & 0x3f; 468 | 469 | for(i = 0; (i <4) ; i++) 470 | ret += base64_chars[char_array_4[i]]; 471 | i = 0; 472 | } 473 | } 474 | 475 | if (i) 476 | { 477 | for(j = i; j < 3; j++) 478 | char_array_3[j] = '\0'; 479 | 480 | char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; 481 | char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); 482 | char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); 483 | char_array_4[3] = char_array_3[2] & 0x3f; 484 | 485 | for (j = 0; (j < i + 1); j++) 486 | ret += base64_chars[char_array_4[j]]; 487 | 488 | while((i++ < 3)) 489 | ret += '='; 490 | 491 | } 492 | 493 | return ret; 494 | } 495 | 496 | string CCRYPTO::Decrypto_base64(string const& encoded_string) 497 | { 498 | int in_len = encoded_string.size(); 499 | int i = 0; 500 | int j = 0; 501 | int in_ = 0; 502 | unsigned char char_array_4[4], char_array_3[3]; 503 | std::string ret; 504 | 505 | while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) { 506 | char_array_4[i++] = encoded_string[in_]; in_++; 507 | if (i ==4) { 508 | for (i = 0; i <4; i++) 509 | char_array_4[i] = base64_chars.find(char_array_4[i]); 510 | 511 | char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); 512 | char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); 513 | char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; 514 | 515 | for (i = 0; (i < 3); i++) 516 | ret += char_array_3[i]; 517 | i = 0; 518 | } 519 | } 520 | 521 | if (i) { 522 | for (j = i; j <4; j++) 523 | char_array_4[j] = 0; 524 | 525 | for (j = 0; j <4; j++) 526 | char_array_4[j] = base64_chars.find(char_array_4[j]); 527 | 528 | char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); 529 | char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); 530 | char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; 531 | 532 | for (j = 0; (j < i - 1); j++) ret += char_array_3[j]; 533 | } 534 | 535 | return ret; 536 | } 537 | 538 | /* 539 | bool CCRYPTO::Encrypto_java() 540 | { 541 | map::iterator it; 542 | for (it = m_ReadyFileMap.begin(); it != m_ReadyFileMap.end(); it++) { 543 | if ((*it).second->type == 1) { 544 | char sTmpPath[512] = {0}; 545 | sprintf(sTmpPath, "%s/%s", g_CurrentPath, (*it).second->name); 546 | FILE *pFile = NULL; 547 | pFile = fopen(sTmpPath, "rb"); 548 | fseek(pFile, 0, SEEK_END); 549 | long nSize = 0; 550 | nSize = ftell(pFile); 551 | rewind(pFile); 552 | long nFileFlag = 0; 553 | fread(&nFileFlag, sizeof(long), 1, pFile); 554 | if (ntohl(nFileFlag) == 0x1100) { 555 | cout << (*it).second->name << " 已加密过" << endl; 556 | fclose(pFile); 557 | continue; 558 | } 559 | rewind(pFile); 560 | char *pBuffer = (char*)malloc(nSize + 1); 561 | memset(pBuffer, 0, nSize + 1); 562 | long nLen = 0; 563 | long nRSize = nSize; 564 | while (nLen < nSize) { 565 | char szBuffer[40960] = {0}; 566 | int nRlen = fread(szBuffer, sizeof(char), 40960, pFile); 567 | if (nRlen > 0) { 568 | memcpy(pBuffer, szBuffer, nRlen); 569 | nLen += nRlen; 570 | } else 571 | break; 572 | } 573 | if (nLen < nSize) { 574 | free(pBuffer); 575 | fclose(pFile); 576 | continue; 577 | } 578 | cout << "ready enc:"<< pBuffer << endl; 579 | fclose(pFile); 580 | 581 | pFile = NULL; 582 | pFile = fopen(sTmpPath, "wb"); 583 | if (pFile == NULL) 584 | return false; 585 | 586 | fseek(pFile, 0, SEEK_SET); 587 | long lFileType = htonl(0x1100); 588 | fwrite(&lFileType, sizeof(long), 1, pFile); 589 | 590 | unsigned char szEncodeKey[16] = {0}; 591 | unsigned char szEndData[16] = { 0xe1, 0x02, 0xa3, 0x04, 0x15, 0xb6, 0x07, 0x08, 0xc9, 0x0a, 0xab, 0x0c, 0x6d, 0x0e, 0x2f, 0x01 }; 592 | int nIndex = 0; 593 | for (nIndex = 0; nIndex < 16; nIndex++) 594 | { 595 | if (nIndex % 3 == 0) 596 | { 597 | szEncodeKey[nIndex] = (0x0f) & (szEndData[nIndex] >> 2); 598 | } 599 | else if (nIndex % 3 == 1) 600 | { 601 | szEncodeKey[nIndex] = (0x1f) & (szEndData[nIndex] >> 3); 602 | } 603 | else 604 | { 605 | szEncodeKey[nIndex] = (0x3f) & (szEndData[nIndex] >> 4); 606 | } 607 | } 608 | 609 | char uszEncodeData[4096] = {0}; 610 | int nEncodeLen = 0; 611 | int nBufferIndex = strlen(pBuffer); 612 | AES_CryptDataEVP((unsigned char*)pBuffer, nBufferIndex, szEncodeKey, (unsigned char*)uszEncodeData, &nEncodeLen); 613 | 614 | int nDataLen = 0; 615 | while (nEncodeLen > 0) 616 | { 617 | int nWriteLen = fwrite(uszEncodeData + nDataLen, sizeof(char), nEncodeLen, pFile); 618 | if (nWriteLen <= 0) 619 | break; 620 | nEncodeLen -= nWriteLen; 621 | nDataLen += nWriteLen; 622 | } 623 | 624 | fclose(pFile); 625 | } 626 | } 627 | return true; 628 | } 629 | 630 | bool CCRYPTO::Decrypto_java() 631 | { 632 | DIR *dirp; 633 | struct dirent *direntp; 634 | if ((dirp = opendir(g_CurrentPath)) == NULL) { 635 | 636 | } 637 | while ((direntp = readdir(dirp)) != NULL) { 638 | if (strcmp(direntp->d_name, ".") == 0 || 639 | strcmp(direntp->d_name, "..") == 0 || 640 | strcmp(direntp->d_name, "crypt_java") == 0) { 641 | continue; 642 | } 643 | char sTmpPath[512] = {0}; 644 | sprintf(sTmpPath, "%s/%s", g_CurrentPath, direntp->d_name); 645 | FILE *pFile = NULL; 646 | pFile = fopen(sTmpPath, "rb"); 647 | fseek(pFile, 0, SEEK_END); 648 | long nFileSize = ftell(pFile); 649 | if (nFileSize == 0) { 650 | fclose(pFile); 651 | continue; 652 | } 653 | rewind(pFile); 654 | 655 | long nFileFlag = 0; 656 | fread(&nFileFlag, sizeof(long), 1, pFile); 657 | if (ntohl(nFileFlag) != 0x1100) { 658 | fclose(pFile); 659 | continue; 660 | } 661 | nFileSize -= sizeof(long); 662 | char *pBuffer = (char*)malloc(nFileSize + 1); 663 | memset(pBuffer, 0, nFileSize + 1); 664 | 665 | long nLen = 0; 666 | while (nLen < nFileSize) { 667 | char szBuffer[40960] = {0}; 668 | int nRlen = fread(szBuffer, sizeof(char), 40960, pFile); 669 | if (nRlen > 0) { 670 | memcpy(pBuffer, szBuffer, nRlen); 671 | nLen += nRlen; 672 | } else 673 | break; 674 | } 675 | fclose(pFile); 676 | if (nLen < nFileSize) { 677 | free(pBuffer); 678 | continue; 679 | } 680 | 681 | char* pszDecodeData = (char*)malloc(nFileSize * 2); 682 | if (pszDecodeData == NULL) { 683 | free(pBuffer); 684 | return false; 685 | } 686 | memset(pszDecodeData, 0, nFileSize * 2); 687 | 688 | unsigned char szEncodeKey[16] = {0}; 689 | unsigned char szEndData[16] = { 0xe1, 0x02, 0xa3, 0x04, 0x15, 0xb6, 0x07, 0x08, 0xc9, 0x0a, 0xab, 0x0c, 0x6d, 0x0e, 0x2f, 0x01 }; 690 | int nIndex = 0; 691 | for (nIndex = 0; nIndex < 16; nIndex++) 692 | { 693 | if (nIndex % 3 == 0) 694 | { 695 | szEncodeKey[nIndex] = (0x0f) & (szEndData[nIndex] >> 2); 696 | } 697 | else if (nIndex % 3 == 1) 698 | { 699 | szEncodeKey[nIndex] = (0x1f) & (szEndData[nIndex] >> 3); 700 | } 701 | else 702 | { 703 | szEncodeKey[nIndex] = (0x3f) & (szEndData[nIndex] >> 4); 704 | } 705 | } 706 | int nDecodeLen = 0; 707 | int nEncodeLen = 0; 708 | AES_DecryptDataEVP((unsigned char*)pBuffer, nFileSize, szEncodeKey, (unsigned char*)pszDecodeData, &nDecodeLen); 709 | if (nDecodeLen == 0) 710 | { 711 | free(pBuffer); 712 | free(pszDecodeData); 713 | return false; 714 | } 715 | free(pBuffer); 716 | 717 | pFile = NULL; 718 | pFile = fopen(sTmpPath, "wb"); 719 | if (pFile == NULL) 720 | return false; 721 | 722 | fseek(pFile, 0, SEEK_SET); 723 | int nDataLen = 0; 724 | while (nEncodeLen > 0) { 725 | int nWriteLen = fwrite(pszDecodeData + nDataLen, sizeof(char), nDecodeLen, pFile); 726 | if (nWriteLen <= 0) 727 | break; 728 | nEncodeLen -= nWriteLen; 729 | nDataLen += nWriteLen; 730 | } 731 | fclose(pFile); 732 | free(pszDecodeData); 733 | } 734 | if (direntp == NULL) { 735 | closedir(dirp); 736 | } 737 | return true; 738 | } 739 | 740 | void CCRYPTO::Encrypto_3des(char *in, long size, char *out, int *out_len) 741 | { 742 | int i = 0; 743 | int len = 0; 744 | int nlen = 0; 745 | int klen = 0; 746 | char ch = '\0'; 747 | 748 | const char *u_key = "ABCDEFGHIJKLMNOPQRSTUVWX"; 749 | 750 | unsigned char key[LEN_OF_KEY]; 751 | unsigned char tmp[1024] = {0}; 752 | unsigned char src[1024] = {0}; 753 | 754 | unsigned char block[8] = {0}; 755 | DES_key_schedule ks1, ks2, ks3; 756 | 757 | klen = strlen(u_key); 758 | memcpy(key, u_key, klen); 759 | memset(key + klen, 0x00, LEN_OF_KEY - klen); 760 | 761 | 762 | memcpy(block, key, sizeof(block)); 763 | TDES_set_key_unchecked((const_DES_cblock *)block, &ks1); 764 | 765 | memcpy(block, key + 8, sizeof(block)); 766 | TDES_set_key_unchecked((const_DES_cblock *)block, &ks2); 767 | 768 | memcpy(block, key + 16, sizeof(block)); 769 | TDES_set_key_unchecked((const_DES_cblock *)block, &ks3); 770 | 771 | //nlen = size; 772 | nlen = strlen(in); 773 | memcpy(src, in, nlen); 774 | 775 | len = (nlen / 8 + 1) * 8; 776 | 777 | ch = 8 - nlen % 8; 778 | memset(src + nlen, ch, (8 - nlen % 8)); 779 | 780 | for (i = 0; i < len; i += 8) { 781 | DES_ecb3_encrypt((const_DES_cblock *)(src + i), (DES_cblock *)(out + i), &ks1, &ks2, &ks3, DES_ENCRYPT); 782 | } 783 | *out_len = len; 784 | 785 | } 786 | 787 | void CCRYPTO::Decrypto_3des(char *in, int size, char *out) 788 | { 789 | int i = 0; 790 | int len = 0; 791 | int nlen = 0; 792 | int klen = 0; 793 | char ch = '\0'; 794 | 795 | const char *u_key = "ABCDEFGHIJKLMNOPQRSTUVWX"; 796 | 797 | unsigned char key[LEN_OF_KEY]; 798 | unsigned char tmp[1024] = {0}; 799 | unsigned char src[1024] = {0}; 800 | 801 | unsigned char block[8] = {0}; 802 | DES_key_schedule ks1, ks2, ks3; 803 | 804 | klen = strlen(u_key); 805 | memcpy(key, u_key, klen); 806 | memset(key + klen, 0x00, LEN_OF_KEY - klen); 807 | 808 | 809 | memcpy(block, key, sizeof(block)); 810 | TDES_set_key_unchecked((const_DES_cblock *)block, &ks1); 811 | 812 | memcpy(block, key + 8, sizeof(block)); 813 | TDES_set_key_unchecked((const_DES_cblock *)block, &ks2); 814 | 815 | memcpy(block, key + 16, sizeof(block)); 816 | TDES_set_key_unchecked((const_DES_cblock *)block, &ks3); 817 | 818 | //nlen = size; 819 | //nlen = strlen(in); 820 | //memcpy(src, in, nlen); 821 | 822 | //len = (nlen / 8 + 1) * 8; 823 | len = size; 824 | 825 | //ch = 8 - nlen % 8; 826 | //memset(src + nlen, ch, (8 - nlen % 8)); 827 | 828 | for (i = 0; i < 1000; i += 8) { 829 | DES_ecb3_encrypt((const_DES_cblock *)(in + i), (DES_cblock *)(out + i), &ks1, &ks2, &ks3, DES_DECRYPT); 830 | } 831 | 832 | for (i = 0; i < 1000; i++) { 833 | printf("%c", *(out + i)); 834 | } 835 | printf("\n"); 836 | } 837 | 838 | 839 | 840 | void CCRYPTO::Encrypto_aes(char *in, long size, char *out, unsigned int *out_len) 841 | { 842 | AES_KEY aes; 843 | unsigned char key[AES_BLOCK_SIZE]; 844 | unsigned char iv[AES_BLOCK_SIZE]; 845 | unsigned char* input_string; 846 | unsigned char* encrypt_string; 847 | //unsigned char* decrypt_string; 848 | unsigned int len; 849 | unsigned int i; 850 | 851 | len = 0; 852 | if ((strlen(in) + 1) % AES_BLOCK_SIZE == 0) { 853 | len = strlen(in) + 1; 854 | } else { 855 | len = ((strlen(in) + 1) / AES_BLOCK_SIZE + 1) * AES_BLOCK_SIZE; 856 | } 857 | 858 | 859 | input_string = (unsigned char*)calloc(len, sizeof(unsigned char)); 860 | if (input_string == NULL) { 861 | fprintf(stderr, "Unable to allocate memory for input_string\n"); 862 | exit(-1); 863 | } 864 | strncpy((char*)input_string, in, strlen(in)); 865 | 866 | memcpy(key, "192.168.2.155###", strlen("192.168.2.155###")); 867 | 868 | 869 | for (i=0; i