linux c crypto


原文链接: linux c crypto

https://gist.github.com/neikeq/d6d00bd24db606dd0b86

#ifndef _DEFINE_H_
#define _DEFINE_H_

#define OK                          0
#define NOK                         -1
#define YES                         1
#define NO                          0

#define MAX_SERVER                  50
#define MAX_PLAYER                  500
#define MAX_ROOM                    150
#define MAX_TEAM                    150

#define MAX_VIEWER                  4
#define MAX_ATHLETE                 10
#define MAX_ITEM                    70
#define MAX_LEVEL                   50
#define MAX_LEVEL_SECTION           (MAX_LEVEL/5)+1
#define MAX_TIP                     50
#define MAX_MISSION                 46
#define MAX_HOLIDAY                 50
#define MAX_CARDSKILL               3
#define MAX_CARDRANK                4
#define MAX_BUDDY                   30
#define MAX_BLACKLIST               30

#define EMBLEM_TYPE_GOLD            604101101
#define EMBLEM_TYPE_SILVER          604101102

#define MAX_PATH                    255

#define GAME_PLAY_TIME              300

#define ID_NAME_SIZE                (15 * 2)    + 1
#define PLAYER_NAME_SIZE            (7 * 2)     + 1
#define OBJECT_NAME_SIZE            (20 * 2)    + 1
#define SERVER_NAME_SIZE            (15 * 2)    + 1
#define TITLE_NAME_SIZE             (23 * 2)    + 1
#define CLUB_NAME_SIZE              (10 * 2)    + 1
#define PLAYER_MENT_SIZE            (22 * 2)    + 1
#define MESSAGE_SIZE                (40 * 2)    + 1
#define PASS_SIZE                   (10 * 2)    + 1
#define LOGIN_PASS_SIZE             (32 * 2)    + 1
#define TIP_SIZE                    (60 * 2)    + 1
#define MISSTION_TEXT_SIZE          (60 * 2)    + 1

#define MAX_MUSIC_COUNT             3

#define MAX_EQUIP                   17
#define MAX_FACULTY                 130
#define MAX_CHARACTER               3
#define MAX_INVEN                   80
#define MAX_SKILL                   50
#define MAX_CEREMONY                5
#define MAX_CARD                    100
#define MAX_SCHEDULE_LIST           10
#define MAX_BUDDY_LIST              10

#define MAX_ITEM_LIST               10
#define MAX_ITEM_LIST               10
#define MAX_TRAINING_LIST           10
#define MAX_CEREMONEY_LIST          5
#define MAX_SKILL_LIST              50
#define MAX_QUEST_LIST              10
#define MAX_CARD_LIST               30

#define PACKET_SIZE                 2048

#define HEAD_SIZE                   (int)sizeof(Header)
#define IP_SIZE                     20
#define TEAM_SIZE                   6

#define ITEM_OPTION_SIZE            5

#define SLOT_SIZE                   4
#define BASE_CHARACTER_SIZE         1
#define BASE_INVEN_SIZE             20
#define BASE_SKILL_SIZE             8
#define ITEM1_MIX_SIZE              5
#define ITEM2_MIX_SIZE              5
#define CARD_MIX_SIZE               12

#define LIST5_SIZE                  5
#define LIST6_SIZE                  6
#define LIST8_SIZE                  8
#define LIST10_SIZE                 10

#define TEAM_SIZE                   6

#define SHOP_BIT_EXIST              0
#define SHOP_BIT_NONE               1

#define ITEM_FACE                   100
#define ITEM_HAIR                   101
#define ITEM_SHIRTS                 102
#define ITEM_PANTS                  103
#define ITEM_GLOVE                  104
#define ITEM_SHOES                  105

#define ITEM_DIGIT                  1000000

#define OPTION_DIGIT                100000

#define POSITION_NONE               0
#define POSITION_ALL                1

#define POSITION_FW                 10
#define POSITION_ST                 11
#define POSITION_CF                 12
#define POSITION_WF                 13
#define POSITION_SS                 14
#define POSITION_FW_RANGE           4

#define POSITION_MF                 20
#define POSITION_AM                 21
#define POSITION_CM                 22
#define POSITION_SM                 23
#define POSITION_DM                 24
#define POSITION_MF_RANGE           4

#define POSITION_DF                 30
#define POSITION_SW                 31
#define POSITION_CB                 32
#define POSITION_SB                 33
#define POSITION_DF_RANGE           3

#define POSITION_GK                 40
#define POSITION_BG                 50

#define AUTO_FACULTY_LEVEL1         10
#define AUTO_FACULTY_LEVEL2         20

#define MAX_AI_COSTUME              61

#define PC_EXIT01                   1
#define PC_EXIT02                   2
#define PC_EXIT03                   3
#define PC_EXIT04                   4
#define PC_EXIT11                   11
#define PC_EXIT12                   12
#define PC_EXIT20                   20
#define PC_EXIT21                   21
#define PC_EXIT22                   22
#define PC_EXIT30                   30
#define PC_EXIT31                   31
#define PC_EXIT32                   32
#define PC_EXIT33                   33
#define PC_EXIT90                   90

