目录

概述

前言

  我是用linux系统中的vim进行编辑
  用gcc进行编译
  在命令符窗口进行连接与运行
  内容:
  client.c,server.c//主函数,其中server.c为服务器端,client.c为客户端
  bool.h和tables.h//自定义的头文件,由于我使用”“进行引用,所以该头文件必须要和主函数在一个文件夹中才能正确连接。
  附注:1,先运行server,再在命令符运行./client 127.0.0.1 3490 jake //127.0.0.1为server所在的ip地址,这里是本机所以直接使用127.0.0.1,3490为对应端口,jake是创建的用户名,我在程序中设定用户名必须为4个字节。
   2,本程序支持多人聊天。可以在不同的电脑使用不同的用户名访问服务器在聊天室进行聊天。

运行截图

服务器端运行截图
服务器端运行截图
客户端运行截图
客户端运行截图

一些问题

  由于个人能力的不足,这个程序非常粗糙而且有一些问题没能解决。
  1,客户端第一次接收消息时不能翻译,出现乱码,后面就能正常运行。
  2,为了便于将服务器端发送到客户端的密文裁剪出来进行翻译,我规定了用户名的长度为4个字节,多了少了都会导致裁剪出错,从而翻译失败。
  3,不能发送汉字,并且发送的内容不能超过8个字节。

正文

服务器端

  用于接收客户端发过来的消息(密文),并将所有消息(密文)发送给所有客户端,并保存所有发来的消息。

//-------------------------服务器端server.c-------------------------------//
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h> //数据类型定义
#include <sys/stat.h> //文件属性
#include <netinet/in.h> //定义数据结构sockaddr_in
#include <sys/socket.h> //提供socket函数和数据结构
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/ipc.h>
#include <errno.h>
#include <sys/shm.h> //共享内存
#include <time.h>
#include <sys/time.h>
#include <arpa/inet.h>

#define PERM S_IRUSR | S_IWUSR //用户读写
#define MYPORT  3490 //通信端口
#define BACKLOG 10 //定义服务器段可以连接的最大客户数
#define WELCOME "|---------------Welcome to the chat room!----------------|"//当客户端连接服务端时,向客户端发送此字符串
//将int类型转换成char*类型
void itoa(int i, char *string)
{
    int mask = 1;
    while (i / mask >= 10)
        mask *= 10;
    while (mask > 0)
    {
        *string++ = i / mask + '0';
        i %= mask;
        mask /= 10;
    }
    *string = '\0';
}

//得到当前系统的时间
void get_cur_time(char *time_str)
{
    struct timeval now;
    gettimeofday(&now, NULL);
    strcpy(time_str, ctime(&now.tv_sec));
}

//创建共享存储区
int shm_create()
{
    int shmid;
    //shmid = shmget(IPC_PRIVATE, 1024, PERM);
    if ((shmid = shmget(IPC_PRIVATE, 1024, PERM)) == -1)
    {
        fprintf(stderr, "Create Share Memory Error:%s\n\a", strerror(errno));
        exit(1);
    }
    return shmid;
}

//端口绑定函数。创建套接字,并绑定到指定端口
int bindPort(unsigned short int port)
{
    int sockfd;
    struct sockaddr_in my_addr;
    sockfd = socket(AF_INET, SOCK_STREAM, 0); //创建基于流套接字
    //bzero(&(my_addr.sin_zero),0);
    bzero(&my_addr, sizeof(my_addr));
    my_addr.sin_family = AF_INET; //IPV4协议族
    my_addr.sin_port = htons(port); //转换端口为网络字节序
    my_addr.sin_addr.s_addr = INADDR_ANY;

    if (bind(sockfd, (struct sockaddr*)&my_addr, sizeof(struct sockaddr)) == -1)
    {
        perror("fail to bind");
        exit(1);
    }

    printf("bind success!\n");
    return sockfd;
}

