zoukankan      html  css  js  c++  java
  • 攻防世界-进阶-parallel-comparator-200

    C文件

    #include <stdlib.h>
    #include <stdio.h>
    #include <pthread.h>
    
    #define FLAG_LEN 20
    
    void * checking(void *arg) {
        char *result = malloc(sizeof(char));
        char *argument = (char *)arg;
        *result = (argument[0]+argument[1]) ^ argument[2];//argument[0]>97
        return result;
    }
    
    int highly_optimized_parallel_comparsion(char *user_string)
    {
        int initialization_number;
        int i;
        char generated_string[FLAG_LEN + 1];
        generated_string[FLAG_LEN] = '';
    
        while ((initialization_number = random()) >= 64);//开始认为随机数且大于64,事实上为一固定数37
        
        int first_letter;
        first_letter = (initialization_number % 26) + 97;//97-123
    
        pthread_t thread[FLAG_LEN];
        char differences[FLAG_LEN] = {0, 9, -9, -1, 13, -13, -4, -11, -9, -1, -7, 6, -13, 13, 3, 9, -13, -11, 6, -7};
        char *arguments[20];
        //没有全部执行,
        for (i = 0; i < FLAG_LEN; i++) {
            arguments[i] = (char *)malloc(3*sizeof(char));
            arguments[i][0] = first_letter;//三个数拼接到一起 随机的
            arguments[i][1] = differences[i];//固定的
            arguments[i][2] = user_string[i];//输入的
    
            pthread_create((pthread_t*)(thread+i), NULL, checking, arguments[i]);
        }
    
        void *result;
        int just_a_string[FLAG_LEN] = {115, 116, 114, 97, 110, 103, 101, 95, 115, 116, 114, 105, 110, 103, 95, 105, 116, 95, 105, 115};
        for (i = 0; i < FLAG_LEN; i++) {
            pthread_join(*(thread+i), &result);
            generated_string[i] = *(char *)result + just_a_string[i];
            free(result);
            free(arguments[i]);
        }
    
        int is_ok = 1;
        for (i = 0; i < FLAG_LEN; i++) {
            if (generated_string[i] != just_a_string[i])
                return 0;
        }
    
        return 1;
    }
    
    int main()
    {
        char *user_string = (char *)calloc(FLAG_LEN+1, sizeof(char));
        fgets(user_string, FLAG_LEN+1, stdin);
        int is_ok = highly_optimized_parallel_comparsion(user_string);
        if (is_ok)
            printf("You win!
    ");
        else
            printf("Wrong!
    ");
        return 0;
    }
    
    

    代码分析

     generated_string[i] = *(char *)result + just_a_string[i];
    
    if (generated_string[i] != just_a_string[i])
                return 0;
    

    说明result为固定值零。

    解题脚本

    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    #define FLAG_LEN 20
    char differences[FLAG_LEN] = {0, 9, -9, -1, 13, -13, -4, -11, -9, -1, -7, 6, -13, 13, 3, 9, -13, -11, 6, -7};
    int main()
    {       
          int initialization_number,first_letter;
          while ((initialization_number = random()) >= 64);  
          first_letter = (initialization_number % 26) + 97;
    	  printf("%d",initialization_number);
          for(int i=0;i<FLAG_LEN;i++)
          {
            printf("%c",first_letter+differences[i]);
          }
          printf("
    ");
    }
    
    
  • 相关阅读:
    poj3083(Children of the Candy Corn)
    poj3278(Catch That Cow)
    poj2996(Help Me with the Game)
    poj2993(Emag eht htiw Em Pleh)
    js 对多sheet Excel赋值操作
    学习进度总结(三)
    学习进度总结(二)
    学习进度总结(一)
    《人月神话》阅读笔记(1)
    Android studio的安装与使用
  • 原文地址:https://www.cnblogs.com/banpingcu/p/12696422.html
Copyright © 2011-2022 走看看