#include<stdio.h> #include<malloc.h> #include<stdlib.h> #include<string.h> #define LENGTH 12
int rot13(char *buf) { int length = strlen(buf);
for (int i = 0; i < length; i++) { int cases; if (buf[i] >= 'A'&&buf[i] <= 'Z') { cases = 65;
buf[i] = (buf[i] + 13) % (cases + 26); if (buf[i] <= 25) { buf[i] += cases; } } else if (buf[i] >= 'a'&&buf[i] <= 'z') { cases = 97;
buf[i] = (buf[i] + 13) % (cases + 26); if (buf[i] <= 25) { buf[i] += cases; } } }
return length; }
int base64_decode_table(char ch) { char temp = ch;
if (ch < 'A' || ch > 'Z') { if (ch <'a' || ch > 'z') { if (ch <= '/' || ch > '9') { if (ch == '+') { temp = 62; } else if (ch == '/') { temp = 63; } } else { temp = ch + 4; } } else { temp = ch - 71; } } else { temp = ch - 65; }
return temp; }
void base64_decode(char *str, char *buf, int length) { if (str[length - 1] == '=') { length--; } if (str[length - 1] == '=') { length--; }
int buf_length = 0; if (buf != NULL) { int i; for (i = 0; i < length / 4 * 4; i += 4) { if (str[i] == '\n') { i++; }
int temp = base64_decode_table(str[i]) << 2; buf[buf_length] = temp | ((base64_decode_table(str[i + 1]) & 0b00110000) >> 4);
temp = base64_decode_table(str[i + 1]) << 4; buf[buf_length + 1] = temp | (base64_decode_table(str[i + 2]) >> 2);
temp = base64_decode_table(str[i + 2]) << 6; buf[buf_length + 2] = temp | base64_decode_table(str[i + 3]);
buf_length += 3; }
int offset = length & 0b00000011; if (offset == 0b10) { int temp = base64_decode_table(str[i]) << 2; buf[buf_length++] = temp | ((base64_decode_table(str[i + 1]) & 0b00110000) >> 4); } else if (offset = 0b11) { int temp = base64_decode_table(str[i]) << 2; buf[buf_length] = temp | ((base64_decode_table(str[i + 1]) & 0b00110000) >> 4); temp = base64_decode_table(str[i + 1]) << 4; buf[buf_length + 1] = temp | (base64_decode_table(str[i + 2]) >> 2); buf_length += 2; } } else {
}
return buf_length; }
int judge(char *str, int str_length) { int x = 0; int result; for (int i = 0; i < str_length; i++) { result = i; x = 10 * x + (str[i] - '0'); if (x % (i + 1)) { exit(1); } }
return result; }
int decode1(char *out, char *str, int str_length) { char *v6 = out + 8; out[0] = '\0'; char *result = out; out[1] = '\0';
for (int i = 0; i < 256; i++) { result = v6 + i; *result = i; }
unsigned char v9 = 0; int v10 = 0; for (int i = 0; i < 256; i++) { char ch = v6[i]; v9 = v9 + ch + str[v10]; char v5 = v6[v9]; v6[v9] = ch; v6[i] = v5; result == ++v10; if (v10 >= str_length) { v10 = 0; } }
return result; }
int decode3(char *in) { char *v1 = in + 2 * 4; int ch = (unsigned int)(in[0] + 1); unsigned char v3 = in[ch + 8]; int v4 = (unsigned char)(in[4] + v3); unsigned char v5 = in[v4 + 8];
*(int *)&(in[0]) = ch; *(int *)&(in[4]) = v4; v1[v4] = v3; v1[ch] = v5;
return (unsigned char)in[(unsigned int)((unsigned char)(v3 + v5) + 8)]; }
int decode2(char *in1, char *out, char *in2, int in2_length) { int result; for (int i = 0; i < in2_length; i++) { result = i; char ch = in2[i]; out[i] = ch ^ decode3(in1); }
return result; }
int main(int argc, char **argv) { char buf[LENGTH] = { 0 }; char s1[1024], s2[1024]; memset(s1, 0, 1024); memset(s2, 0, 1024);
printf("Please input key:"); fflush(stdout);
fgets(buf, LENGTH, stdin);
rot13(buf);
base64_decode(buf, s1, LENGTH);
judge(s1, strlen(s1));
char *s = (char *)malloc(264); memset(s, 0, 264);
decode1(s, s1, strlen(s1));
char const_s[] = { 14, 62, 106, 121, 121, 235, 156, 221, 130, 200, 82, 145, 155, 99, 157, 159, 2, 141, 207, 34, 68, 127, 0 }; decode2(s, s2, const_s, strlen(const_s));
printf("Text is:%s\n", s2); return 0; }
|