• 搜狐公司的测评


    搜狐公司的测评题目(C/C++) [Dev-C++]

     1 #include  <stdio.h> 
    2 #include <stdlib.h>
    3 //#include <stdint.h>
    4 #include <assert.h>
    5 #include <string.h>
    6
    7 typedef unsigned int uint32_t;
    8 typedef unsigned char uint8_t;
    9
    10 /*******************************************************************************
    11 * 搜狐评测题,通过加密和解密两个步骤
    12 ******************************************************************************/
    13
    14 int encode(constvoid* raw_in, void* raw_out, uint32_t password, size_t len)
    15 {
    16 const uint8_t*in= (const uint8_t*)raw_in;
    17 uint8_t*out= (uint8_t*)raw_out;
    18 // 对于 a = b ^ c 来说; 可以 b = a ^ c = b ^ c ^ c; 即对于下面中
    19 // 的所有 ^ 都只要重新来一次 ^ 就OK了
    20 uint32_t seed = password ^0xa4b9cdddu;
    21 for (size_t i =0 ; i < len; ++i) {
    22 // 输入首先右移一位; 存放在a中,即输入的高7位存放在a的低7位
    23 // a = zxxx xxxx
    24 uint8_t a = ( in[i] ^ seed ) >>1;
    25 // 下面这句是将输入左移 19,然后右移12位,相当于左移了7位
    26 // 即输入的低1位存在b的最高位 b = xzzz zzzz
    27 uint8_t b = ( ( ((uint32_t)in[i]) <<19 ) ^ seed ) >> (19-7);
    28 // 127 = 0111 1111 即将a的第7位置为0; a = 0\xxx xxxx(原来in的高7位)
    29 a &=127;
    30 // 128 = 1000 0000 即将b的第6~0位置为0 b = x(原来in的低1位)/000 0000
    31 b &=128;
    32 // 下面这句没有用,因为b右移三位,直接变成了0; 再与a异或的话就直接是a原来的值了
    33 a =127& ( a ^ (b <<3));
    34 // 最后out[i] 存放的就是将in[i]对调了 ;
    35 out[i] = a | b;
    36 // 种子的话,加密和解密的是一样的,只是这里的out[i]改为in[i]就OK了,因为加密
    37 // 算法中的输出就是解密算法时的输入; 而初始种子都是一样的
    38 seed = (((seed <<7) ^ seed ^out[i]) +144123481);
    39 }
    40 }
    41
    42
    43 int decode(constvoid* raw_in, void* raw_out, uint32_t password, size_t len)
    44 {
    45 const uint8_t*in= (const uint8_t*)raw_in;
    46 uint8_t*out= (uint8_t*)raw_out;
    47 //
    48 uint32_t seed = password ^0xa4b9cdddu;
    49 for (size_t i =0 ; i < len; ++i) {
    50 // 将经过加密的输入in[i]的低7位放在a高7位
    51 uint8_t a = ( in[i] <<1 ) ^ seed;
    52 // 将经过加密的输入in[i]的高1位放在b的低1位
    53 uint8_t b = (((uint32_t)in[i] << (19-7))^seed) >>19;
    54 // 254 = 1111 1110 将a的最后1位强制清零;注意加密中存放在a中是低7位,所以与127与;而这里是存在a的高七位,所以是254
    55 a &=254;
    56 // 将b高7位强制清零
    57 b &=1;
    58 // 两者异或就得到了加密前的数据了
    59 out[i] = a|b;
    60 // 种子解释看加密
    61 seed = (((seed <<7) ^ seed ^in[i]) +144123481);
    62 }
    63 }
    64 int main()
    65 {
    66 const uint8_t buf1[] = {0xbe, 0x58, 0xd5, 0xcb, 0xc7, 0x85, 0x84, 0x3a, 0xa2, 0x30, 0xa3, 0x36, 0x5f, 0x43, 0xc9, 0x53, 0x67, 0xf3, 0xac, 0xed, 0xc4, 0x38, 0xb7, 0xdf, 0xec, 0x51, 0x09, 0xbf, 0x2a, 0x32, 0x6b, 0x42, 0x32, 0x71, 0xe0, 0x93, 0x8b, 0xeb, 0x1c, 0xd2, 0x62, 0x6a, 0xb6, 0x10, 0x09, 0x89, 0xb6, 0xb1, 0x29, 0x31, 0xd2, 0xb9, 0xa0, 0xb5, 0x5d, 0x40, 0xf8, 0x74, 0xc3, 0x35, 0x70, 0x95, 0x2a, 0x0b, 0x5a, 0x82, 0x51, 0x05, };
    67 uint8_t buf2[100] = {};
    68 const uint32_t password =0x1c1c2c6bu;
    69 const size_t len =sizeof(buf1);
    70 decode(buf1, buf2, password, len);
    71 printf("%s\n", buf2);
    72 system("PAUSE");
    73 return EXIT_SUCCESS;
    74 }

      输出结果:

     1 #include  <stdio.h> 
    2 #include <stdlib.h>
    3 //#include <stdint.h>
    4 #include <assert.h>
    5 #include <string.h>
    6 typedef unsigned int uint32_t;
    7 typedef unsigned char uint8_t;
    8
    9 int encode(constvoid* raw_in, void* raw_out, uint32_t password, size_t len)
    10 {
    11 const uint8_t*in= (const uint8_t*)raw_in;
    12 uint8_t*out= (uint8_t*)raw_out;
    13
    14 uint32_t seed = password ^0x91e69948u;
    15 for (size_t i =0 ; i < len; ++i) {
    16
    17 uint8_t a = ( in[i] ^ seed ) >>5;
    18
    19 uint8_t b = ( ( ((uint32_t)in[i]) <<19 ) ^ seed ) >> (19-3);
    20 a &=7;
    21 b &=248;
    22 a =7& ( a ^ (b <<3));
    23 out[i] = a | b;
    24 seed = ((seed ^in[i]) *536513+in[i]);
    25 }
    26 return0;
    27 }
    28
    29
    30 int decode(constvoid* raw_in, void* raw_out, uint32_t password, size_t len)
    31 {
    32 const uint8_t*in= (const uint8_t*)raw_in;
    33 uint8_t*out= (uint8_t*)raw_out;
    34
    35 uint32_t seed = password ^0x91e69948u;
    36 for (size_t i =0 ; i < len; ++i) {
    37 uint8_t a = (in[i] <<5) ^ seed;
    38 uint8_t b = ( ((uint32_t)in[i] << (19-3)) ^seed ) >>19;
    39 a &=224;
    40 b &=31;
    41 out[i] = a | b;
    42 seed = ((seed ^out[i]) *536513+out[i]);
    43 }
    44 return0;
    45 }
    46 int main()
    47 {
    48 const uint8_t buf1[] = {0xfe, 0x7c, 0x75, 0xf8, 0x85, 0x14, 0x1b, 0x21, 0xee, 0x13, 0x52, 0xb6, 0xc8, 0xfd, 0x06, 0x40, 0x92, 0x7c, };
    49 uint8_t buf2[100] = {};
    50 const uint32_t password =0x27430952u;
    51 const size_t len =sizeof(buf1);
    52 decode(buf1, buf2, password, len);
    53 printf("%s\n", buf2);
    54 system("PAUSE");
    55 return0;
    56 }

      输出结果

  • 相关阅读:
    LA 6891 Money Transfers(最短路)
    Gym
    UVa 1662 Brackets Removal
    Gym 101334F Feel Good
    Gym 101334E Exploring Pyramids(dp+乘法原理)
    POJ 2112 Optimal Milking(二分+最大流)
    POJ 2115 C Looooops(模线性方程)
    UVa 11552 最小的块数(序列划分模型:状态设计)
    UVa 10534 波浪子序列(快速求LIS)
    UVa 10891 Sum游戏
  • 原文地址:https://www.cnblogs.com/Firefly727/p/2173958.html
Copyright © 2020-2023  润新知