<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 基于單片機(jī)的DES加密解密算法C源碼

          基于單片機(jī)的DES加密解密算法C源碼

          作者: 時(shí)間:2016-11-29 來(lái)源:網(wǎng)絡(luò) 收藏

          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);
          }

          //源碼完畢


          上一頁(yè) 1 2 下一頁(yè)

          評(píng)論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();