- 论坛徽章:
- 0
|
本帖最后由 snowboy9859 于 2011-09-09 19:03 编辑
以下程序是一个信息编码的程序,阅读其encode部分,并补全其decode部分
最后运行程序,会打印出的一句话。这句话就是我们要求的答案。 (本段代码遵循c99标准,gcc编译请加-std=c99)
注意!这句话是用GBK编码的!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <assert.h>
- #include <string.h>
- int encode(const void* raw_in, void* raw_out, uint32_t password, size_t len)
- {
- const uint8_t* in=(const uint8_t*)raw_in;
- uint8_t* out=(uint8_t*)raw_out;
-
- uint32_t seed = password ^ 0x164ba504u;
- for (size_t i = 0; i < len; ++i)
- {
- uint8_t a = (in[i] ^ seed) >> 4; //将in[i]的高四位异或后的结果存放在a的低四位中
- uint8_t b = ((((uint32_t)in[i]) << 12)^seed)>>(12-4); //将in[i]的低四位异或后放在b的高四位中
- a &= 15; //将未存储有效数据位清零
- b &= 240; //将未存储有效数据位清零
- a = 15 & (a ^(b <<3)); //这句约等于没有,不起任何作用起误导作用
- out[i] = a | b; //把经过转换的数据相或组成新的数据
- seed = ((seed ^ out[i]) * 48475829 + out[i]); //通过通式更新seed
- }
- }
- int decode(const void* raw_in, void* raw_out, uint32_t password, size_t len)
- {
- const uint8_t* in = (const uint8_t*)raw_in;
- uint8_t* out = (uint8_t*)raw_out;
-
- uint32_t seed = password ^ 0x164ba504u;
- for(size_t i = 0 ; i < len; ++i)
- {
- // 请在此处补全代码 (本人不才,下面代码为自己给出尚且通过调试未得到预期的答案,请高手指教)
- uint8_t a = in[i] & 15;//取出in[i]的低四位放入a中
- uint8_t b = in[i] & 240;//取出in[i]的高四位放入b中
- uint8_t c = ((a<<4)^seed)&240;//运用c=a^b;a=c^b;的原理,将原来的数据还原,并清空无效的数据位
- unit8_t d = (((((uint32_t)b)<<8)^seed)>>12)&15;//运用c=a^b;a=c^b;的原理,将原来的数据还原,并清空无效的数据位
- out[i] = c | d;//将还原后的高四位和低四位相或
- seed = ((seed ^ in[i]) * 48475829 + in[i]);//保证编码和解码用的一个密码;(编码中的out[i]就是解码中的in[i])
- }
- }
- int main()
- {
- const uint8_t buf1[] = {0xf2, 0x60, 0xab, 0xa6, 0xa5, 0xbe, 0xc9, 0x39, 0x15, 0xdf, 0xe6, 0x06, 0x47, 0x02, 0xa6, 0xfb, 0xcb, 0x60, 0x76, 0xf0, 0x08, 0xfb, 0x92, 0xe4, 0xaf, 0x4d, 0x22, 0x7a, 0x6a, 0xfe, 0xcf, 0x85, 0x25, 0x25, 0xfc, 0x31, 0xe4, 0xa2, 0x10, 0x2b, 0xd8, 0x50, 0x8f, 0x7f, 0x3f, 0xf0, 0xde, 0xee, 0x30, 0x96, 0x24, 0x7f, 0x9f, 0x56, 0x06, 0x21, 0xd3, };
- uint8_t buf2[100] = {};
- const uint32_t password = 0xee90efe9u;
- const size_t len = sizeof(buf1);
- decode(buf1, buf2, password, len);
- printf("%s\n", buf2);
- }
复制代码 要补全之后求答案
在此将测试程序贴出:(此程序可以在vc下直接编译运行)- #include <stdio.h>
- #include <stdlib.h>
- //#include <stdint.h>
- #include <assert.h>
- #include <string.h>
- typedef unsigned int uint32_t;
- typedef unsigned char uint8_t;
- int encode(const void* raw_in, void* raw_out, uint32_t password, size_t len)
- {
- const uint8_t* in = (const uint8_t*)raw_in;
- uint8_t* out = (uint8_t*)raw_out;
-
- uint32_t seed = password ^ 0x164ba504u;
- for (size_t i = 0 ; i < len; ++i) {
- uint8_t a = ( in[i] ^ seed ) >> 4;
- uint8_t b = ( ( ((uint32_t)in[i]) << 12 ) ^ seed ) >> (12-4);
- a &= 15;
- b &= 240;
- a = 15 & ( a ^ (b << 3));
- out[i] = a | b;
- seed = ((seed ^ out[i]) * 48475829 + out[i]);
- }
- return 0;
- }
- int decode(const void* raw_in, void* raw_out, uint32_t password, size_t len)
- {
- const uint8_t* in = (const uint8_t*)raw_in;
- uint8_t* out = (uint8_t*)raw_out;
-
- uint32_t seed = password ^ 0x164ba504u;
- for (size_t i = 0 ; i < len; ++i) {
- out[i] = (((in[i]<<4)^seed)&0xF0) | (((in[i]>>4)^(seed>>12))&0x0F);
- seed = ((seed ^ in[i]) * 48475829 + in[i]);
-
- // 请在此处补全代码
- }
- return 0;
- }
- int main(void)
- {
- const uint8_t buf1[] = {0xf2, 0x60, 0xab, 0xa6, 0xa5, 0xbe, 0xc9, 0x39, 0x15, 0xdf, 0xe6, 0x06, 0x47, 0x02, 0xa6, 0xfb, 0xcb, 0x60, 0x76, 0xf0, 0x08, 0xfb, 0x92, 0xe4, 0xaf, 0x4d, 0x22, 0x7a, 0x6a, 0xfe, 0xcf, 0x85, 0x25, 0x25, 0xfc, 0x31, 0xe4, 0xa2, 0x10, 0x2b, 0xd8, 0x50, 0x8f, 0x7f, 0x3f, 0xf0, 0xde, 0xee, 0x30, 0x96, 0x24, 0x7f, 0x9f, 0x56, 0x06, 0x21, 0xd3, };
- uint8_t buf2[100] = {0};
- const uint32_t password = 0xee90efe9u;
- const size_t len = sizeof(buf1);
- decode(buf1, buf2, password, len);
- printf("%s\n", buf2);
- return 0;
- }
复制代码 运行结果
|
|