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

📄 des_myself.c

📁 DES加密和解密算法源代码
💻 C
📖 第 1 页 / 共 2 页
字号:


/***********************E置换--32位膨胀为48位(begin)**************************/
void expand(unsigned char array[64])
{
    int i;
    unsigned char temp[64];
    for(i=0;i<48;i++) temp[i]=array[e[i]-1];
    for(i=0;i<48;i++) array[i]=temp[i];
}
/**********************E置换--32位膨胀为48位(end)*****************************/




/*******************************P置换(begin)**********************************/
void p_permute(unsigned char array[64])
{
    int i;
    unsigned char temp[64];
    for(i=0;i<32;i++) temp[i]=array[p[i]-1];
    for(i=0;i<32;i++) array[i]=temp[i];
}
/******************************P置换(end)*************************************/





/*******************************函数f(begin)**********************************/
void f(unsigned char r_byte[64],unsigned char k_bit[8])
{
    int i,j;
    int row, col;
    unsigned char r_bit[8]={0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};
    unsigned char temp_bit[8]={0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};
    unsigned char temp_byte[64];
    unsigned char s_out_bit[8]={0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0} ;

    expand(r_byte);     /*now r is 48 bits */
    byte2bit(r_byte,r_bit);
    for(i=0;i<6;i++) r_bit[i]^=k_bit[i];     /*xor 48bit key*/
    bit2byte(r_bit,r_byte);

    for(i=0;i<8;i++)
    {
        /**********change 48bit(8bit x 6byte) to 48bit(6bit x 8byte)********/
        for(j=0;j<64;j++) temp_byte[j] = 0x0 ;/*clear temp*/
        temp_byte[6] = r_byte[i*6] ;       /*get bit 0 in 6 bit*/
        temp_byte[7] = r_byte[i*6+5] ;     /*get bit 5 in 6 bit*/
        byte2bit(temp_byte,temp_bit) ;     /* 0000 00?? */
        row = temp_bit[0] ;                /*get row[i]*/

        for(j=0;j<64;j++) temp_byte[j] = 0x0 ;/*clear temp*/
        temp_byte[4] = r_byte[i*6+1] ;   /*0000 ????*/
        temp_byte[5] = r_byte[i*6+2] ;
        temp_byte[6] = r_byte[i*6+3] ;
        temp_byte[7] = r_byte[i*6+4] ;
        byte2bit(temp_byte,temp_bit) ;
        col = temp_bit[0] ;    /*get column in S table*/
        /**********change 48bit(8bit x 6byte) to 48bit(6bit x 8byte)********/

        /*get number from S table with row and col*/
        switch (i) {
            case 0 :
                s_out_bit[i] = s1[row][col] ;
                break ;
            case 1 :
                s_out_bit[i] = s2[row][col] ;
                break ;
            case 2 :
                s_out_bit[i] = s3[row][col] ;
                break ;
            case 3 :
                s_out_bit[i] = s4[row][col] ;
                break ;
            case 4 :
                s_out_bit[i] = s5[row][col] ;
                break ;
            case 5 :
                s_out_bit[i] = s6[row][col] ;
                break ;
            case 6 :
                s_out_bit[i] = s7[row][col] ;
                break ;
            case 7 :
                s_out_bit[i] = s8[row][col] ;
                break ;
            } ;
    } /*s_out_bit[0-7]:0000???? 0000???? 0000???? 0000????...0000???? */

    /*change 64bit to 32bit : clean 0000(high 4bit)*/
    r_bit[0] = (s_out_bit[0]<<4) + s_out_bit[1] ;
    r_bit[1] = (s_out_bit[2]<<4) + s_out_bit[3] ;
    r_bit[2] = (s_out_bit[4]<<4) + s_out_bit[5] ;
    r_bit[3] = (s_out_bit[6]<<4) + s_out_bit[7] ;
    /*now r_bit[0-7] = ???????? ???????? ???????? ???????? 0000..*/

    bit2byte(r_bit,r_byte);
    p_permute(r_byte);
}
/******************************函数f(end)*************************************/






