网络游戏数据加密技术

发表于2015-09-09
评论0 3.1k浏览

消息(message)被称为明文(plaintext)。用某种方法伪装消息以隐藏它的内容的过程称为加密(encryption),被加密的消息成为密文(ciphertext),而把密文转变为明文的过程称为解密(decryption)。


信息加密解密过程

 

算法和密钥

密码算法是用于加密和解密的数学函数。通常有加密和解密两个相关的函数。

如果算法(algorighm)的保密性是基于保持算法的秘密,这种算法被称为爱限制的算法。在低密级的应用中还是会采用算法级的保密性。

 

现代密码学采用密钥(key)解决了这个问题。密钥通常成对使用,即加密密钥和解密密钥。加密密钥和解密密钥可能相同也可能不同。密码系统由算法以及所有可能的明文、密文和密钥组成。 


现代密码系统组成

 

基于密钥的算法通常有两类:对称算法和公开密钥算法。

对称算法有时又叫传统密码算法,就是加密密钥能够从解密密钥中推算出来,反过来也成立。在大多数对称算法中,加密密钥和解密密钥是相同的。

 

公开密钥算法是这样设计的:加密密钥和解密密钥不同,而且解密密钥不能根据加密密钥计算出来(至少在合理假定的长时间内)。之所以叫做公开密钥算法,是因为加密密钥能够公开,即发送者能用加密密钥加密信息,但只有用相应的解密密钥才能解密信息。在这些系统中,加密密钥叫做公开密钥,解密密钥叫做私人密钥。

 

MD5算法(message-digest algorithm 5,信息——摘要算法)

MD5是在20世纪90年代初由mit laboratory for computer science和rsa data security inc的ronald l.rivest开发出来,经md2、md3和md4发展而来的。

它的作用是让大容量信息在用数字签名软件签署私人密匙前被“压缩”成一种保密的格式(就是把一个任意长度的字节串变换成一定长的大整数)。


MD5算法原理

概述

MD5算法是一种消息摘要算法(Message Digest Algorithm),此算法以任意长度的信息(message)作为输入进行计算,产生一个128-bit(16-byte)的指纹或报文摘要(fingerprint

 or message digest)。两个不同的message产生相同message digest的几率相当小,从一个给定的message digest逆向产生原始message更是困难。

MD5是一种公开密钥的算法,单向函数(one way function)的概念是公开密钥的中心。

单向函数计算起来相对容易,但求逆非常困难。也就是说,已知x,我们很容易计算出f(x)。但已知f(x),却很难计算出x。打碎盘子就是一个很好的单向函数的例子。

 

MD5算法的应用

MD5算法是单向的,在网络游戏中的应用主要体现在以下两个方面。

(1)游戏账号的密码保护

用单向加密算法加密后的密码存放在数据库中,即使有人得到了数据库中的数据,想利用密文密码恢复成明文密码几乎是不可能的。而用户再次登录过程中输入的密码经过加密运算后,生成的密文密码和数据库中存储的密文密码一致,则说明用户再次输入的密码明文是和注册时的密码明文一致的。

(2)自动更新

MD5可以把一个文件当做大文本信息,通过算法变换,产生唯一的MD5信息摘要。以后传播文件过程中,无论文件的内容发生了任何形式的变化,只要重新计算文件的MD5值,就会发现是不同的,由此可以决定文件是否需要更新。

 

MD5算法的实现

