Chinaunix首页 | 论坛 | 博客
  • 博客访问: 50596
  • 博文数量: 27
  • 博客积分: 716
  • 博客等级: 上士
  • 技术积分: 285
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-31 11:12
文章分类

全部博文(27)

文章存档

2012年(8)

2011年(19)

我的朋友

分类: C/C++

2011-10-20 11:23:41

字符串使用GBK编码。首先保证你的终端能够解析GBK编码。才能得到答案。

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <stdint.h>

  4. int encode(const void* raw_in, void* raw_out,uint32_t password, size_t len)
  5. {
  6.     const uint8_t* in = (const uint8_t*)raw_in;
  7.     uint8_t* out = (uint8_t*)raw_out;

  8.     uint32_t seed = password ^ 0x72590570u;
  9.     size_t i;
  10.     for (i = 0; i < len; ++i) {
  11.         uint8_t a = ( in[i] ^ seed ) >> 2;
  12.         uint8_t b = ( ( ((uint32_t)in[i]) << 17 ) ^ seed ) >> (17-6);
  13.         a &= 63;
  14.         b &= 192;
  15.         a = 63 & ( a ^ (b << 3));
  16.         out[i] = a | b;
  17.         seed = (seed * 84723701 ^ seed ^ out[i]);
  18.     }
  19. }


  20. int decode(const void* raw_in, void* raw_out, uint32_t password, size_t len)
  21. {
  22.     const uint8_t* in = (const uint8_t*)raw_in;
  23.     uint8_t* out = (uint8_t*)raw_out;

  24.     uint32_t seed = password ^ 0x72590570u;
  25.     size_t i;
  26.     for (i = 0 ; i < len; ++i) {
  27.         uint8_t a = ((in[i] << 2) ^ seed);
  28.         uint8_t b = ((((uint32_t)in[i]) << 11) ^ seed) >> 17;
  29.         a &= 252;
  30.         b &= 3;
  31.         out[i] = a | b;
  32.         seed = (seed * 84723701 ^ seed ^ in[i]);
  33.     }
  34. }

  35. int main()
  36. {
  37.     const uint8_t buf1[] = {0x5f, 0xc0, 0x91, 0xbd, 0x9f, 0x7e, 0x51, \
  38.                                 0x72, 0x82, 0xff, 0xbc, 0x29, 0x12, 0x53, \
  39.                                 0x4f, 0x24, 0x2d, 0x52, 0xf9, 0x0d, 0x04, \
  40.                                 0xa6, 0x56, 0x44, 0xe9, 0x0b, 0x72, 0xd0, 0x37, 0x9d, 0xd7, \
  41.                                 0x7e, 0x60, 0x9a, 0xe6, 0x3f, 0x4d, 0xbe, 0xda, 0x78, 0xac, 0x13, };
  42.     uint8_t buf2[100] = {};
  43.     const uint32_t password = 0xf514c1c5u;
  44.     const size_t len = sizeof(buf1);
  45.     decode(buf1, buf2, password, len);
  46.     printf("%s\n", buf2);
  47. }
阅读(680) | 评论(0) | 转发(0) |
0

上一篇:删除字符串中的空格

下一篇:《诫子书》

给主人留下些什么吧!~~