⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 destest.c

📁 应用于MCS51的DES算法,最小的RAM占用率,只要
💻 C
📖 第 1 页 / 共 3 页
字号:

/*
应用于MCS51当中的DES算法,使用最少的RAM,只要28字节

*/

#pragma OPTIMIZE(6)   	//优化级别与优化方式
#pragma SMALL				//编译模式 SAMLL  COMPACT LARGE
//#include <c:\work\omf51\inc\string.h>			//字符串函数
typedef unsigned int  uint;
typedef unsigned char uchar;
typedef unsigned char sint;
//        =========       Des算法中所使用的常数表    ===========
//初使换位表IP
code uchar  Initial_Permutation[64]={
                        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,
                        56,48,40,32,24,16, 8, 0,
                        58,50,42,34,26,18,10, 2,
                        60,52,44,36,28,20,12, 4,
                        62,54,46,38,30,22,14, 6};
//逆初使换位表IP(-1)
code uchar Initial_Permutation_Reverse[64]={
                        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,
                        32, 0,40, 8,48,16,56,24};
//换位函数 P
code uchar  Permutation_Function_P[32] ={
                        15, 6,19,20,
                        28,11,27,16,
                         0,14,22,25,
                         4,17,30, 9,
                         1, 7,23,13,
                        31,26, 2, 8,
                        18,12,29, 5,
                        21,10, 3,24};
//扩充函数 E
code uchar  Extend_Function_E[48] ={
                        31, 0, 1, 2, 3, 4,
                         3, 4, 5, 6, 7, 8,
                         7, 8, 9,10,11,12,
                        11,12,13,14,15,16,
                        15,16,17,18,19,20,
                        19,20,21,22,23,24,
                        23,24,25,26,27,28,
                        27,28,29,30,31, 0};
//换位选择表一(用于KEY的生成)
code uchar Key_Select_Permutation_1[56]={
                        56,48,40,32,24,16, 8,
                         0,57,49,41,33,25,17,
                         9, 1,58,50,42,34,26,
                        18,10, 2,59,51,43,35,
                        62,54,46,38,30,22,14,
                         6,61,53,45,37,29,21,
                        13, 5,60,52,44,36,28,
                        20,12, 4,27,19,11, 3};
//换位选择表二(用于KEY的生成)
code uchar Key_Select_Permutation_2[48]= {
                        13,16,10,23, 0, 4,
                         2,27,14, 5,20, 9,
                        22,18,11, 3,25, 7,
                        15, 6,26,19,12, 1,
                        40,51,30,36,46,54,
                        29,39,50,44,32,47,
                        43,48,38,55,33,52,
                        45,41,49,35,28,31};
//替代函数,S输出盒
code uchar    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, 9, 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, 9, 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};
code uchar    S_Box_0[4][16] ={
                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},
              S_Box_1[4][16] ={
                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},
              S_Box_2[4][16] ={
                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},
              S_Box_3[4][16] ={
                 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},
              S_Box_4[4][16] ={
                 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},
              S_Box_5[4][16]={
                12, 1,10,15, 9, 2, 6, 8, 0,13, 3, 4,14, 7, 5,11,
                10,15, 4, 2, 7,12, 9, 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},
             S_Box_6[4][16] ={
                 4,11, 2,14,15, 0, 8,13, 3,12, 9, 7, 5,10, 6, 1,
                13, 0,11, 7, 4, 9, 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},
             S_Box_7[4][16] ={
                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};
code  uchar des_bit_map[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};

//DES的标准加密算法,在时行位变换时使用BIT的变量以减少内存的消耗.
/*****************************************************************************
 *                          ===  三重DES算法  ===                            *
 *****************************************************************************
 *    三重DES算法,它是用两个密钥对明文加密/解密三次.发送者先用每一个密钥对明 *
 *  文加密,然后用第二个密钥对所形成的密文解密,最后用第一个密钥再次进行加密;  *
 *  接收者先用第一个密钥解密,用第二个密钥加密,最后用第一个密钥解密.          *
 *             C=Ek3 (Dd2 (Ek1 P))         P=Dk1 (Ek2 (Dk 3C))               *
 *                                                                           *
 *****************************************************************************/

