新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 基于單片機的DES加密解密算法C源碼

        基于單片機的DES加密解密算法C源碼

        作者: 時間:2016-11-29 來源:網絡 收藏

        void des(uint8_t *plain_strng, uint8_t *key, uint8_t d, uint8_t *ciph_strng)

        {
        uint8_t a_str[8], b_str[8], x_str[8];
        uint8_t i, j, *pkey, temp;

        for (i = 0; i < 8 ; ++i)
        {
        if (key[i] != main_key[i])
        {
        compute_subkeys(key);
        i = 7;
        }
        }

        transpose(plain_strng, a_str, initial_tr, 64);
        for (i=1; i < 17; ++i)
        {
        for (j=0; j < 8; ++j){b_str[j] = a_str[j];}

        if (!d) /*enchipher*/
        pkey = &sub_keys[i-1][0];
        else /*dechipher*/
        pkey = &sub_keys[16-i][0];

        for (j=0; j < 4; ++j){a_str[j] = b_str[j+4];}

        f(pkey, a_str, x_str);

        for (j=0; j < 4; ++j) {a_str[j+4] = b_str[j] ^ x_str[j];}
        }

        temp = a_str[0]; a_str[0] = a_str[4]; a_str[4] = temp;
        temp = a_str[1]; a_str[1] = a_str[5]; a_str[5] = temp;
        temp = a_str[2]; a_str[2] = a_str[6]; a_str[6] = temp;
        temp = a_str[3]; a_str[3] = a_str[7]; a_str[7] = temp;
        transpose(a_str, ciph_strng, final_tr, 64);

        }

        /************************************************************************/
        /* */
        /* Module title: transpose */
        /* Module type: des subrutine */
        /* */
        /* Author: YXH */
        /* Date: 2012-07-13 */
        /* */
        /* Last changed by: YXH */
        /* Date: 2012-07-13 */
        /* */
        /* Functional Description: Permute n bits in a string, according */
        /* to a table describing the new order. */
        /* 0 < n <= 64 */
        /* */
        /* input parameter 1: pointer to first byte in input string */
        /* 2: pointer to first byte in output string */
        /* 3: pointer to table describing new order */
        /* 4: number of bits to be permuted */
        /************************************************************************/

        void transpose(uint8_t *idata, uint8_t *odata, const uint8_t *tbl, uint8_t n)
        {
        const uint8_t *tab_adr;
        int i, bi_idx;

        tab_adr = &bit_msk[0];
        i = 0;

        do
        {odata[i++] = 0;}
        while (i < 8);

        i = 0;
        do
        {
        bi_idx = *tbl++;
        if (idata[bi_idx>>3] & tab_adr[bi_idx & 7])
        {
        odata[i>>3] |= tab_adr[i & 7];
        }
        }
        while (++i < n);
        }

        /************************************************************************/
        /* */
        /* Module title: rotate_l */
        /* Module type: des subrutine */
        /* */
        /* Author: YXH */
        /* Date: 2012-07-13 */
        /* */
        /* Last changed by: YXH */
        /* Date: 2012-07-13 */
        /* */
        /* Functional Description: rotate 2 concatenated strings of 28 */
        /* bits one position to the left. */
        /* */
        /* input parameter 1: pointer to first byte in key string */
        /* */
        /************************************************************************/

        void rotate_l(uint8_t *key)
        {
        uint8_t str_x[8];
        uint8_t i;

        for (i=0; i < 8; ++i) str_x[i] = key[i];
        for (i=0; i < 7; ++i)
        {
        key[i] = (key[i] << 1);
        if ((i < 6) && ((str_x[i+1] & 128) == 128))
        key[i] |= 1;
        }
        if (str_x[0] & 0x80 )
        key[3] |= 0x10;
        else
        key[3] &= ~0x10;
        if (str_x[3] & 0x08 )
        key[6] |= 0x01;
        else
        key[6] &= ~0x01;
        }

        /************************************************************************/
        /* */
        /* Module title: compute_subkeys */
        /* Module type: des subrutine */
        /* */
        /* Author: YXH */
        /* Date: 2012-07-13 */
        /* */
        /* Last changed by: YXH */
        /* Date: 2012-07-13 */
        /* */
        /* Functional Description: Computes the 16 sub keys for use in the */
        /* DES algorithm */
        /* */
        /* input parameter 1: pointer to first byte in key string */
        /* output : fills the array sub_keys[16][8] with */
        /* sub keys and stores the input key in */
        /* main_key[8] */
        /************************************************************************/
        void compute_subkeys(uint8_t *key)
        {
        uint8_t i, j, ikey[8], okey[8];

        for (i=0; i < 8; ++i)
        {
        main_key[i] = key[i];
        }

        transpose(key, ikey, key_tr1, 56);

        for (i=0; i < 16; ++i)
        {
        for (j=0; j < rots[i]; ++j) {rotate_l(ikey);}
        transpose(ikey, okey, key_tr2, 64);
        for (j=0; j < 8; ++j)
        {sub_keys[i][j] = okey[j];}
        }

        }

        /************************************************************************/
        /* */
        /* Module title: f */
        /* Module type: des subrutine */
        /* */
        /* Author: YXH */
        /* Date: 2012-07-13 */
        /* */
        /* Last changed by: YXH */
        /* Date: 2012-07-13 */
        /* */
        /* Functional Description: The chipher function */
        /* */
        /* input parameter 1: pointer to first byte in key string */
        /* 2: pointer to a 32 bit input string */
        /* 3: pointer to a 32 bit output string */
        /************************************************************************/
        void f(uint8_t *skey, uint8_t *a_str, uint8_t *x_str)
        {
        uint8_t e_str[8], y_str[8], z_str[8];
        uint8_t k;

        transpose(a_str, e_str, etr, 64);
        for (k=0; k < 8; ++k)
        {
        y_str[k] = (e_str[k] ^ skey[k]) & 63;
        z_str[k] = s[k] [y_str[k]];
        }
        transpose(z_str, x_str, ptr, 32);
        }

        //源碼完畢


        上一頁 1 2 下一頁

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 文化| 信阳市| 平潭县| 虞城县| 左权县| 苍溪县| 白水县| 辽源市| 临澧县| 拉孜县| 莎车县| 清徐县| 外汇| 札达县| 百色市| 武胜县| 兖州市| 临沂市| 壶关县| 舟曲县| 桐梓县| 萍乡市| 遵化市| 新兴县| 乐清市| 涪陵区| 和硕县| 扬中市| 梁河县| 法库县| 桂林市| 峨山| 浙江省| 镇平县| 永川市| 潮安县| 稻城县| 桂林市| 阳新县| 偃师市| 武平县|