typedef struct {
unsigned char S[256];
int i, j;
} Crypto_CTX;
void crypto_init(Crypto_CTX *ctx, const unsigned char *key, int keylen) {
int i, j = 0, k;
unsigned char tmp;
for (i = 0; i < 256; i++) {
ctx->S[i] = i;
}
for (i = 0; i < 256; i++) {
j = (j + ctx->S[i] + key[i % keylen]) % 256;
tmp = ctx->S[i];
ctx->S[i] = ctx->S[j];
ctx->S[j] = tmp;
}
ctx->i = 0;
ctx->j = 0;
}
void crypto_crypt(Crypto_CTX *ctx, const unsigned char *inbuf, unsigned char *outbuf, int buflen) {
int i;
unsigned char tmp;
for (i = 0; i < buflen; i++) {
ctx->i = (ctx->i + 1) % 256;
ctx->j = (ctx->j + ctx->S[ctx->i]) % 256;
tmp = ctx->S[ctx->i];
ctx->S[ctx->i] = ctx->S[ctx->j];
ctx->S[ctx->j] = tmp;
outbuf[i] = inbuf[i] ^ ctx->S[(ctx->S[ctx->i] + ctx->S[ctx->j]) % 256];
}
}
void encrypt_flag(const char *flag, unsigned char *encrypted_flag) {
Crypto_CTX crypto_ctx;
crypto_init(&crypto_ctx, (const unsigned char *)KEY, strlen(KEY));
crypto_crypt(&crypto_ctx, (const unsigned char *)flag, encrypted_flag, strlen(flag));
}
void bytes_to_hex(const unsigned char *bytes, char *hex, int len) {
for (int i = 0; i < len; i++) {
sprintf(hex + 2 * i, "%02x", bytes[i]);
}
}
int main() {
char input_flag[256];
unsigned char encrypted_flag[256];
char encrypted_flag_hex[256 * 2 + 1];
printf("Enter the flag: ");
fgets(input_flag, sizeof(input_flag), stdin);
// Remove the newline character from the input if present
input_flag[strcspn(input_flag, "\n")] = '\0';
// Encrypt the input flag
encrypt_flag(input_flag, encrypted_flag);
// Convert encrypted flag to hex string
bytes_to_hex(encrypted_flag, encrypted_flag_hex, strlen(input_flag));
// Compare the encrypted flag with the target encrypted flag
if (strcmp(encrypted_flag_hex, TARGET_ENCRYPTED_FLAG) == 0) {
printf("congratulation\n");
} else {
printf("try again\n");
}
return 0;
}