int main(int argc, char *argv[])
{
    int sockfd, clientfd; //监听套接字、客户套接字
    int sin_size, recvbytes;

    pid_t pid, ppid; //定义父子进程标记
    char *buf, *read_addr, *write_addr, *temp, *time_str; //需要用到的缓冲区
    struct sockaddr_in their_addr; //定义地址结构
    int shmid;

    shmid = shm_create(); //创建共享存储区

    temp = (char *)malloc(255);
    time_str = (char *)malloc(50);
    sockfd = bindPort(MYPORT); //绑定端口

    get_cur_time(time_str);
    printf("Time is : %s\n", time_str);

    if (listen(sockfd, BACKLOG) == -1)
    {
        //在指定端口上监听
        perror("fail to listen");
        exit(1);
    }

    printf("listen....\n");
    while (1)
    {
        /*
        if (listen(sockfd, BACKLOG) == -1)
        {
            perror("fail to listen");
            exit(1);
        }
        */
        //接受一个客户端的连接请求
        if ((clientfd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1)
        {
            perror("fail to accept");
            exit(1);
        }

        //得到客户端的IP地址输出
        char address[20];
        inet_ntop(AF_INET, &their_addr.sin_addr, address, sizeof(address));

        printf("accept from %s\n", address);
        send(clientfd, WELCOME, strlen(WELCOME), 0); //发送问候信息
        buf = (char *)malloc(255);

        ppid = fork(); //创建子进程
        if (ppid == 0) //子进程
        {
            pid = fork(); //子进程创建子进程
            while (1)
            {
                if (pid > 0)
                {
                    //buf = (char *)malloc(255);
                    //父进程用于接收信息
                    memset(buf, 0, 255);
                    printf("OK\n");
                    if ((recvbytes = recv(clientfd, buf, 255, 0)) <= 0)
                    {
                        perror("fail to recv");
                        close(clientfd);
                        raise(SIGKILL);
                        exit(1);
                    }
                    write_addr = shmat(shmid, 0, 0); //shmat将shmid所代表的全局的共享存储区关联到本进程的进程空间
                    memset(write_addr, '\0', 1024);

                    //把接收到的消息存入共享存储区中
                    strncpy(write_addr, buf, 1024);

                    //把接收到的消息连接此刻的时间字符串输出到标准输出
                    get_cur_time(time_str);
                    strcat(buf, time_str);
                    printf("%s\n", buf);
                }
                else if (pid == 0)
                {
                    //子进程用于发送消息
                    sleep(1); //子进程先等待父进程把接收到的信息存入共享存储区
                    read_addr = shmat(shmid, 0, 0); //读取共享存储区的内容

                    //temp存储上次读取过的内容,每次先判断是否已经读取过该消息
                    if (strcmp(temp, read_addr) != 0)
                    {
                        strcpy(temp, read_addr); //更新temp,表示已经读取过该消息

                        get_cur_time(time_str);
                        strcat(read_addr, time_str);
                        if (send(clientfd, read_addr, strlen(read_addr), 0) == -1)
                        {
                            perror("fail to send");
                            exit(1);
                        }
                        memset(read_addr, '\0', 1024);
                        strcpy(read_addr, temp);
                    }
                }
                else
                    perror("fai to fork");
            }
        }
    }
    printf("------------------------------------\n");
    free(buf);
    close(sockfd);
    close(clientfd);
    return 0;
}

客户端

  将输入的明文(小于8字节)转化成密文(DES加密)发送给服务器端,同时接收来自服务器端的消息(密文)将其翻译成明文。

#include <stdio.h>
#include <netinet/in.h> //定义数据结构sockaddr_in
#include <sys/socket.h> //定义socket函数以及数据结构
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <netdb.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <assert.h>
#include "bool.h"   // 位处理 
#include "tables.h"

void BitsCopy(bool *DatOut,bool *DatIn,int Len);  // 数组复制 

void ByteToBit(bool *DatOut,char *DatIn,int Num); // 字节到位 
void BitToByte(char *DatOut,bool *DatIn,int Num); // 位到字节

void BitToHex(char *DatOut,bool *DatIn,int Num);  // 二进制到十六进制 64位 to 4*16字符
void HexToBit(bool *DatOut,char *DatIn,int Num);  // 十六进制到二进制 

void TablePermute(bool *DatOut,bool *DatIn,const char *Table,int Num); // 位表置换函数 
void LoopMove(bool *DatIn,int Len,int Num);     // 循环左移 Len长度 Num移动位数 
void Xor(bool *DatA,bool *DatB,int Num);         // 异或函数 

void S_Change(bool DatOut[32],bool DatIn[48]);   // S盒变换 
void F_Change(bool DatIn[32],bool DatKi[48]);    // F函数                                  

void SetKey(char KeyIn[8]);                         // 设置密钥
void PlayDes(char MesOut[8],char MesIn[8]);       // 执行DES加密
void KickDes(char MesOut[8],char MesIn[8]);             // 执行DES解密 
char* mysubstr(char* srcstr, int offset, int length);   //裁剪字符串函数
int main(int argc, char *argv[])//程序名,服务器ip地址,套接字,用户名
{
    int i=0; 
    char MesHex[16]={0};         // 16个字符数组用于存放 64位16进制的密文
    char MyKey[8]={'1','2','3','4','5','6','7','8'};           // 初始密钥 8字节*8需要提前设定
    char YourKey[8]={'1','2','3','4','5','6','7','8'};         // 输入的解密密钥 8字节*8
    char MyMessage[8]={0};       // 初始明文 
    char *My=MyMessage;//用于接收服务器信息时接收裁剪后的指针
    struct sockaddr_in clientaddr; //定义地址结构
    pid_t pid;
    int clientfd, sendbytes, recvbytes;
    struct hostent *host; //主机信息数据结构
    char *buf;
    if (argc < 4)
    {
        printf("wrong usage");
        printf("%s host port name\n", argv[0]);
        exit(1);
    }
    host = gethostbyname(argv[1]);
    if ((clientfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        perror("fail to create socket");
        exit(1);
    }
    bzero(&clientaddr, sizeof(clientaddr));
    clientaddr.sin_family = AF_INET;
    clientaddr.sin_port = htons((uint16_t)atoi(argv[2]));
    clientaddr.sin_addr = *((struct in_addr *)host->h_addr);
    //客户端连接服务端
    if (connect(clientfd, (struct sockaddr *)&clientaddr, sizeof(struct sockaddr)) == -1)
    {
        perror("fail to connect");
        exit(1);
    }
    buf = (char *)malloc(120);//为buf分配120个字节空间
    My =(char *)malloc(120);//为My分配120个字节空间
    memset(buf, 0, 120);//将buf内的120个字节空间全部初始化为NULL(ASCII中的0)

    if (recv(clientfd, buf, 100, 0) == -1)//将buf里面的内容发送给服务器端,如果失败,执行相应操作
    {
        perror("fail to recv");
        exit(1);
    }
    //printf("\n%s\n", buf);
    printf("请输入8字节以内的话:\n");
    pid = fork();//克隆一个进程
    while (1)
    {
        if (pid > 0)
        {
            //父进程发送消息
            strcpy(buf, argv[3]);//截取用户名,将他加入消息前方
            strcat(buf, ":");
            fgets(MyMessage,40,stdin);            // 明文
            SetKey(MyKey);               // 设置密钥 得到子密钥Ki
            PlayDes(MesHex,MyMessage);   // 执行DES加密
            printf("生成密文:");  // 信息已加密
            for(i=0;i<16;i++)           
            {
                printf("%c ",MesHex[i]);
            }
            printf("\n\n");
            strncat(buf, MesHex, strlen(MesHex) - 1);
            if ((sendbytes = send(clientfd, buf, strlen(buf), 0)) == -1)
            {
                perror("fail to send");
                exit(1);
            }
        }
        else if (pid == 0)
        {
            //子进程接受消息
            memset(buf, 0, 100);
            if (recv(clientfd, buf, 100, 0) <= 0)
            {
                perror("fail to recv");
                close(clientfd);
                raise(SIGSTOP);
                exit(1);
            }
            SetKey(YourKey);                                       // 设置密钥
            //KickDes(MyMessage,MesHex);                     // 解密输出到MyMessage
            printf("接收到信息:%s",buf);
            My=mysubstr(buf,5,16);
            printf("翻译成明文:");
            KickDes(MyMessage,My);                     // 解密输出到MyMessage
            for(i=0;i<8;i++)
            {
                printf("%c",MyMessage[i]);
            }
            //printf("%s\n", buf);
        }
        else
            perror("fork error");
    }
    close(clientfd);
    return 0;
}

/*-------------------------------
 把DatIn开始的长度位Len位的二进制
 复制到DatOut后
--------------------------------*/
void BitsCopy(bool *DatOut,bool *DatIn,int Len)     // 数组复制 OK 
{
    int i=0;
    for(i=0;i<Len;i++)
    {
        DatOut[i]=DatIn[i];
    }
}

/*-------------------------------
 字节转换成位函数 
 每8次换一个字节 每次向右移一位
 和1与取最后一位 共64位 
--------------------------------*/
void ByteToBit(bool *DatOut,char *DatIn,int Num)       // OK
{
    int i=0;
    for(i=0;i<Num;i++)
    {
        DatOut[i]=(DatIn[i/8]>>(i%8))&0x01;   
    }                                       
}

/*-------------------------------
 位转换成字节函数
 字节数组每8次移一位
 位每次向左移 与上一次或   
---------------------------------*/
void BitToByte(char *DatOut,bool *DatIn,int Num)        // OK
{
    int i=0;
    for(i=0;i<(Num/8);i++)
    {
        DatOut[i]=0;
    } 
    for(i=0;i<Num;i++)
    {
        DatOut[i/8]|=DatIn[i]<<(i%8);    
    }        
}


/*----------------------------------
 二进制密文转换为十六进制
 需要16个字符表示
-----------------------------------*/
void BitToHex(char *DatOut,bool *DatIn,int Num)
{
    int i=0;
    for(i=0;i<Num/4;i++)
    {
        DatOut[i]=0;
    }
    for(i=0;i<Num/4;i++)
    {
        DatOut[i] = DatIn[i*4]+(DatIn[i*4+1]<<1)
                    +(DatIn[i*4+2]<<2)+(DatIn[i*4+3]<<3);
        if((DatOut[i]%16)>9)
        {
            DatOut[i]=DatOut[i]%16+'7';       //  余数大于9时处理 10-15 to A-F
        }                                     //  输出字符 
        else
        {
            DatOut[i]=DatOut[i]%16+'0';       //  输出字符       
        }
    }

}

/*---------------------------------------------
 十六进制字符转二进制
----------------------------------------------*/
void HexToBit(bool *DatOut,char *DatIn,int Num)
{
    int i=0;                        // 字符型输入 
    for(i=0;i<Num;i++)
    {
        if((DatIn[i/4])>'9')         //  大于9 
        {
            DatOut[i]=((DatIn[i/4]-'7')>>(i%4))&0x01;               
        }
        else
        {
            DatOut[i]=((DatIn[i/4]-'0')>>(i%4))&0x01;     
        } 
    }    
}

// 表置换函数  OK
void TablePermute(bool *DatOut,bool *DatIn,const char *Table,int Num)  
{
    int i=0;
    static bool Temp[256]={0};
    for(i=0;i<Num;i++)                // Num为置换的长度 
    {
        Temp[i]=DatIn[Table[i]-1];  // 原来的数据按对应的表上的位置排列 
    }
    BitsCopy(DatOut,Temp,Num);       // 把缓存Temp的值输出 
} 

// 子密钥的移位
void LoopMove(bool *DatIn,int Len,int Num) // 循环左移 Len数据长度 Num移动位数
{
    static bool Temp[256]={0};    // 缓存   OK
    BitsCopy(Temp,DatIn,Num);       // 将数据最左边的Num位(被移出去的)存入Temp 
    BitsCopy(DatIn,DatIn+Num,Len-Num); // 将数据左边开始的第Num移入原来的空间
    BitsCopy(DatIn+Len-Num,Temp,Num);  // 将缓存中移出去的数据加到最右边 
} 

// 按位异或
void Xor(bool *DatA,bool *DatB,int Num)           // 异或函数
{
    int i=0;
    for(i=0;i<Num;i++)
    {
        DatA[i]=DatA[i]^DatB[i];                  // 异或 
    }
} 

// 输入48位 输出32位 与Ri异或
void S_Change(bool DatOut[32],bool DatIn[48])     // S盒变换
{
    int i,X,Y;                                    // i为8个S盒 
    for(i=0,Y=0,X=0;i<8;i++,DatIn+=6,DatOut+=4)   // 每执行一次,输入数据偏移6位 
    {                                              // 每执行一次,输出数据偏移4位
        Y=(DatIn[0]<<1)+DatIn[5];                          // af代表第几行
        X=(DatIn[1]<<3)+(DatIn[2]<<2)+(DatIn[3]<<1)+DatIn[4]; // bcde代表第几列
        ByteToBit(DatOut,&S_Box[i][Y][X],4);      // 把找到的点数据换为二进制    
    }
}

// F函数
void F_Change(bool DatIn[32],bool DatKi[48])       // F函数
{
    static bool MiR[48]={0};             // 输入32位通过E选位变为48位
    TablePermute(MiR,DatIn,E_Table,48); 
    Xor(MiR,DatKi,48);                   // 和子密钥异或
    S_Change(DatIn,MiR);                 // S盒变换
    TablePermute(DatIn,DatIn,P_Table,32);   // P置换后输出
}



void SetKey(char KeyIn[8])               // 设置密钥 获取子密钥Ki 
{
    int i=0;
    static bool KeyBit[64]={0};                // 密钥二进制存储空间 
    static bool *KiL=&KeyBit[0],*KiR=&KeyBit[28];  // 前28,后28共56
    ByteToBit(KeyBit,KeyIn,64);                    // 把密钥转为二进制存入KeyBit 
    TablePermute(KeyBit,KeyBit,PC1_Table,56);      // PC1表置换 56次
    for(i=0;i<16;i++)
    {
        LoopMove(KiL,28,Move_Table[i]);       // 前28位左移 
        LoopMove(KiR,28,Move_Table[i]);          // 后28位左移 
         TablePermute(SubKey[i],KeyBit,PC2_Table,48); 
         // 二维数组 SubKey[i]为每一行起始地址 
         // 每移一次位进行PC2置换得 Ki 48位 
    }        
}

void PlayDes(char MesOut[8],char MesIn[8])  // 执行DES加密
{                                           // 字节输入 Bin运算 Hex输出 
    int i=0;
    static bool MesBit[64]={0};        // 明文二进制存储空间 64位
    static bool Temp[32]={0};
    static bool *MiL=&MesBit[0],*MiR=&MesBit[32]; // 前32位 后32位 
    ByteToBit(MesBit,MesIn,64);                 // 把明文换成二进制存入MesBit
    TablePermute(MesBit,MesBit,IP_Table,64);    // IP置换 
    for(i=0;i<16;i++)                       // 迭代16次 
    {
        BitsCopy(Temp,MiR,32);            // 临时存储
        F_Change(MiR,SubKey[i]);           // F函数变换
        Xor(MiR,MiL,32);                  // 得到Ri 
        BitsCopy(MiL,Temp,32);            // 得到Li 
    }                    
    TablePermute(MesBit,MesBit,IPR_Table,64);
    BitToHex(MesOut,MesBit,64);
}

void KickDes(char MesOut[8],char MesIn[8])       // 执行DES解密
{                                                // Hex输入 Bin运算 字节输出 
    int i=0;
    static bool MesBit[64]={0};        // 密文二进制存储空间 64位
    static bool Temp[32]={0};
    static bool *MiL=&MesBit[0],*MiR=&MesBit[32]; // 前32位 后32位
    HexToBit(MesBit,MesIn,64);                 // 把密文换成二进制存入MesBit
    TablePermute(MesBit,MesBit,IP_Table,64);    // IP置换 
    for(i=15;i>=0;i--)
    {
        BitsCopy(Temp,MiL,32);
        F_Change(MiL,SubKey[i]);
        Xor(MiL,MiR,32);
        BitsCopy(MiR,Temp,32);
    }    
    TablePermute(MesBit,MesBit,IPR_Table,64);
    BitToByte(MesOut,MesBit,64);        
}
char* mysubstr(char* srcstr, int offset, int length) {
    assert(length > 0);
    assert(srcstr != NULL);

    int total_length = strlen(srcstr);//首先获取srcstr的长度
    //判断srcstr的长度减去需要截取的substr开始位置之后,剩下的长度
    //是否大于指定的长度length,如果大于,就可以取长度为length的子串
    //否则就把从开始位置剩下的字符串全部返回。
    int real_length = ((total_length - offset) >= length ? length : (total_length - offset)) + 1;
    char *tmp;
    if (NULL == (tmp=(char*) malloc(real_length * sizeof(char)))) {
        printf("Memory overflow . \n");
        exit(0);
    }
    strncpy(tmp, srcstr+offset, real_length - 1);
    tmp[real_length - 1] = '\0';

    return tmp;
}

头文件

tables.h

  关于DES加密的置换表

/*-------------------------------------------------------------
        置换表 
-------------------------------------------------------------*/

#ifndef _TABLES_H_    // 防重复编译 
#define _TABLES_H_  

// 对明文执行IP置换得到L0,R0 (L左32位,R右32位)               [明文操作]
const char IP_Table[64]={             
    58,50,42,34,26,18,10, 2,60,52,44,36,28,20,12, 4,
    62,54,46,38,30,22,14, 6,64,56,48,40,32,24,16, 8,
    57,49,41,33,25,17, 9, 1,59,51,43,35,27,19,11, 3,
    61,53,45,37,29,21,13, 5,63,55,47,39,31,23,15, 7 
};

// 对迭代后的L16,R16执行IP逆置换,输出密文
const char IPR_Table[64]={              
    40, 8,48,16,56,24,64,32,39, 7,47,15,55,23,63,31,
    38, 6,46,14,54,22,62,30,37, 5,45,13,53,21,61,29,
    36, 4,44,12,52,20,60,28,35, 3,43,11,51,19,59,27,
    34, 2,42,10,50,18,58,26,33, 1,41, 9,49,17,57,25    
};

/*--------------------------- 迭代法则 ----------------------------*/ 

// F函数,32位的R0进行E变换,扩为48位输出 (R1~R16)        [备用A]  [明文操作] 
static char E_Table[48]={
    32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9,
     8, 9,10,11,12,13,12,13,14,15,16,17,
    16,17,18,19,20,21,20,21,22,23,24,25,
    24,25,26,27,28,29,28,29,30,31,32, 1
};

// 子密钥K(i)的获取 密钥为K 抛弃第6,16,24,32,40,48,64位          [密钥操作] 
// 用PC1选位 分为 前28位C0,后28位D0 两部分  
static char PC1_Table[56]={
    57,49,41,33,25,17, 9, 1,58,50,42,34,26,18,
    10, 2,59,51,43,35,27,19,11, 3,60,52,44,36,
    63,55,47,39,31,23,15, 7,62,54,46,38,30,22,
    14, 6,61,53,45,37,29,21,13, 5,28,20,12, 4
};

// 对C0,D0分别进行左移,共16次,左移位数与下面对应                 [密钥操作]
static char Move_Table[16]={
     1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
};

// C1,D1为第一次左移后得到,进行PC2选位,得到48位输出K1   [备用B]   [密钥操作]     
static char PC2_Table[48]={
    14,17,11,24, 1, 5, 3,28,15, 6,21,10,
    23,19,12, 4,26, 8,16, 7,27,20,13, 2,
    41,52,31,37,47,55,30,40,51,34,33,48,
    44,49,39,56,34,53,46,42,50,36,29,32    
};

/*------------- F函数 备用A和备用B 异或 得到48位输出 ---------------*/ 

// 异或后的结果48位分为8组,每组6位,作为8个S盒的输入             [组合操作] 
// S盒以6位作为输入(8组),4位作为输出(4*(8组)=32位)
// S工作原理 假设输入为A=abcdef ,则bcde所代表的数是0-15之间的
// 一个数记为 X=bcde ,af代表的是0-3之间的一个数,记为 Y=af 
// 在S1的X列,Y行找到一个数Value,它在0-15之间,可以用二进制表示
// 所以为4bit (共32位)  
static char S_Box[8][4][16]={
    //S1
    14, 4,13, 1, 2,15,11, 8, 3,10, 6,12, 5, 9, 0, 7,
     0,15, 7, 4,14, 2,13, 1,10, 6,12,11, 9, 5, 3, 8,
     4, 1,14, 8,13, 6, 2,11,15,12, 9, 7, 3,10, 5, 0,
    15,12, 8, 2, 4, 9, 1, 7, 5,11, 3,14,10, 0, 6,13,
    //S2
    15, 1, 8,14, 6,11, 3, 4, 9, 7, 2,13,12, 0, 5,10,
     3,13, 4, 7,15, 2, 8,14,12, 0, 1,10, 6, 9,11, 5,
     0,14, 7,11,10, 4,13, 1, 5, 8,12, 6, 9, 3, 2,15,
    13, 8,10, 1, 3,15, 4, 2,11, 6, 7,12, 0, 5,14, 9,
    //S3
    10, 0, 9,14, 6, 3,15, 5, 1,13,12, 7,11, 4, 2, 8,
    13, 7, 0, 9, 3, 4, 6,10, 2, 8, 5,14,12,11,15, 1,
    13, 6, 4, 9, 8,15, 3, 0,11, 1, 2,12, 5,10,14, 7,
     1,10,13, 0, 6, 9, 8, 7, 4,15,14, 3,11, 5, 2,12,
    //S4
     7,13,14, 3, 0, 6, 9,10, 1, 2, 8, 5,11,12, 4,15,
    13, 8,11, 5, 6,15, 0, 3, 4, 7, 2,12, 1,10,14, 9,
    10, 6, 9, 0,12,11, 7,13,15, 1, 3,14, 5, 2, 8, 4,
     3,15, 0, 6,10, 1,13, 8, 9, 4, 5,11,12, 7, 2,14,
    //S5
     2,12, 4, 1, 7,10,11, 6, 8, 5, 3,15,13, 0,14, 9,
    14,11, 2,12, 4, 7,13, 1, 5, 0,15,10, 3, 9, 8, 6,
     4, 2, 1,11,10,13, 7, 8,15, 9,12, 5, 6, 3, 0,14,
    11, 8,12, 7, 1,14, 2,13, 6,15, 0, 9,10, 4, 5, 3,
    //S6
    12, 1,10,15, 9, 2, 6, 8, 0,13, 3, 4,14, 7, 5,11,
    10,15, 4, 2, 7,12, 0, 5, 6, 1,13,14, 0,11, 3, 8,
     9,14,15, 5, 2, 8,12, 3, 7, 0, 4,10, 1,13,11, 6,
        4, 3, 2,12, 9, 5,15,10,11,14, 1, 7, 6, 0, 8,13,
    //S7
     4,11, 2,14,15, 0, 8,13, 3,12, 9, 7, 5,10, 6, 1,
    13, 0,11, 7, 4, 0, 1,10,14, 3, 5,12, 2,15, 8, 6,
     1, 4,11,13,12, 3, 7,14,10,15, 6, 8, 0, 5, 9, 2,
     6,11,13, 8, 1, 4,10, 7, 9, 5, 0,15,14, 2, 3,12,
    //S8
    13, 2, 8, 4, 6,15,11, 1,10, 9, 3,14, 5, 0,12, 7,
     1,15,13, 8,10, 3, 7, 4,12, 5, 6,11, 0,14, 9, 2,
     7,11, 4, 1, 9,12,14, 2, 0, 6,10,13,15, 3, 5, 8,
     2, 1,14, 7, 4,10, 8,13,15,12, 9, 0, 3, 5, 6,11
};

// F函数 最后第二步,对S盒输出的32进行P置换                     [组合操作]
// 输出的值参与一次迭代:
// L(i)=R(i-1)
// R(i)=L(i-1)^f(R(i-1),K(i)) 异或 
static char P_Table[32]={
    16, 7,20,21,29,12,28,17, 1,15,23,26, 5,18,31,10,
     2, 8,24,14,32,27, 3, 9,19,13,30, 6,22,11, 4,25
};

// 16个子密钥K(1~16) 
static bool SubKey[16][48]={0};

#endif

bool.h

  关于DES加密


#ifndef __BOOL_H__
#define __BOOL_H__

typedef enum
  {
    false = 0,
    true  = 1
  } bool;

#endif

参考资料

  Linux C实现简单的网络聊天室
  实验一:C语言实现DES加解密算法

Logo

更多推荐