Loading... ## 数据流加密 ```cpp // #include "stdafx.h" #include <Windows.h> #include <time.h> struct CONFIG { BOOL antisandbox; BOOL autostart; unsigned char key[128]; }; void StreamCrypt(unsigned char* Data, unsigned long Length, unsigned char* Key, unsigned long KeyLength) { int i = 0, j = 0; unsigned char k[256] = { 0 }, s[256] = { 0 }; unsigned char tmp = 0; for (i = 0; i < 256; i++) { s[i] = i; k[i] = Key[i%KeyLength]; } for (i = 0; i < 256; i++) { j = (j + s[i] + k[i]) % 256; tmp = s[i]; s[i] = s[j]; s[j] = tmp; } int t = 0; i = 0, j = 0, tmp = 0; unsigned long l = 0; for (l = 0; l < Length; l++) { i = (i + 1) % 256; j = (j + s[i]) % 256; tmp = s[i]; s[i] = s[j]; s[j] = tmp; t = (s[i] + s[j]) % 256; Data[l] ^= s[t]; } } BYTE hexData[] = { 0xFC, 0xE8, 0x89, 0x00, 0x00, 0x00, 0x60, 0x89, 0xE5, 0x31, 0xD2, 0x64, 0x8B, 0x52, 0x30, 0x8B, 0x52, 0x0C, 0x8B, 0x52, 0x14, 0x8B, 0x72, 0x28, 0x0F, 0xB7, 0x4A, 0x26, 0x31, 0xFF, 0x31, 0xC0, }; int main() { CONFIG config = { 0 }; config.autostart = 1; config.antisandbox = 1; srand(time(0)); for (int i = 0; i < 128; i++) { memset(&config.key[i], rand() % 0xFF, 1); } DWORD dw = _countof(hexData); //加密 StreamCrypt(hexData , dw, config.key, 128); //解密 StreamCrypt(hexData, dw, config.key, 128); return 0; } ``` ## CR4 加密 ```cpp #include <iostream> #include <string.h> using namespace std; //char -> 16 void char_2_Hex(char* Char, char* Hex) { int length = strlen(Char); for (size_t i = 0; i < length; i++) { int tmp = int(Char[i]); if(Char[i] < 0) tmp = (-1) * Char[i] + 128; int high = tmp / 16; int low = tmp % 16; char HIHG; char LOW; if(high >= 10) HIHG = char(high - 10 + 65); else HIHG = char(high + 48); if(low >= 10) LOW = char(low - 10 + 65); else LOW = char(low + 48); Hex[2*i] = HIHG; Hex[2*i+1] = LOW; } } //16 -> char void Hex_2_char(char*Hex, char* Char) { int length = strlen(Hex) / 2; for (size_t i = 0; i < length; i++) { int high; int low; if(int(Hex[2*i])>=65) high = int(Hex[2*i] - 65 + 10); else high = int(Hex[2*i]- 48); if(int(Hex[2*i+1])>=65) low = int(Hex[2*i+1] - 65 + 10); else low = int(Hex[2*i+1] - 48); Char[i] = char(high*16 + low); } } char* RC4(char* C,char* key) { int S[256]; int T[256]; int count = 0; count = strlen(key); for(int i = 0; i < 256; i++) { S[i] = i; int tmp = i % count; T[i] = key[tmp]; } int j = 0; for(int i = 0; i < 256; i++) { j = (j + S[i] + T[i]) % 256; int tmp; tmp = S[j]; S[j] = S[i]; S[i] = tmp; } int length = 0; length = strlen(C); int i,j; i=0,j=0; for(int p = 0; p < length; p++) { i = (i + 1) % 256; j = (j + S[i]) % 256; int tmp; tmp = S[j]; S[j] = S[i]; S[i] = tmp; int k = S[(S[i] + S[j]) % 256]; C[p]=C[p]^k; } return C; } int main() { char A[1000] = "aaaaaaaaaaa; cout << "C =" << A <<endl; char key[256] = "abcde"; cout << "key = " << key <<endl; size_t count = 0; count = strlen(A); char* B = RC4(A,key); char* M = new char[2*count]; char_2_Hex(B,M); cout<<"M = " << M<<endl; char* C = RC4(B,key); cout <<"C' = "<<C<<endl; system("pause"); return 0; } ``` 最后修改:2022 年 05 月 19 日 © 允许规范转载 赞 0 如果觉得我的文章对你有用,请随意赞赏