1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
| #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h>
#define MAX_INPUT_LEN 2048
typedef struct { unsigned char S[256]; unsigned char i; unsigned char j; } RC4_CTX;
void rc4_init(RC4_CTX* ctx, const unsigned char* key, size_t key_len) { int i; unsigned char j = 0; unsigned char temp;
for (i = 0; i < 256; i++) { ctx->S[i] = (unsigned char)i; }
ctx->i = 0; ctx->j = 0;
for (i = 0; i < 256; i++) { j = (unsigned char)(j + ctx->S[i] + key[i % key_len]); temp = ctx->S[i]; ctx->S[i] = ctx->S[j]; ctx->S[j] = temp; } }
unsigned char rc4_generate_byte(RC4_CTX* ctx) { unsigned char temp; ctx->i = (unsigned char)(ctx->i + 1); ctx->j = (unsigned char)(ctx->j + ctx->S[ctx->i]); temp = ctx->S[ctx->i]; ctx->S[ctx->i] = ctx->S[ctx->j]; ctx->S[ctx->j] = temp; return ctx->S[(ctx->S[ctx->i] + ctx->S[ctx->j]) & 0xFF]; }
void rc4_crypt(RC4_CTX* ctx, const unsigned char* input, unsigned char* output, size_t len) { size_t n; for (n = 0; n < len; n++) { output[n] = input[n] ^ rc4_generate_byte(ctx); } }
unsigned char* hex2bin(const char* hex, size_t* len) { size_t hex_len = strlen(hex);
if (hex_len % 2 != 0) { fprintf(stderr, "错误: 无效的十六进制长度\n"); return NULL; }
*len = hex_len / 2; if (*len == 0) { return NULL; }
unsigned char* bin = (unsigned char*)malloc(*len); if (!bin) return NULL;
for (size_t i = 0; i < *len; i++) { char hex_byte[3] = { 0 }; hex_byte[0] = hex[i * 2]; hex_byte[1] = hex[i * 2 + 1];
char* endptr; unsigned long value = strtoul(hex_byte, &endptr, 16);
if (*endptr != '\0' || value > 255) { fprintf(stderr, "错误: 无效的十六进制字节 '%s'\n", hex_byte); free(bin); return NULL; }
bin[i] = (unsigned char)value; }
return bin; }
void get_input(const char* prompt, char* buffer, size_t max_len) { printf("%s", prompt); if (fgets(buffer, (int)max_len, stdin) == NULL) { buffer[0] = '\0'; return; }
size_t len = strlen(buffer); if (len > 0 && buffer[len - 1] == '\n') { buffer[len - 1] = '\0'; } }
int main() { char key[MAX_INPUT_LEN]; char hex_cipher[MAX_INPUT_LEN];
printf("=== RC4 解密工具 ===\n");
get_input("请输入密钥: ", key, sizeof(key)); get_input("请输入密文(HEX): ", hex_cipher, sizeof(hex_cipher));
for (size_t i = 0; hex_cipher[i]; i++) { if (!isxdigit((unsigned char)hex_cipher[i])) { fprintf(stderr, "错误: 无效的十六进制字符 '%c'\n", hex_cipher[i]); return 1; } }
size_t cipher_len; unsigned char* ciphertext = hex2bin(hex_cipher, &cipher_len); if (!ciphertext || cipher_len == 0) { fprintf(stderr, "错误: 十六进制转换失败\n"); return 1; }
unsigned char* decrypted = (unsigned char*)calloc(cipher_len + 1, 1); if (!decrypted) { fprintf(stderr, "错误: 内存分配失败\n"); free(ciphertext); return 1; }
RC4_CTX ctx; rc4_init(&ctx, (const unsigned char*)key, strlen(key)); rc4_crypt(&ctx, ciphertext, decrypted, cipher_len);
decrypted[cipher_len] = '\0';
printf("\n解密结果:\n"); printf("密钥: %s\n", key); printf("密文(HEX): %s\n", hex_cipher); printf("解密文本: %s\n", decrypted);
free(ciphertext); free(decrypted);
printf("\n按 Enter 键退出..."); getchar(); return 0; }
|