md5.h

  1. #ifndef MD5_H  
  2. #define MD5_H  
  3.   
  4. #include <string>  
  5. #include <fstream>  
  6.   
  7. /* Type define */  
  8. typedef unsigned char byte;  
  9. typedef unsigned int uint32;  
  10.   
  11. using std::string;  
  12. using std::ifstream;  
  13.   
  14. /* MD5 declaration. */  
  15. class MD5 {  
  16. public:  
  17.     MD5();  
  18.     MD5(const void* input, size_t length);  
  19.     MD5(const string& str);  
  20.     MD5(ifstream& in);  
  21.     void update(const void* input, size_t length);  
  22.     void update(const string& str);  
  23.     void update(ifstream& in);  
  24.     const byte* digest();  
  25.     string toString();  
  26.     void reset();  
  27.   
  28. private:  
  29.     void update(const byte* input, size_t length);  
  30.     void final();  
  31.     void transform(const byte block[64]);  
  32.     void encode(const uint32* input, byte* output, size_t length);  
  33.     void decode(const byte* input, uint32* output, size_t length);  
  34.     string bytesToHexString(const byte* input, size_t length);  
  35.   
  36.     /* class uncopyable */  
  37.     MD5(const MD5&);  
  38.     MD5& operator=(const MD5&);  
  39.   
  40. private:  
  41.     uint32 _state[4];   /* state (ABCD) */  
  42.     uint32 _count[2];   /* number of bits, modulo 2^64 (low-order word first) */  
  43.     byte _buffer[64];   /* input buffer */  
  44.     byte _digest[16];   /* message digest */  
  45.     bool _finished;     /* calculate finished ? */  
  46.   
  47.     static const byte PADDING[64];  /* padding for calculate */  
  48.     static const char HEX[16];  
  49.     enum { BUFFER_SIZE = 1024 };  
  50. };  
  51.   
  52. #endif /*MD5_H*/  