#define ROOT2                       1.414
#define PROTOCOL_MAGIC              (('O'<<24)+('G'<<16)+('R'<<8)+'E')

#define MAIN                        100
#define AAA                         200
#define BBB                         300

#endif

crypto.h

#ifndef SECURE_H_
#define SECURE_H_

#include "struct_types.h"

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX_SECURE_SEQ 65535

char SECURE_ARRAY[130] =
{
    'C', 'a', 'r', 'p', 'e', 'd', 'i', 'e', 'm', 'S',
    'e', 'i', 'z', 'e', 'T', 'h', 'e', 'D', 'a', 'y',
    'B', 'o', 'y', 's', 'M', 'a', 'k', 'e', 'Y', 'o',
    'u', 'r', 'L', 'i', 'v', 'e', 's', 'E', 'x', 't',
    'r', 'a', 'o', 'd', 'i', 'n', 'a', 'r', 'y', 'W',
    'O', 'r', 'k', 'H', 'a', 'r', 'd', '!', 'H', 'a',
    'v', 'e', 'F', 'u', 'n', '!', 'M', 'a', 'k', 'e',
    'H', 'i', 's', 't', 'o', 'r', 'y', '!', 'B', 'e',
    'T', 'h', 'e', 'C', 'h', 'a', 'n', 'g', 'e', 'Y',
    'o', 'u', 'W', 'a', 'n', 'n', 'a', 'S', 'e', 'e',
    'I', 'n', 'T', 'h', 'e', 'W', 'o', 'r', 'l', 'd',
    'B', 'y', ' ', 'M', 'u', 't', 't', 'e', 'r', ' ',
    '2', '0', '0', '9', '.', '0', '7', '@', '!', '#'
};

int send_seq;
int recv_seq;

int encrypt(char* buf, int len);
int decrypt(char* buf, int len);
int make_key(int* seq);
int make_send_seq();
int make_recv_seq();

int init_seq();
int init_seq(int seq);
int make_send_packet(HeadPacket* packet);
int make_recv_packet(HeadPacket* packet);

#endif

crypto.c

#include "crypto.h"

int init_seq()
{
    time_t t; time(&t);
    srand((int)t);
    int nSeed = rand() % MAX_SECURE_SEQ;
    init_seq(nSeed);
    return nSeed;
}

int init_seq(int seq)
{
    recv_seq = seq;
    send_seq = seq;
    return seq;
}

int make_key(int* seq)
{
    (*seq)++;
    if (*seq > MAX_SECURE_SEQ)*seq = 0;
    int nArray1 = (*seq) % 128;
    int nArray2 = (*seq/127) % 128;
    int nResult = ((*seq) ^ (SECURE_ARRAY[nArray1] * SECURE_ARRAY[nArray2]));
    return nResult;
}

int make_send_seq()
{
    return make_key(&send_seq);
}

int make_recv_seq()
{
    return make_key(&recv_seq);
}

void make_send_packet(HeadPacket* packet)
{
    if (packet == NULL) return -1;
    char* buf = (char*)packet;
    int sendSeq = make_send_seq();
    packet->mSequence = sendSeq;
    encrypt(&buf[HEAD_SIZE], packet->mBodySize);
}

void make_recv_packet(HeadPacket* packet)
{
    if (packet == NULL) return -1;
    char* buf = (char*)packet;
    int recvSeq = make_recv_seq();

    if (packet->mSequence != recvSeq) return -2;
    decrypt(&buf[HEAD_SIZE], packet->mBodySize);
}

void encrypt(char* buf, int len)
{
    int nArray1 = send_seq % 128;
    int nArray2 = (send_seq / 127) % 128;
    if (nArray1 == nArray2) nArray2++;
    int i = 0;
    for (i = 0; i < len; i++)
    {
        buf[i] = buf[i] ^ SECURE_ARRAY[nArray1];
        buf[i] = buf[i] ^ SECURE_ARRAY[nArray2];
        nArray1++; if (nArray1 > 127) nArray1 = 0;
        nArray2++; if (nArray2 > 127) nArray2 = 0;
    }
}

void decrypt(char* buf, int len)
{
    int nArray1 = recv_seq % 128;
    int nArray2 = (recv_seq / 127) % 128;
    if (nArray1 == nArray2) nArray2++;
    int i = 0;
    for (i = 0; i < len; i++)
    {
        buf[i] = buf[i] ^ SECURE_ARRAY[nArray2];
        buf[i] = buf[i] ^ SECURE_ARRAY[nArray1];
        nArray1++; if (nArray1 > 127) nArray1 = 0;
        nArray2++; if (nArray2 > 127) nArray2 = 0;
    }
}
`