/**************************生成子密钥(beging)*********************************/
void keychange(unsigned char oldkey[8] , unsigned char newkey[16][8])
{
    int i=0,j=0,k=0 ;
    int pc_1[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} ;
    int pc_2[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,45,33,48,
            44,49,39,56,34,53,
            46,42,50,36,29,32} ;
    int ccmovebit[16] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1} ;
    unsigned char oldkey_byte[64] ;   /*64位原始密钥,不过需要从oldkey[8]中提取出64位信息*/
    unsigned char oldkey_byte1[64] ;  /*实际只有56个字节,存放经过pc_1置换后的56位密钥*/
    unsigned char oldkey_byte2[64] ;  /*存放c(i)和d(i)合并起来后的56位,然后用于PC_2置换得到48位的k(i)*/
    unsigned char oldkey_c[28] ;      /*28位密钥c0*/
    unsigned char oldkey_d[28] ;      /*28位密钥d0*/
    unsigned char cc_temp ;
    unsigned char newkey_byte[16][64] ;   /*16个子密钥k1、k2、...、k16,每个子密钥实际存放48位*/

    bit2byte(oldkey,oldkey_byte) ;/*change to byte把64位(8字节)密钥转换为64字节,每字节代表密钥的一位*/
    for(i=0;i<=55;i++) oldkey_byte1[i] = oldkey_byte[pc_1[i] - 1] ;/*goto PC-1*/
    for(i=0;i<=27;i++) oldkey_c[i] = oldkey_byte1[i] ;/*move 28bit -> c0*/
    for(i=28;i<=55;i++) oldkey_d[i -28] = oldkey_byte1[i] ;/*move other 28bit -> d0*/

    /*cc_movebit : get c1-16,d1-16*/
    for(i=0;i<=15;i++)
    {
        for(j=1;j<=ccmovebit[i];j++)      /*循环左移位,位数由ccmovebit[i]事先确定*/
        {
            cc_temp = oldkey_c[0] ; /*move out the first bit*/
            for(k=0;k<=26;k++)
            {
                oldkey_c[k] = oldkey_c[k+1] ;
            }
            oldkey_c[27] = cc_temp ; /*move the first bit to the last bit*/

            cc_temp = oldkey_d[0] ; /*move out the first bit*/
            for(k=0;k<=26;k++)
            {
                oldkey_d[k] = oldkey_d[k+1] ;
            }
            oldkey_d[27] = cc_temp ; /*move the first bit to the last bit*/
        }  /*cc_movebit*/

        /*goto pc-2 change bit*/
        for(k=0;k<=27;k++) oldkey_byte2[k] = oldkey_c[k] ;
        for(k=28;k<=55;k++) oldkey_byte2[k] = oldkey_d[k-28] ;
            /*add c(i)+d(i) -> for pc-2 change 56bit to 48bit k(i)*/
        for(k=0;k<=47;k++) newkey_byte[i][k] = oldkey_byte2[pc_2[k] - 1] ;
    }/*end of one of change the 48bit key*/
    /*byte to bit for 48bit newkey*/
    for(i=0;i<=15;i++) byte2bit(newkey_byte[i],newkey[i]) ;
}
/****************************生成子密钥(end)**********************************/





/************************加密算法encryption(begin)****************************/
void endes(unsigned char m_bit[8], unsigned char k_bit[8], unsigned char e_bit[8])
{
    unsigned char m_bit1[8] = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0} ; /*明文经过IP置换后的存放地--8字节64位明文*/
    unsigned char m_byte[64] ;  /*存放原始明文--64字节64位明文*/
    unsigned char key_n[16][8] ;   /*存放子密钥*/
    unsigned char l_bit[17][8] ;   /*存放加密过程的每一层结果的左32位,实际每层只用4个字节表示32位结果*/
    unsigned char r_bit[17][8] ;   /*存放加密过程的每一层结果的右32位,实际每层只用4个字节表示32位结果*/
    unsigned char e_byte[64] ;     /*存放密文,64个字节表示64位密文*/
    unsigned char r_byte[64] ;     /*存放R(i),实际用32个字节存放32位有用信息*/
    int i = 0 , j = 0;

    keychange(k_bit,key_n) ;/*get the 48bit key x 16 (16rows x 6byte in key_n)*/
    bit2byte(m_bit,m_byte) ;/*change to byte*/
    ip_permute(m_byte);
    byte2bit(m_byte,m_bit1) ;/*re-change to bit*/

    for(i=0;i<=3;i++) l_bit[0][i] = m_bit1[i] ;/*move left 32bit -> l0*/
    for(i=4;i<=7;i++) r_bit[0][i - 4] = m_bit1[i] ;/*move right 32bit -> r0*/

    for(i=1;i<=16;i++) /*16 layer*/
    {
        for(j=0;j<=3;j++) l_bit[i][j] = r_bit[i-1][j] ;/*L(n) = R(n-1)*/

        /*comput f(R(n-1),k)*/
        bit2byte(r_bit[i-1],r_byte) ;
        f(r_byte,key_n[i-1]);
        byte2bit(r_byte,r_bit[i-1]);

        for(j=0;j<=3;j++)/*get next r_bit*/
        {
         r_bit[i][j] = l_bit[i-1][j] ^ r_bit[i-1][j] ;
        }
    }/*end of endes*/

    for(i=0;i<=3;i++) e_bit[i] = r_bit[16][i] ;
    for(i=4;i<=7;i++) e_bit[i] = l_bit[16][i - 4] ;  /*r_bit + l_bit -> e_bit(64bit)*/

    bit2byte(e_bit,e_byte) ;/*change to byte for swap bit IP-1*/
    ip_1_permute(e_byte);
    byte2bit(e_byte,e_bit) ;/*got e_bit*/
}
/*************************加密算法encryption(end)*****************************/