void Des_Encrypt(uchar *plain,uchar *key)//DES算法
//入口: 明文放在 *plain N=8, 密钥放在 *key N=8
//出口: 返回密文在 *plain N=8.
{
    uchar i,j,l,Circle_Number;
    uchar byte_point,bit_point;
    uchar PlainText[8];//明文
    uchar SecretKey[8];//密鈅
    uchar PlainText_Right[8],PlainText_Left[8];//左右两个明文
    uchar Des_Temp[8];
    #define Roll_Key PlainText	//明文在完成左右变换以后,原文已不再使用

    for(i=0;i<8;i++)
    {//保存数据
    	PlainText[i]=*plain++;
    	SecretKey[i]=*key++;
    }
    //SETP 1 按初使置换表进行第一次的变换,变换后的数据为 Des_Temp[0-7]
    for(i=0;i<8;i++)
    {
        Des_Temp[i]=0;
        for(j=0;j<8;j++)
        {
            bit_point=Initial_Permutation[i*8+j];
            byte_point=bit_point/8;
            bit_point=bit_point%8;
            if((PlainText[byte_point]&des_bit_map[bit_point])!=0)
                Des_Temp[i]=Des_Temp[i]|des_bit_map[j];
        }
    }
    //SETP 2
    //将 Des_Temp[0-7]当中 8*8 的数据变换为左右两个 8*4 ,
    //放在 PlainText_Right[0-7] 与 PlainText_Left [0-7]当中
    for(i=0,j=0;i<8;i++)  //8个四位二进制
    {
        PlainText_Left[i]=Des_Temp[j];
        i++;
        PlainText_Left[i]=Des_Temp[j]<<4;
        j++;
    }
    for(i=0,j=4;i<8;i++)
    {
        PlainText_Right[i]=Des_Temp[j];
        i++;
        PlainText_Right[i]=Des_Temp[j]<<4;
        j++;
    }
    //KEY-SETP  1  Initial key
    //按换位选择表 1 对密钥进行变换,舍去BIT8的奇偶校验位.有效位为bit1-7
    //完成后存于 Des_Temp[0-7] 当中,然后再回写入 SecretKey[0-7]当中
    for(i=0;i<8;i++)
    {
        Des_Temp[i]=0;
        for(j=0;j<7;j++)
        {
            bit_point=Key_Select_Permutation_1[i*7+j];
            byte_point=bit_point/8;
            bit_point=bit_point%8;
            if((SecretKey[byte_point]&des_bit_map[bit_point])!=0)
                Des_Temp[i]=Des_Temp[i]|des_bit_map[j];
        }
    }
    for(i=0;i<8;i++)
        SecretKey[i]=Des_Temp[i];
    //进行十六次的循环迭代
    for(Circle_Number=0;Circle_Number<16;Circle_Number++)
    {
        //密钥在进行左循环移位时位的次数
        if ((Circle_Number==0)||(Circle_Number==1)||(Circle_Number==8)||(Circle_Number==15))
            l=1;
        else
            l=2;
        //KEY-SETP 2 对密钥时行左循环移位,最后存在 SecretKey[0-7]当中,总长只有7位
        //在进行左循环移位时分为两组分别进行,即 SecretKey[0-3],SecretKey[4-7]
        for(i=0;i<l;i++)
        {
            //前四个SecretKey[0-3]
            if((SecretKey[0]&0x80)!=0)
                SecretKey[3]=SecretKey[3]|0x1;
            else
                SecretKey[3]=SecretKey[3]&0xfe;
            for(j=0;j<3;j++)
            {
                SecretKey[j]=SecretKey[j]<<1;
                if((SecretKey[j+1]&0x80)!=0)
                    SecretKey[j]=SecretKey[j]|0x2;
                else
                    SecretKey[j]=SecretKey[j]&0xfc;
            }
            SecretKey[3]=SecretKey[3]<<1;
            //后四个SecretKey[4-7]
            if((SecretKey[4]&0x80)!=0)
                SecretKey[7]=SecretKey[7]|0x1;
            else
                SecretKey[7]=SecretKey[7]&0xfe;
            for(j=4;j<7;j++)
            {
                SecretKey[j]=SecretKey[j]<<1;
                if((SecretKey[j+1]&0x80)!=00)
                    SecretKey[j]=SecretKey[j]|0x2;
                else
                    SecretKey[j]=SecretKey[j]&0xfc;
            }
            SecretKey[7]=SecretKey[7]<<1;
        }
        //KEY-SETP 3 按初使化置换表 2 对密钥进行变换,完成后存于 Roll_Key[0-7]当中
        //其有效数位为 8*6
        for(i=0;i<8;i++)
        {
            Roll_Key[i]=0;
            for(j=0;j<6;j++)
            {
                bit_point=Key_Select_Permutation_2[i*6+j];
                byte_point=bit_point/7;
                bit_point=bit_point%7;
                if((SecretKey[byte_point]&des_bit_map[bit_point])!=0)
                    Roll_Key[i]=Roll_Key[i]|des_bit_map[j];
            }
        }
        //SETP 3  按扩充函数,对原文的右边 PlainText_Right[0-7]的8*4数据进行扩充,
        // 变为8*6放在 Des_Temp[0-7]当中
        for(i=0;i<8;i++)
        {
            Des_Temp[i]=0;
            for(j=0;j<6;j++)
            {
                bit_point=Extend_Function_E[i*6+j];
                byte_point=bit_point/4;
                bit_point=bit_point%4;
                if((PlainText_Right[byte_point]&des_bit_map[bit_point])!=0)
                    Des_Temp[i]=Des_Temp[i]|des_bit_map[j];
            }
        }
        //SETP 4 变换后的原文的右边 Des_Temp[0-7](8*6) 与活动密钥 Roll_Key[0-7] 进行位或操作
        for(i=0;i<8;i++)
            Des_Temp[i]=Des_Temp[i]^Roll_Key[i];
        //SETP 5 使用替代盒将8*6的数据 Des_Temp[0-7] 还原成8*4,仍存于 Des_Temp[0-7]
        //原数据为高6位,完成以后时高4位,替换盒中的两个维是[Bit8-3,Bit7-6-5-4]
        for(i=0;i<8;i++)
        {
            //ROW
            if((Des_Temp[i]&0x80)!=0)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -