找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3731|回复: 2
打印 上一主题 下一主题
收起左侧

单片机DES加解密源程序,参数带入密钥就能加密解密了

[复制链接]
跳转到指定楼层
楼主
ID:379633 发表于 2018-7-26 22:08 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
单片机的DES加解密程序,参数带入密钥就能加密解密了,注意密钥,原 始数据和加密产生的数据都是8个字节的。

单片机源程序如下:
  1. #include <stdint.h>

  2. //密钥:        B4 31 5B 86 9D 7D FA A2
  3. //数据:       1F AD 61 A5 F7 19 77 14
  4. //DES加密结果:4C 78 E9 1A F2 DA 9C D3

  5. const uint8_t initial_tr[64] =
  6. {
  7.   57, 49, 41, 33, 25, 17,  9,  1,
  8.   59, 51, 43, 35, 27, 19, 11,  3,
  9.   61, 53, 45, 37, 29, 21, 13,  5,
  10.   63, 55, 47, 39, 31, 23, 15,  7,
  11.   56, 48, 40, 32, 24, 16,  8,  0,
  12.   58, 50, 42, 34, 26, 18, 10,  2,
  13.   60, 52, 44, 36, 28, 20, 12,  4,
  14.   62, 54, 46, 38, 30, 22, 14,  6
  15. };

  16. const uint8_t final_tr[64] =
  17. {
  18.   39,  7, 47, 15, 55, 23, 63, 31,
  19.   38,  6, 46, 14, 54, 22, 62, 30,
  20.   37,  5, 45, 13, 53, 21, 61, 29,
  21.   36,  4, 44, 12, 52, 20, 60, 28,
  22.   35,  3, 43, 11, 51, 19, 59, 27,
  23.   34,  2, 42, 10, 50, 18, 58, 26,
  24.   33,  1, 41,  9, 49, 17, 57, 25,
  25.   32,  0, 40,  8, 48, 16, 56, 24
  26. };

  27. const uint8_t swap[64] =
  28. {
  29.   33, 34, 35, 36, 37, 38, 39, 40,
  30.   41, 42, 43, 44, 45, 46, 47, 48,
  31.   49, 50, 51, 52, 53, 54, 55, 56,
  32.   57, 58, 59, 60, 61, 62, 63, 64,
  33.    1,  2,  3,  4,  5,  6,  7,  8,
  34.    9, 10, 11, 12, 13, 14, 15, 16,
  35.   17, 18, 19, 20, 21, 22, 23, 24,
  36.   25, 26, 27, 28, 29, 30, 31, 32
  37. };

  38. const uint8_t key_tr1[56] =
  39. {
  40.   56, 48, 40, 32, 24, 16,  8,
  41.    0, 57, 49, 41, 33, 25, 17,
  42.    9,  1, 58, 50, 42, 34, 26,
  43.   18, 10,  2, 59, 51, 43, 35,
  44.   62, 54, 46, 38, 30, 22, 14,
  45.    6, 61, 53, 45, 37, 29, 21,
  46.   13,  5, 60, 52, 44, 36, 28,
  47.   20, 12,  4, 27, 19, 11,  3
  48. };

  49. const uint8_t key_tr2[64] =
  50. {
  51.   0,  0, 13,  4, 16, 10, 23,  0,
  52.   0,  0,  2,  9, 27, 14,  5, 20,
  53.   0,  0, 22,  7, 18, 11,  3, 25,
  54.   0,  0, 15,  1,  6, 26, 19, 12,
  55.   0,  0, 40, 54, 51, 30, 36, 46,
  56.   0,  0, 29, 47, 39, 50, 44, 32,
  57.   0,  0, 43, 52, 48, 38, 55, 33,
  58.   0,  0, 45, 31, 41, 49, 35, 28
  59. };

  60. const uint8_t etr[64] =
  61. {
  62.   0,  0, 31,  4,  0,  1,  2,  3,
  63.   0,  0,  3,  8,  4,  5,  6,  7,
  64.   0,  0,  7, 12,  8,  9, 10, 11,
  65.   0,  0, 11, 16, 12, 13, 14, 15,
  66.   0,  0, 15, 20, 16, 17, 18, 19,
  67.   0,  0, 19, 24, 20, 21, 22, 23,
  68.   0,  0, 23, 28, 24, 25, 26, 27,
  69.   0,  0, 27,  0, 28, 29, 30, 31
  70. };

  71. const uint8_t ptr[32] =
  72. {
  73.   31, 14, 39, 44, 60, 23, 55, 36,
  74.    4, 30, 46, 53, 12, 37, 62, 21,
  75.    5, 15, 47, 29, 63, 54,  6, 20,
  76.   38, 28, 61, 13, 45, 22,  7, 52
  77. };

  78. const uint8_t s[8][64] =
  79. {
  80.   {
  81.    14,  4, 13,  1,  2, 15, 11,  8,  3, 10,  6, 12,  5,  9,  0,  7,
  82.     0, 15,  7,  4, 14,  2, 13,  1, 10,  6, 12, 11,  9,  5,  3,  8,
  83.     4,  1, 14,  8, 13,  6,  2, 11, 15, 12,  9,  7,  3, 10,  5,  0,
  84.    15, 12,  8,  2,  4,  9,  1,  7,  5, 11,  3, 14, 10,  0,  6, 13
  85.   },
  86.   {
  87.    15,  1,  8, 14,  6, 11,  3,  4,  9,  7,  2, 13, 12,  0,  5, 10,
  88.     3, 13,  4,  7, 15,  2,  8, 14, 12,  0,  1, 10,  6,  9, 11,  5,
  89.     0, 14,  7, 11, 10,  4, 13,  1,  5,  8, 12,  6,  9,  3,  2, 15,
  90.    13,  8, 10,  1,  3, 15,  4,  2, 11,  6,  7, 12,  0,  5, 14,  9
  91.   },
  92.   {
  93.    10,  0,  9, 14,  6,  3, 15,  5,  1, 13, 12,  7, 11,  4,  2,  8,
  94.    13,  7,  0,  9,  3,  4,  6, 10,  2,  8,  5, 14, 12, 11, 15,  1,
  95.    13,  6,  4,  9,  8, 15,  3,  0, 11,  1,  2, 12,  5, 10, 14,  7,
  96.     1, 10, 13,  0,  6,  9,  8,  7,  4, 15, 14,  3, 11,  5,  2, 12
  97.   },
  98.   {
  99.     7, 13, 14,  3,  0,  6,  9, 10,  1,  2,  8,  5, 11, 12,  4, 15,
  100.    13,  8, 11,  5,  6, 15,  0,  3,  4,  7,  2, 12,  1, 10, 14,  9,
  101.    10,  6,  9,  0, 12, 11,  7, 13, 15,  1,  3, 14,  5,  2,  8,  4,
  102.     3, 15,  0,  6, 10,  1, 13,  8,  9,  4,  5, 11, 12,  7,  2, 14
  103.   },
  104.   {
  105.     2, 12,  4,  1,  7, 10, 11,  6,  8,  5,  3, 15, 13,  0, 14,  9,
  106.    14, 11,  2, 12,  4,  7, 13,  1,  5,  0, 15, 10,  3,  9,  8,  6,
  107.     4,  2,  1, 11, 10, 13,  7,  8, 15,  9, 12,  5,  6,  3,  0, 14,
  108.    11,  8, 12,  7,  1, 14,  2, 13,  6, 15,  0,  9, 10,  4,  5,  3
  109.   },
  110.   {
  111.    12,  1, 10, 15,  9,  2,  6,  8,  0, 13,  3,  4, 14,  7,  5, 11,
  112.    10, 15,  4,  2,  7, 12,  9,  5,  6,  1, 13, 14,  0, 11,  3,  8,
  113.     9, 14, 15,  5,  2,  8, 12,  3,  7,  0,  4, 10,  1, 13, 11,  6,
  114.     4,  3,  2, 12,  9,  5, 15, 10, 11, 14,  1,  7, 6,  0,  8,  13
  115.   },
  116.   {
  117.     4, 11,  2, 14, 15,  0,  8, 13,  3, 12,  9,  7,  5, 10,  6,  1,
  118.    13,  0, 11,  7,  4,  9,  1, 10, 14,  3,  5, 12,  2, 15,  8,  6,
  119.     1,  4, 11, 13, 12,  3,  7, 14, 10, 15,  6,  8,  0,  5,  9,  2,
  120.     6, 11, 13,  8,  1,  4, 10,  7,  9,  5,  0, 15, 14,  2,  3, 12
  121.   },
  122.   {
  123.    13,  2,  8,  4,  6, 15, 11,  1, 10,  9,  3, 14,  5,  0, 12,  7,
  124.     1, 15, 13,  8, 10,  3,  7,  4, 12,  5,  6, 11,  0, 14,  9,  2,
  125.     7, 11,  4,  1,  9, 12, 14,  2,  0,  6, 10, 13, 15,  3,  5,  8,
  126.     2,  1, 14,  7,  4, 10,  8, 13, 15, 12,  9,  0,  3,  5,  6, 11
  127.   }
  128. };

  129. const uint8_t rots[16] =
  130. {
  131.   1,  1,  2,  2,  2,  2,  2,  2,  1,  2,  2,  2,  2,  2,  2,  1
  132. };

  133. const uint8_t bit_msk[8] =
  134. {
  135.   0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01
  136. };

  137. uint8_t DES_Encrypt_key[8];
  138. uint8_t DES_Decrypt_key[8];
  139. uint8_t sub_keys[16][8]; //sub_keys[16][8]
  140. uint8_t main_key[8];

  141.         void    des(uint8_t*, uint8_t*, uint8_t, uint8_t*);
  142.    void  FLASH_Read_KEYS(uint8_t key_index);
  143. static  void    transpose (uint8_t*, uint8_t*, const uint8_t*, uint8_t);
  144. static  void    rotate_l (uint8_t*);
  145. static  void    compute_subkeys (uint8_t*);
  146. static  void    f (uint8_t*, uint8_t*, uint8_t*);

  147. /************************************************************************/
  148. /*                                                                      */
  149. /*      Module title:           des                                     */
  150. /*      Module type:            des mainrutine                          */
  151. /*                                                                      */
  152. /*      Author:                 YXH                                     */
  153. /*      Date:                   2012-07-13                              */
  154. /*                                                                      */
  155. /*      Last changed by:        YXH                                     */
  156. /*      Date:                   2012-07-13                              */
  157. /*                                                                      */
  158. /*      Functional Description: Encipher and decipher 64 bits string    */
  159. /*                              according to a 64 bits key string       */
  160. /*                              The string format is shown below        */
  161. /*                                                                      */
  162. /*      input parameter 1:      pointer to 64 bits input string         */
  163. /*                      2:      pointer to 64 bits key string           */
  164. /*                      3:      boolean if false indicating enciphering */
  165. /*                              if true dechiphering                    */
  166. /*                      4:      pointer to a 64 bits output string      */
  167. /************************************************************************/
  168. /*                                                                      */
  169. /*                      msb                     lsb                     */
  170. /*                      bit                     bit                     */
  171. /*                      +-- -- -- -- -- -- -- --+                       */
  172. /*              addr    !1st                 8th!                       */
  173. /*                      +-- -- -- -- -- -- -- --+                       */
  174. /*              addr+1  !9th                16th!                       */
  175. /*                      +-- -- -- -- -- -- -- --+                       */
  176. /*                      :                       :                       */
  177. /*                      :                       :                       */
  178. /*                      +-- -- -- -- -- -- -- --+                       */
  179. /*              addr+7  !57th               64th!                       */
  180. /*                      +-- -- -- -- -- -- -- --+                       */
  181. /*                                                                      */
  182. /************************************************************************/

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

  184. {
  185.    uint8_t a_str[8], b_str[8], x_str[8];
  186.    uint8_t i, j, *pkey, temp;

  187.     for (i = 0; i < 8 ; ++i)
  188.   {
  189.    if (key[i] != main_key[i])
  190.          {
  191.          compute_subkeys(key);
  192.          i = 7;
  193.          }
  194.   }
  195.   
  196.      transpose(plain_strng, a_str, initial_tr, 64);
  197.      for (i=1; i < 17; ++i)
  198.      {
  199.    for (j=0; j < 8; ++j){b_str[j] = a_str[j];}
  200.    
  201.    if (!d)           /*enchipher*/
  202.     pkey = &sub_keys[i-1][0];
  203.    else                /*dechipher*/
  204.     pkey = &sub_keys[16-i][0];
  205.    
  206.    for (j=0; j < 4; ++j){a_str[j] = b_str[j+4];}
  207.    
  208.    f(pkey, a_str, x_str);
  209.    
  210.    for (j=0; j < 4; ++j) {a_str[j+4] = b_str[j] ^ x_str[j];}
  211.      }
  212.      
  213.      temp = a_str[0]; a_str[0] = a_str[4]; a_str[4] = temp;
  214.      temp = a_str[1]; a_str[1] = a_str[5]; a_str[5] = temp;
  215.      temp = a_str[2]; a_str[2] = a_str[6]; a_str[6] = temp;
  216.      temp = a_str[3]; a_str[3] = a_str[7]; a_str[7] = temp;
  217.      transpose(a_str, ciph_strng, final_tr, 64);

  218. }

  219. /************************************************************************/
  220. /*                                                                      */
  221. /*      Module title:           transpose                               */
  222. /*      Module type:            des subrutine                           */
  223. /*                                                                      */
  224. /*      Author:                 YXH                                     */
  225. /*      Date:                   2012-07-13                              */
  226. /*                                                                      */
  227. /*      Last changed by:        YXH                                     */
  228. /*      Date:                   2012-07-13                              */
  229. /*                                                                      */
  230. /*      Functional Description: Permute n bits in a string, according   */
  231. /*                              to a table describing the new order.    */
  232. /*                              0 < n <= 64                             */
  233. /*                                                                      */
  234. /*      input parameter 1:      pointer to first byte in input string   */
  235. /*                      2:      pointer to first byte in output string  */
  236. /*                      3:      pointer to table describing new order   */
  237. /*                      4:      number of bits to be permuted           */
  238. /************************************************************************/

  239. void transpose(uint8_t *idata, uint8_t *odata, const uint8_t *tbl, uint8_t n)
  240. {
  241.   const uint8_t *tab_adr;
  242.   int i, bi_idx;

  243. tab_adr = &bit_msk[0];
  244.   i = 0;
  245.   
  246.   do
  247.      {odata[i++] = 0;}
  248.   while (i < 8);

  249. i = 0;
  250.   do
  251.   {
  252.          bi_idx = *tbl++;
  253.          if (idata[bi_idx>>3] & tab_adr[bi_idx & 7])
  254.    {
  255.     odata[i>>3] |= tab_adr[i & 7];
  256.    }
  257.   }
  258.   while (++i < n);
  259. }

  260. /************************************************************************/
  261. /*                                                                      */
  262. /*      Module title:           rotate_l                                */
  263. /*      Module type:            des subrutine                           */
  264. /*                                                                      */
  265. /*      Author:                 YXH                                     */
  266. /*      Date:                   2012-07-13                              */
  267. /*                                                                      */
  268. /*      Last changed by:        YXH                                  */
  269. /*      Date:                   2012-07-13                              */
  270. /*                                                                      */
  271. /*      Functional Description: rotate 2 concatenated strings of 28     */
  272. /*                              bits one position to the left.          */
  273. /*                                                                      */
  274. /*      input parameter 1:      pointer to first byte in key string     */
  275. /*                                                                      */
  276. /************************************************************************/

  277. void rotate_l(uint8_t *key)
  278. {
  279.    uint8_t str_x[8];
  280.    uint8_t i;

  281.       for (i=0; i < 8; ++i) str_x[i] = key[i];
  282.        for (i=0; i < 7; ++i)
  283.        {
  284.          key[i] = (key[i] << 1);
  285.          if ((i < 6) && ((str_x[i+1] & 128) == 128))
  286.            key[i] |= 1;
  287.        }
  288.        if (str_x[0] & 0x80 )
  289.          key[3] |= 0x10;
  290.        else
  291.          key[3] &= ~0x10;
  292.        if (str_x[3] & 0x08 )
  293.          key[6] |= 0x01;
  294.        else
  295.          key[6] &= ~0x01;
  296. }

  297. /************************************************************************/
  298. /*                                                                      */
  299. /*      Module title:           compute_subkeys                         */
  300. /*      Module type:            des subrutine                           */
  301. /*                                                                      */
  302. /*      Author:                 YXH                                     */
  303. /*      Date:                   2012-07-13                              */
  304. /*                                                                      */
  305. /*      Last changed by:        YXH                                     */
  306. /*      Date:                   2012-07-13                              */
  307. /*                                                                      */
  308. /*      Functional Description: Computes the 16 sub keys for use in the */
  309. /*                              DES algorithm                           */
  310. /*                                                                      */
  311. /*      input parameter 1:      pointer to first byte in key string     */
  312. /*      output           :      fills the array sub_keys[16][8] with    */
  313. /*                              sub keys and stores the input key in    */
  314. /*                              main_key[8]                             */
  315. /************************************************************************/
  316. void compute_subkeys(uint8_t *key)
  317. {
  318.   uint8_t i, j, ikey[8], okey[8];

  319. for (i=0; i < 8; ++i)
  320.   {
  321.    main_key[i] = key[i];
  322.   }
  323.   
  324.   transpose(key, ikey, key_tr1, 56);
  325.   
  326.   for (i=0; i < 16; ++i)
  327.      {
  328.    for (j=0; j < rots[i]; ++j) {rotate_l(ikey);}
  329.     transpose(ikey, okey, key_tr2,  64);
  330.    for (j=0; j < 8; ++j)
  331.    { sub_keys[i][j] = okey[j];}
  332.      }

  333. }

  334. /************************************************************************/
  335. /*                                                                      */
  336. /*      Module title:           f                                       */
  337. /*      Module type:            des subrutine                           */
  338. /*                                                                      */
  339. /*      Author:                 YXH                                     */
  340. /*      Date:                   2012-07-13                              */
  341. /*                                                                      */
  342. /*      Last changed by:        YXH                                     */
  343. /*      Date:                   2012-07-13                              */
  344. /*                                                                      */
  345. /*      Functional Description: The chipher function                    */
  346. /*                                                                      */
  347. /*      input parameter 1:      pointer to first byte in key string     */
  348. /*                      2:      pointer to a 32 bit input string        */
  349. /*                      3:      pointer to a 32 bit output string       */
  350. /************************************************************************/
  351. void f(uint8_t *skey, uint8_t *a_str, uint8_t *x_str)
  352. {
  353.   uint8_t e_str[8], y_str[8], z_str[8];
  354.   uint8_t k;

  355. transpose(a_str, e_str, etr, 64);
  356.   for (k=0; k < 8; ++k)
  357.   {
  358.          y_str[k] = (e_str[k] ^ skey[k]) & 63;
  359.          z_str[k] = s[k] [y_str[k]];
  360.   }
  361.   transpose(z_str, x_str, ptr, 32);
  362. }
复制代码

所有资料51hei提供下载:
DES.zip (3.48 KB, 下载次数: 47)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:462629 发表于 2019-12-19 22:24 | 只看该作者
有没有可以加32个字符的
回复

使用道具 举报

板凳
ID:584814 发表于 2020-4-22 09:03 | 只看该作者
附件中木有头文件貌似搞不定  
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表