/************************解密算法uncryption(begin)****************************/
void undes(unsigned char m_bit[8], unsigned char k_bit[8], unsigned char e_bit[8])
{    /*~~~注意,这里m_bit[8]实际上是密文,而e_bit[8]则是需要的明文!~~~~*/
    unsigned char m_bit1[8] = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0} ; /*明文经过IP置换后的存放地--8字节64位明文*/
    unsigned char m_byte[64] ;  /*存放原始明文--64字节64位明文*/
    unsigned char key_n[16][8] ;   /*存放子密钥*/
    unsigned char l_bit[17][8] ;   /*存放加密过程的每一层结果的左32位,实际每层只用4个字节表示32位结果*/
    unsigned char r_bit[17][8] ;   /*存放加密过程的每一层结果的右32位,实际每层只用4个字节表示32位结果*/
    unsigned char e_byte[64] ;     /*存放密文,64个字节表示64位密文*/
    unsigned char r_byte[64] ;     /*存放R(i),实际用32个字节存放32位有用信息*/
    int i = 0 , j = 0;

    keychange(k_bit,key_n) ;/*get the 48bit key x 16 (16rows x 6byte in key_n)*/
    bit2byte(m_bit,m_byte) ;/*change to byte*/
    ip_permute(m_byte);
    byte2bit(m_byte,m_bit1) ;/*re-change to bit*/

    for(i=0;i<=3;i++) l_bit[0][i] = m_bit1[i] ;/*move left 32bit -> l0*/
    for(i=4;i<=7;i++) r_bit[0][i - 4] = m_bit1[i] ;/*move right 32bit -> r0*/

    for(i=1;i<=16;i++) /*16 layer*/
    {
        for(j=0;j<=3;j++) l_bit[i][j] = r_bit[i-1][j] ;/*L(n) = R(n-1)*/

        /*comput f(R(n-1),k)*/
        bit2byte(r_bit[i-1],r_byte) ;
        f(r_byte,key_n[16-i]);
        byte2bit(r_byte,r_bit[i-1]);

        for(j=0;j<=3;j++)/*get next r_bit*/
        {
         r_bit[i][j] = l_bit[i-1][j] ^ r_bit[i-1][j] ;
        }
    }/*end of endes*/

    for(i=0;i<=3;i++) e_bit[i] = r_bit[16][i] ;
    for(i=4;i<=7;i++) e_bit[i] = l_bit[16][i - 4] ;  /*r_bit + l_bit -> e_bit(64bit)*/

    bit2byte(e_bit,e_byte) ;/*change to byte for swap bit IP-1*/
    ip_1_permute(e_byte);
    byte2bit(e_byte,e_bit) ;/*got e_bit*/
}
/*************************解密算法uncryption(end)*****************************/







void main()
{
    unsigned char message[8]={"tsinghua"} ;   /*存放明文*/
    unsigned char keyword[8]={"computer"} ;   /*存放密钥*/
    unsigned char result[8];                  /*存放解密后的明文*/
    unsigned char encryption[8];              /*存放密文*/
    unsigned char encryption_byte[64];        /*用64个字节来存放密文*/
    int i;

    printf("the original message is: ");
    for(i=0;i<8;i++) printf("%c",message[i]);
    printf("\n");

    printf("the keyword is:          ");
    for(i=0;i<8;i++) printf("%c",keyword[i]);
    printf("\n\n");


    endes(message,keyword,encryption);


    printf("the encryption message is(char): ");
    for(i=0;i<8;i++) printf("%c",encryption[i]);
    printf("\n");

    printf("the encryption message is(hex):  ");
    for(i=0; i<8; i++) printf("%x ",encryption[i]);
    printf("\n");

    bit2byte(encryption,encryption_byte);
    printf("the encryption message is(bin):\n");
    for(i=0; i<64; i++)
        if(i%8==7) printf("%1d ",encryption_byte[i]);
        else printf("%1d",encryption_byte[i]);
    printf("\n");

    printf("\n\n\n************* after uncryption *****************\n");


    undes(encryption,keyword,result);

    printf("the uncryption message is: ");
    for(i=0;i<8;i++)
        printf("%c",result[i]);
    printf("\n");

    getch();
}

⌨️ 快捷键说明

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