md5.cpp

  1. #include "md5.h"  
  2.   
  3. using namespace std;  
  4.   
  5. /* Constants for MD5Transform routine. */  
  6. #define S11 7  
  7. #define S12 12  
  8. #define S13 17  
  9. #define S14 22  
  10. #define S21 5  
  11. #define S22 9  
  12. #define S23 14  
  13. #define S24 20  
  14. #define S31 4  
  15. #define S32 11  
  16. #define S33 16  
  17. #define S34 23  
  18. #define S41 6  
  19. #define S42 10  
  20. #define S43 15  
  21. #define S44 21  
  22.   
  23.   
  24. /* F, G, H and I are basic MD5 functions. 
  25. */  
  26. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))  
  27. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))  
  28. #define H(x, y, z) ((x) ^ (y) ^ (z))  
  29. #define I(x, y, z) ((y) ^ ((x) | (~z)))  
  30.   
  31. /* ROTATE_LEFT rotates x left n bits. 
  32. */  
  33. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))  
  34.   
  35. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. 
  36. Rotation is separate from addition to prevent recomputation. 
  37. */  
  38. #define FF(a, b, c, d, x, s, ac) {   
  39.     (a) += F ((b), (c), (d)) + (x) + ac;   
  40.     (a) = ROTATE_LEFT ((a), (s));   
  41.     (a) += (b);   
  42. }  
  43. #define GG(a, b, c, d, x, s, ac) {   
  44.     (a) += G ((b), (c), (d)) + (x) + ac;   
  45.     (a) = ROTATE_LEFT ((a), (s));   
  46.     (a) += (b);   
  47. }  
  48. #define HH(a, b, c, d, x, s, ac) {   
  49.     (a) += H ((b), (c), (d)) + (x) + ac;   
  50.     (a) = ROTATE_LEFT ((a), (s));   
  51.     (a) += (b);   
  52. }  
  53. #define II(a, b, c, d, x, s, ac) {   
  54.     (a) += I ((b), (c), (d)) + (x) + ac;   
  55.     (a) = ROTATE_LEFT ((a), (s));   
  56.     (a) += (b);   
  57. }  
  58.   
  59.   
  60. const byte MD5::PADDING[64] = { 0x80 };  
  61. const char MD5::HEX[16] = {  
  62.     '0''1''2''3',  
  63.     '4''5''6''7',  
  64.     '8''9''a''b',  
  65.     'c''d''e''f'  
  66. };  
  67.   
  68.   
  69. /* Default construct. */  
  70. MD5::MD5() {  
  71.     reset();  
  72. }  
  73.   
  74. /* Construct a MD5 object with a input buffer. */  
  75. MD5::MD5(const void* input, size_t length) {  
  76.     reset();  
  77.     update(input, length);  
  78. }  
  79.   
  80. /* Construct a MD5 object with a string. */  
  81. MD5::MD5(const string& str) {  
  82.     reset();  
  83.     update(str);  
  84. }  
  85.   
  86. /* Construct a MD5 object with a file. */  
  87. MD5::MD5(ifstream& in) {  
  88.     reset();  
  89.     update(in);  
  90. }  
  91.   
  92. /* Return the message-digest */  
  93. const byte* MD5::digest() {  
  94.   
  95.     if (!_finished) {  
  96.         _finished = true;  
  97.         final();  
  98.     }  
  99.     return _digest;  
  100. }  
  101.   
  102. /* Reset the calculate state */  
  103. void MD5::reset() {  
  104.   
  105.     _finished = false;  
  106.     /* reset number of bits. */  
  107.     _count[0] = _count[1] = 0;  
  108.     /* Load magic initialization constants. */  
  109.     _state[0] = 0x67452301;  
  110.     _state[1] = 0xefcdab89;  
  111.     _state[2] = 0x98badcfe;  
  112.     _state[3] = 0x10325476;  
  113. }  
  114.   
  115. /* Updating the context with a input buffer. */  
  116. void MD5::update(const void* input, size_t length) {  
  117.     update((const byte*)input, length);  
  118. }  
  119.   
  120. /* Updating the context with a string. */  
  121. void MD5::update(const string& str) {  
  122.     update((const byte*)str.c_str(), str.length());  
  123. }  
  124.   
  125. /* Updating the context with a file. */  
  126. void MD5::update(ifstream& in) {  
  127.   
  128.     if (!in) {  
  129.         return;  
  130.     }  
  131.   
  132.     std::streamsize length;  
  133.     char buffer[BUFFER_SIZE];  
  134.     while (!in.eof()) {  
  135.         in.read(buffer, BUFFER_SIZE);  
  136.         length = in.gcount();  
  137.         if (length > 0) {  
  138.             update(buffer, length);  
  139.         }  
  140.     }  
  141.     in.close();  
  142. }  
  143.   
  144. /* MD5 block update operation. Continues an MD5 message-digest 
  145. operation, processing another message block, and updating the 
  146. context. 
  147. */  
  148. void MD5::update(const byte* input, size_t length) {  
  149.   
  150.     uint32 i, index, partLen;  
  151.   
  152.     _finished = false;  
  153.   
  154.     /* Compute number of bytes mod 64 */  
  155.     index = (uint32)((_count[0] >> 3) & 0x3f);  
  156.   
  157.     /* update number of bits */  
  158.     if ((_count[0] += ((uint32)length << 3)) < ((uint32)length << 3)) {  
  159.         ++_count[1];  
  160.     }  
  161.     _count[1] += ((uint32)length >> 29);  
  162.   
  163.     partLen = 64 - index;  
  164.   
  165.     /* transform as many times as possible. */  
  166.     if (length >= partLen) {  
  167.   
  168.         memcpy(&_buffer[index], input, partLen);  
  169.         transform(_buffer);  
  170.   
  171.         for (i = partLen; i + 63 < length; i += 64) {  
  172.             transform(&input[i]);  
  173.         }  
  174.         index = 0;  
  175.   
  176.     } else {  
  177.         i = 0;  
  178.     }  
  179.   
  180.     /* Buffer remaining input */  
  181.     memcpy(&_buffer[index], &input[i], length - i);  
  182. }  
  183.   
  184. /* MD5 finalization. Ends an MD5 message-_digest operation, writing the 
  185. the message _digest and zeroizing the context. 
  186. */  
  187. void MD5::final() {  
  188.   
  189.     byte bits[8];  
  190.     uint32 oldState[4];  
  191.     uint32 oldCount[2];  
  192.     uint32 index, padLen;  
  193.   
  194.     /* Save current state and count. */  
  195.     memcpy(oldState, _state, 16);  
  196.     memcpy(oldCount, _count, 8);  
  197.   
  198.     /* Save number of bits */  
  199.     encode(_count, bits, 8);  
  200.   
  201.     /* Pad out to 56 mod 64. */  
  202.     index = (uint32)((_count[0] >> 3) & 0x3f);  
  203.     padLen = (index < 56) ? (56 - index) : (120 - index);  
  204.     update(PADDING, padLen);  
  205.   
  206.     /* Append length (before padding) */  
  207.     update(bits, 8);  
  208.   
  209.     /* Store state in digest */  
  210.     encode(_state, _digest, 16);  
  211.   
  212.     /* Restore current state and count. */  
  213.     memcpy(_state, oldState, 16);  
  214.     memcpy(_count, oldCount, 8);  
  215. }  
  216.   
  217. /* MD5 basic transformation. Transforms _state based on block. */  
  218. void MD5::transform(const byte block[64]) {  
  219.   
  220.     uint32 a = _state[0], b = _state[1], c = _state[2], d = _state[3], x[16];  
  221.   
  222.     decode(block, x, 64);  
  223.   
  224.     /* Round 1 */  
  225.     FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */  
  226.     FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */  
  227.     FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */  
  228.     FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */  
  229.     FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */  
  230.     FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */  
  231.     FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */  
  232.     FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */  
  233.     FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */  
  234.     FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */  
  235.     FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */  
  236.     FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */  
  237.     FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */  
  238.     FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */  
  239.     FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */  
  240.     FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */  
  241.   
  242.     /* Round 2 */  
  243.     GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */  
  244.     GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */  
  245.     GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */  
  246.     GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */  
  247.     GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */  
  248.     GG (d, a, b, c, x[10], S22,  0x2441453); /* 22 */  
  249.     GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */  
  250.     GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */  
  251.     GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */  
  252.     GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */  
  253.     GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */  
  254.     GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */  
  255.     GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */  
  256.     GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */  
  257.     GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */  
  258.     GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */  
  259.   
  260.     /* Round 3 */  
  261.     HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */  
  262.     HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */  
  263.     HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */  
  264.     HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */  
  265.     HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */  
  266.     HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */  
  267.     HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */  
  268.     HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */  
  269.     HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */  
  270.     HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */  
  271.     HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */  
  272.     HH (b, c, d, a, x[ 6], S34,  0x4881d05); /* 44 */  
  273.     HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */  
  274.     HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */  
  275.     HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */  
  276.     HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */  
  277.   
  278.     /* Round 4 */  
  279.     II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */  
  280.     II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */  
  281.     II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */  
  282.     II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */  
  283.     II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */  
  284.     II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */  
  285.     II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */  
  286.     II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */  
  287.     II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */  
  288.     II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */  
  289.     II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */  
  290.     II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */  
  291.     II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */  
  292.     II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */  
  293.     II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */  
  294.     II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */  
  295.   
  296.     _state[0] += a;  
  297.     _state[1] += b;  
  298.     _state[2] += c;  
  299.     _state[3] += d;  
  300. }  
  301.   
  302. /* Encodes input (ulong) into output (byte). Assumes length is 
  303. a multiple of 4. 
  304. */  
  305. void MD5::encode(const uint32* input, byte* output, size_t length) {  
  306.   
  307.     for (size_t i = 0, j = 0; j < length; ++i, j += 4) {  
  308.         output[j]= (byte)(input[i] & 0xff);  
  309.         output[j + 1] = (byte)((input[i] >> 8) & 0xff);  
  310.         output[j + 2] = (byte)((input[i] >> 16) & 0xff);  
  311.         output[j + 3] = (byte)((input[i] >> 24) & 0xff);  
  312.     }  
  313. }  
  314.   
  315. /* Decodes input (byte) into output (ulong). Assumes length is 
  316. a multiple of 4. 
  317. */  
  318. void MD5::decode(const byte* input, uint32* output, size_t length) {  
  319.   
  320.     for (size_t i = 0, j = 0; j < length; ++i, j += 4) {  
  321.         output[i] = ((uint32)input[j]) | (((uint32)input[j + 1]) << 8) |  
  322.         (((uint32)input[j + 2]) << 16) | (((uint32)input[j + 3]) << 24);  
  323.     }  
  324. }  
  325.   
  326. /* Convert byte array to hex string. */  
  327. string MD5::bytesToHexString(const byte* input, size_t length) {  
  328.   
  329.     string str;  
  330.     str.reserve(length << 1);  
  331.     for (size_t i = 0; i < length; ++i) {  
  332.         int t = input[i];  
  333.         int a = t / 16;  
  334.         int b = t % 16;  
  335.         str.append(1, HEX[a]);  
  336.         str.append(1, HEX[b]);  
  337.     }  
  338.     return str;  
  339. }  
  340.   
  341. /* Convert digest to string value */  
  342. string MD5::toString() {  
  343.     return bytesToHexString(digest(), 16);  
  344. }  

test.cpp

  1. #include "md5.h"  
  2. #include <iostream>  
  3.   
  4. using namespace std;  
  5.   
  6. void PrintMD5(const string& str, MD5& md5) {  
  7.     cout << "MD5("" << str << "") = " << md5.toString() << endl;  
  8. }  
  9.   
  10. string FileDigest(const string& file) {  
  11.   
  12.     ifstream in(file.c_str(), ios::binary);  
  13.     if (!in) {  
  14.         return "";  
  15.     }  
  16.   
  17.     MD5 md5;  
  18.     std::streamsize length;  
  19.     char buffer[1024];  
  20.     while (!in.eof()) {  
  21.         in.read(buffer, 1024);  
  22.         length = in.gcount();  
  23.         if (length > 0) {  
  24.             md5.update(buffer, length);  
  25.         }  
  26.     }  
  27.     in.close();  
  28.     return md5.toString();  
  29. }  
  30.   
  31. int main() {  
  32.   
  33.     //cout << MD5("abc").toString() << endl;  
  34.     //cout << MD5(ifstream("D:\test.txt")).toString() << endl;  
  35.     //cout << MD5(ifstream("D:\test.exe", ios::binary)).toString() << endl;  
  36.     //cout << FileDigest("D:\test.exe") << endl;  
  37.   
  38.     MD5 md5;  
  39.     md5.update("");  
  40.     PrintMD5("", md5);  
  41.   
  42.     md5.update("a");  
  43.     PrintMD5("a", md5);  
  44.   
  45.     md5.update("bc");  
  46.     PrintMD5("abc", md5);  
  47.   
  48.     md5.update("defghijklmnopqrstuvwxyz");  
  49.     PrintMD5("abcdefghijklmnopqrstuvwxyz", md5);  
  50.   
  51.     md5.reset();  
  52.     md5.update("message digest");  
  53.     PrintMD5("message digest", md5);  
  54.   
  55.     //md5.reset();  
  56.     //md5.update(ifstream("D:\test.txt"));  
  57.     //PrintMD5("D:\test.txt", md5);  
  58.     while(1);  
  59.     return 0;  
  60. }  


 

位操作

数据在计算机里以二进制“0”“1”的形式存储,大部分的密码算法都是在位运算的基础上进行的,需要直接对二进制位进行操作。与其它高级语言相比,位运算是C语言的特点之一。

 

参考资料:

《网络游戏服务器端编程》第四章

MD5算法的C++实现:http://www.cppblog.com/ant/archive/2007/09/11/31886.html

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引