久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 374|回復: 0
打印 上一主題 下一主題
收起左側

基于STM32單片機的AES加密程序 aes.h

[復制鏈接]
跳轉到指定樓層
樓主
ID:1145470 發表于 2025-3-14 20:07 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
AES加密,實踐可用,配上解密包可解密。
aes.h文件
  1. #ifndef  _AES_H__
  2. #define  _AES_H__

  3. // 以bit為單位的密鑰長度,只能為 128,192 和 256 三種
  4. #define AES_KEY_LENGTH        128

  5. // 加解密模式
  6. #define AES_MODE_ECB        0                                // 電子密碼本模式(一般模式)
  7. #define AES_MODE_CBC        1                                // 密碼分組鏈接模式
  8. #define AES_MODE                AES_MODE_CBC


  9. ///
  10. //        函數名:        aes_init
  11. //        描述:                初始化,在此執行擴展密鑰操作。
  12. //        輸入參數:        pKey -- 原始密鑰,其長度必須為 AES_KEY_LENGTH/8 字節。
  13. //        輸出參數:        無。
  14. //        返回值:        無。
  15. ///
  16. void aes_init(const void *pKey);
  17. int base64_encode( const unsigned char * bindata, char * base64, int binlength );

  18. //
  19. //        函數名:        aes_encrypt
  20. //        描述:                加密數據
  21. //        輸入參數:        pPlainText        -- 明文,即需加密的數據,其長度為nDataLen字節。
  22. //                                nDataLen        -- 數據長度,以字節為單位,必須為AES_KEY_LENGTH/8的整倍數。
  23. //                                pIV                        -- 初始化向量,如果使用ECB模式,可設為NULL。
  24. //        輸出參數:        pCipherText        -- 密文,即由明文加密后的數據,可以與pPlainText相同。
  25. //        返回值:        無。
  26. //
  27. void aes_encrypt(const unsigned char *pPlainText, unsigned char *pCipherText,
  28.                                  unsigned int nDataLen, const unsigned char *pIV);

  29. //
  30. //        函數名:        aes_decrypt
  31. //        描述:                解密數據
  32. //        輸入參數:        pCipherText -- 密文,即需解密的數據,其長度為nDataLen字節。
  33. //                                nDataLen        -- 數據長度,以字節為單位,必須為AES_KEY_LENGTH/8的整倍數。
  34. //                                pIV                        -- 初始化向量,如果使用ECB模式,可設為NULL。
  35. //        輸出參數:        pPlainText  -- 明文,即由密文解密后的數據,可以與pCipherText相同。
  36. //        返回值:        無。
  37. //
  38. void aes_decrypt( const unsigned char *pCipherText,unsigned char *pPlainText,
  39.                                  unsigned int nDataLen, const unsigned char *pIV);
  40. //char *encrypt(char *plain_data,char *test_key,char *test_iv);
  41. char *encrypt(const char *plain_data, const char *test_key, const char *test_iv) ;
  42. #endif  
復制代碼


aes.c文件
  1. #include "AES.h"
  2. #include "stdio.h"
  3. #include "stdlib.h"
  4. #include "string.h"
  5. #include "stm32f10x.h"

  6. #ifndef __C51__
  7.         #define code
  8.         #define data
  9.         #define idata
  10.         #define xdata
  11.         #define pdata
  12.         typedef unsigned char BOOL;
  13. #else
  14.         typedef bit BOOL;
  15. #endif

  16. #define Nk        (AES_KEY_LENGTH / 32)                // 以“字”(4字節)為單位的密鑰長度
  17. #define Nb        4                                                        // 以“字”(4字節)為單位的加解密數據塊大小,固定為4

  18. // Nr:加密的輪數
  19. #if   AES_KEY_LENGTH == 128
  20.         #define Nr        10
  21. #elif AES_KEY_LENGTH == 192
  22.         #define Nr        12
  23. #elif AES_KEY_LENGTH == 256
  24.         #define Nr        14
  25. #else
  26.         #error AES_KEY_LENGTH must be 128, 192 or 256 BOOLs!
  27. #endif

  28. // GF(28) 多項式
  29. #define BPOLY 0x1B //

  30. /*****************************************************************************
  31. *  Typedef  Enum                        枚舉定義
  32. *****************************************************************************/

  33. /*****************************************************************************
  34. *  Struct                               數據結構定義
  35. ******************************************************************************/


  36. /*****************************************************************************
  37. *  Local variable                       局部變量
  38. *****************************************************************************/
  39. const char * const base64char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  40.   
  41. const char padding_char = '=';
  42. int base64_decode( const char * base64, unsigned char * bindata )
  43. {
  44.     int i, j;
  45.     unsigned char k;
  46.     unsigned char temp[4];

  47.     for ( i = 0, j = 0; base64[i] != '\0' ; i += 4 )
  48.     {
  49.         memset( temp, 0xFF, sizeof(temp) );
  50.         for ( k = 0 ; k < 64 ; k ++ )
  51.         {
  52.             if ( base64char[k] == base64[i] )
  53.                 temp[0] = k;
  54.         }
  55.         for ( k = 0 ; k < 64 ; k ++ )
  56.         {
  57.             if ( base64char[k] == base64[i + 1] )
  58.                 temp[1] = k;
  59.         }
  60.         for ( k = 0 ; k < 64 ; k ++ )
  61.         {
  62.             if ( base64char[k] == base64[i + 2] )
  63.                 temp[2] = k;
  64.         }
  65.         for ( k = 0 ; k < 64 ; k ++ )
  66.         {
  67.             if ( base64char[k] == base64[i + 3] )
  68.                 temp[3] = k;
  69.         }

  70.         bindata[j++] = ((unsigned char)(((unsigned char)(temp[0] << 2)) & 0xFC)) |
  71.                        ((unsigned char)((unsigned char)(temp[1] >> 4) & 0x03));
  72.         if ( base64[i + 2] == '=' )
  73.             break;

  74.         bindata[j++] = ((unsigned char)(((unsigned char)(temp[1] << 4)) & 0xF0)) |
  75.                        ((unsigned char)((unsigned char)(temp[2] >> 2) & 0x0F));
  76.         if ( base64[i + 3] == '=' )
  77.             break;

  78.         bindata[j++] = ((unsigned char)(((unsigned char)(temp[2] << 6)) & 0xF0)) |
  79.                        ((unsigned char)(temp[3] & 0x3F));
  80.     }
  81.     return j;
  82. }

  83. int base64_encode(const unsigned char *sourcedata, char *base64, int datalength) {
  84.     int i = 0, j = 0;
  85.     unsigned char temp[3] = {0};

  86.     while (datalength > 2) {
  87.         temp[0] = sourcedata[j++];
  88.         temp[1] = sourcedata[j++];
  89.         temp[2] = sourcedata[j++];

  90.         base64[i++] = base64char[temp[0] >> 2];
  91.         base64[i++] = base64char[(temp[0] & 0x03) << 4 | temp[1] >> 4];
  92.         base64[i++] = base64char[(temp[1] & 0x0F) << 2 | temp[2] >> 6];
  93.         base64[i++] = base64char[temp[2] & 0x3F];
  94.         datalength -= 3;
  95.     }

  96.     if (datalength > 0) {
  97.         temp[0] = sourcedata[j++];
  98.         temp[1] = (datalength > 1) ? sourcedata[j++] : 0;
  99.         temp[2] = 0;

  100.         base64[i++] = base64char[temp[0] >> 2];
  101.         base64[i++] = base64char[(temp[0] & 0x03) << 4 | temp[1] >> 4];
  102.         if (datalength == 1) {
  103.             base64[i++] = padding_char;
  104.         } else {
  105.             base64[i++] = base64char[(temp[1] & 0x0F) << 2];
  106.         }
  107.         base64[i++] = padding_char;
  108.     }

  109.     base64[i] = '\0';
  110.     return i;
  111. }
  112. // AES子密鑰表,當密鑰長度為128位時,占用176字節空間
  113. static xdata unsigned char g_roundKeyTable[4*Nb*(Nr+1)];

  114. // 加密用的SBox
  115. static code const unsigned char sBox[256] =
  116. {
  117.         0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
  118.         0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
  119.         0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
  120.         0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
  121.         0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
  122.         0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
  123.         0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
  124.         0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
  125.         0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
  126.         0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
  127.         0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
  128.         0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
  129.         0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
  130.         0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
  131.         0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
  132.         0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
  133. };

  134. // 解密用的SBox
  135. static code const unsigned char invSBox[256] =
  136. {
  137.         0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
  138.         0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
  139.         0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
  140.         0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
  141.         0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92,
  142.         0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
  143.         0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,
  144.         0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,
  145.         0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
  146.         0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
  147.         0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
  148.         0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
  149.         0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
  150.         0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
  151.         0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
  152.         0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d        
  153. };


  154. //        函數名:        rotation_word
  155. //        描述:                對一個“字”數據進行循環右移。
  156. //        輸入參數:        pWord -- 要右移的4字節數據。
  157. //        輸出參數:        pWord -- 右移后的4字節數據。
  158. //        返回值:        無。
  159. ///
  160. static void rotation_word(unsigned char *pWord)
  161. {
  162.         unsigned char temp = pWord[0];
  163.         pWord[0]  = pWord[1];
  164.         pWord[1]  = pWord[2];
  165.         pWord[2]  = pWord[3];
  166.         pWord[3]  = temp;
  167. }

  168. //        函數名:        xor_bytes
  169. //        描述:                批量異或兩組數據。
  170. //        輸入參數:        pData1 -- 要異或的第一組數據。
  171. //                                pData1 -- 要異或的第二組數據。
  172. //                                nCount -- 要異或的數據長度。
  173. //        輸出參數:        pData1 -- 異或后的結果。
  174. //        返回值:        無。
  175. ///
  176. static void xor_bytes(unsigned char *pData1, const unsigned char *pData2, unsigned char nCount)
  177. {
  178.         unsigned char i;
  179.         
  180.         for (i = 0; i < nCount; i++)
  181.         {
  182.                 pData1[i] ^= pData2[i];
  183.         }
  184. }


  185. #define AddRoundKey(pState, pRoundKey) \
  186.         xor_bytes((pState), (pRoundKey), 4*Nb)


  187. //        函數名:        sub_bytes
  188. //        描述:                通過S盒子置換狀態數據。
  189. //        輸入參數:        pState        -- 狀態數據。
  190. //                                nCount  -- 狀態數據長度。
  191. //                                bInvert        -- 是否使用反向S盒子(解密時使用)。
  192. //        輸出參數:        pState        -- 置換后的狀態數據。
  193. //        返回值:        無。
  194. ///
  195. static void sub_bytes(unsigned char *pState, unsigned char nCount, BOOL bInvert)
  196. {
  197.         unsigned char i;
  198.         const unsigned char code *pSBox = bInvert ? invSBox : sBox;
  199.         
  200.         for (i = 0; i < nCount; i++)
  201.         {
  202.                 pState[i] = pSBox[pState[i]];
  203.         }
  204. }

  205. //        函數名:        shift_rows
  206. //        描述:                把狀態數據移行。
  207. //        輸入參數:        pState        -- 狀態數據。
  208. //                                bInvert        -- 是否反向移行(解密時使用)。
  209. //        輸出參數:        pState        -- 移行后的狀態數據。
  210. //        返回值:        無。
  211. ///
  212. static void shift_rows(unsigned char *pState, BOOL bInvert)
  213. {
  214.         // 注意:狀態數據以列形式存放!

  215.         unsigned char r;        // row,   行
  216.         unsigned char c;        // column,列
  217.         unsigned char temp;
  218.         unsigned char rowData[4];
  219.         
  220.         for (r = 1; r < 4; r++)
  221.         {
  222.                 // 備份一行數據
  223.                 for (c = 0; c < 4; c++)
  224.                 {
  225.                         rowData[c] = pState[r + 4*c];
  226.                 }
  227.                
  228.                 temp = bInvert ? (4 - r) : r;
  229.                 for (c = 0; c < 4; c++)
  230.                 {
  231.                         pState[r + 4*c] = rowData[(c + temp) % 4];
  232.                 }
  233.         }
  234. }

  235. //        函數名:        gf_mult_by02
  236. //        描述:                在GF(28)域的 乘2 運算。
  237. //        輸入參數:        num        -- 乘數。
  238. //        輸出參數:        無。
  239. //        返回值:        num乘以2的結果。
  240. ///
  241. static unsigned char gf_mult_by02(unsigned char num)
  242. {
  243.         if ((num & 0x80) == 0)
  244.         {
  245.                 num = num << 1;
  246.         }
  247.         else
  248.         {
  249.                 num = (num << 1) ^ BPOLY;
  250.         }
  251.         
  252.         return num;
  253. }

  254. //        函數名:        mix_columns
  255. //        描述:                混合狀態各列數據。
  256. //        輸入參數:        pState        -- 狀態數據。
  257. //                                bInvert        -- 是否反向混合(解密時使用)。
  258. //        輸出參數:        pState        -- 混合列后的狀態數據。
  259. //        返回值:        無。
  260. ///
  261. static void mix_columns(unsigned char *pState, BOOL bInvert)
  262. {
  263.         unsigned char i;
  264.         unsigned char temp;
  265.         unsigned char a0Pa2_M4;        // 4(a0 + a2)
  266.         unsigned char a1Pa3_M4;        // 4(a1 + a3)
  267.         unsigned char result[4];

  268.         for (i = 0; i < 4; i++, pState += 4)
  269.         {
  270.                 temp = pState[0] ^ pState[1] ^ pState[2] ^ pState[3];
  271.                 result[0] = temp ^ pState[0] ^ gf_mult_by02((unsigned char) (pState[0] ^ pState[1]));
  272.                 result[1] = temp ^ pState[1] ^ gf_mult_by02((unsigned char) (pState[1] ^ pState[2]));
  273.                 result[2] = temp ^ pState[2] ^ gf_mult_by02((unsigned char) (pState[2] ^ pState[3]));
  274.                 result[3] = temp ^ pState[3] ^ gf_mult_by02((unsigned char) (pState[3] ^ pState[0]));

  275.                 if (bInvert)
  276.                 {
  277.                         a0Pa2_M4 = gf_mult_by02(gf_mult_by02((unsigned char) (pState[0] ^ pState[2])));
  278.                         a1Pa3_M4 = gf_mult_by02(gf_mult_by02((unsigned char) (pState[1] ^ pState[3])));
  279.                         temp         = gf_mult_by02((unsigned char) (a0Pa2_M4 ^ a1Pa3_M4));
  280.                         result[0] ^= temp ^ a0Pa2_M4;
  281.                         result[1] ^= temp ^ a1Pa3_M4;
  282.                         result[2] ^= temp ^ a0Pa2_M4;
  283.                         result[3] ^= temp ^ a1Pa3_M4;
  284.                 }

  285.                 memcpy(pState, result, 4);
  286.         }
  287. }

  288. ///
  289. //        函數名:        block_encrypt
  290. //        描述:                對單塊數據加密。
  291. //        輸入參數:        pState -- 狀態數據。
  292. //        輸出參數:        pState -- 加密后的狀態數據。
  293. //        返回值:        無。
  294. ///
  295. static void block_encrypt(unsigned char *pState)
  296. {
  297.         unsigned char i;
  298.         
  299.         AddRoundKey(pState, g_roundKeyTable);
  300.         
  301.         for (i = 1; i <= Nr; i++)        // i = [1, Nr]
  302.         {
  303.                 sub_bytes(pState, 4*Nb, 0);
  304.                 shift_rows(pState, 0);

  305.                 if (i != Nr)
  306.                 {
  307.                         mix_columns(pState, 0);
  308.                 }

  309.                 AddRoundKey(pState, &g_roundKeyTable[4*Nb*i]);
  310.         }
  311.         
  312. // 為了節省代碼,合并到循化執行
  313. //         sub_bytes(pState, 4*Nb);
  314. //        shift_rows(pState, 0);
  315. //         AddRoundKey(pState, &g_roundKeyTable[4*Nb*Nr]);
  316. }

  317. ///
  318. //        函數名:        block_decrypt
  319. //        描述:                對單塊數據解密。
  320. //        輸入參數:        pState -- 狀態數據。
  321. //        輸出參數:        pState -- 解密后的狀態數據。
  322. //        返回值:        無。
  323. ///
  324. static void block_decrypt(unsigned char *pState)
  325. {
  326.         unsigned char i;
  327.         
  328.         AddRoundKey(pState, &g_roundKeyTable[4*Nb*Nr]);
  329.         
  330.         for (i = Nr; i > 0; i--)        // i = [Nr, 1]
  331.         {
  332.                 shift_rows(pState, 1);
  333.                 sub_bytes(pState, 4*Nb, 1);
  334.                 AddRoundKey(pState, &g_roundKeyTable[4*Nb*(i-1)]);

  335.                 if (i != 1)
  336.                 {
  337.                         mix_columns(pState, 1);
  338.                 }
  339.         }
  340.         
  341. // 為了節省代碼,合并到循化執行
  342. //  shift_rows(pState, 1);
  343. //  sub_bytes(pState, 4*Nb, 1);
  344. //  AddRoundKey(pState, g_roundKeyTable);
  345. }


  346. ///
  347. //        函數名:        aes_init
  348. //        描述:                初始化,在此執行擴展密鑰操作。
  349. //        輸入參數:        pKey -- 原始密鑰,其長度必須為 AES_KEY_LENGTH/8 字節。
  350. //        輸出參數:        無。
  351. //        返回值:        無。
  352. ///
  353. void aes_init(const void *pKey)
  354. {
  355.         // 擴展密鑰
  356.         unsigned char i;
  357.         unsigned char *pRoundKey;
  358.         unsigned char Rcon[4] = {0x01, 0x00, 0x00, 0x00};

  359.         memcpy(g_roundKeyTable, pKey, 4*Nk);

  360.         pRoundKey = &g_roundKeyTable[4*Nk];

  361.         for (i = Nk; i < Nb*(Nr+1); pRoundKey += 4, i++)
  362.         {
  363.                 memcpy(pRoundKey, pRoundKey - 4, 4);

  364.                 if (i % Nk == 0)
  365.                 {
  366.                         rotation_word(pRoundKey);
  367.                         sub_bytes(pRoundKey, 4, 0);
  368.                         xor_bytes(pRoundKey, Rcon, 4);

  369.                         Rcon[0] = gf_mult_by02(Rcon[0]);
  370.                 }
  371.                 else if (Nk > 6 && i % Nk == Nb)
  372.                 {
  373.                         sub_bytes(pRoundKey, 4, 0);
  374.                 }

  375.                 xor_bytes(pRoundKey, pRoundKey - 4*Nk, 4);
  376.         }
  377. }

  378. //
  379. //        函數名:        aes_encrypt
  380. //        描述:                加密數據
  381. //        輸入參數:        pPlainText        -- 明文,即需加密的數據,其長度為nDataLen字節。
  382. //                                nDataLen        -- 數據長度,以字節為單位,必須為AES_KEY_LENGTH/8的整倍數。
  383. //                                pIV                        -- 初始化向量,如果使用ECB模式,可設為NULL。
  384. //        輸出參數:        pCipherText        -- 密文,即由明文加密后的數據,可以與pPlainText相同。
  385. //        返回值:        無。
  386. //
  387. void aes_encrypt(const unsigned char *pPlainText, unsigned char *pCipherText,
  388.                                  unsigned int nDataLen, const unsigned char *pIV)
  389. {
  390.         unsigned int i;

  391.         if (pPlainText != pCipherText)
  392.         {
  393.                 memcpy(pCipherText, pPlainText, nDataLen);
  394.         }

  395.         for (i = nDataLen/(4*Nb); i > 0 ; i--, pCipherText += 4*Nb)
  396.         {
  397.                 #if AES_MODE == AES_MODE_CBC
  398.                         xor_bytes(pCipherText, pIV, 4*Nb);
  399.                 #endif

  400.                 block_encrypt(pCipherText);
  401.                 pIV = pCipherText;
  402.         }
  403. }

  404. //        函數名:        aes_decrypt
  405. //        描述:                解密數據
  406. //        輸入參數:        pCipherText -- 密文,即需解密的數據,其長度為nDataLen字節。
  407. //                                nDataLen        -- 數據長度,以字節為單位,必須為AES_KEY_LENGTH/8的整倍數。
  408. //                                pIV                        -- 初始化向量,如果使用ECB模式,可設為NULL。
  409. //        輸出參數:        pPlainText  -- 明文,即由密文解密后的數據,可以與pCipherText相同。
  410. //        返回值:        無。
  411. //
  412. void aes_decrypt( const unsigned char *pCipherText,unsigned char *pPlainText,
  413.                                  unsigned int nDataLen, const unsigned char *pIV)
  414. {
  415.         unsigned int i;

  416.         if (pPlainText != pCipherText)
  417.         {
  418.                 memcpy(pPlainText, pCipherText, nDataLen);
  419.         }

  420.         // 從最后一塊數據開始解密,這樣不用開辟空間來保存IV
  421.         pPlainText += nDataLen - 4*Nb;
  422.         for (i = nDataLen/(4*Nb); i > 0 ; i--, pPlainText -= 4*Nb)
  423.         {
  424.                 block_decrypt(pPlainText);

  425.                 #if AES_MODE == AES_MODE_CBC
  426.                         if (i == 1)
  427.                         {// 最后一塊數據
  428.                                 xor_bytes(pPlainText, pIV, 4*Nb);
  429.                         }
  430.                         else
  431.                         {
  432.                                 xor_bytes(pPlainText, pPlainText - 4*Nb, 4*Nb);
  433.                         }
  434.                 #endif
  435.         }
  436. }

  437. char *encrypt(const char *plain_data, const char *test_key, const char *test_iv) {
  438.           int base64_len;
  439.     uint16_t len = strlen(plain_data);
  440.     uint16_t blocks = (len + 15) / 16; // 確保能夠容納所有數據
  441.                 unsigned char passwold[blocks * 16];
  442.     uint8_t padding = blocks * 16 - len;
  443.     uint8_t *str = (uint8_t *)malloc(blocks * 16); // 動態分配內存
  444.     uint8_t key[16], iv[16];
  445.     char *encoded_data = (char *)malloc((blocks * 16) * 2 / 3 + 4); // 動態分配Base64編碼后的內存,額外加4用于'\0'和可能的填充

  446.     if (!str || !encoded_data) {
  447.         // 內存分配失敗處理
  448.         free(str);
  449.         free(encoded_data);
  450.         return NULL;
  451.     }

  452.     memcpy(str, plain_data, len);
  453.     memset(str + len, padding, padding); // 填充數據

  454.     memcpy(key, test_key, 16);
  455.     memcpy(iv, test_iv, 16);

  456.     aes_init(key);
  457.     aes_encrypt(str, passwold, blocks * 16, iv);

  458.     // 計算實際Base64編碼的長度
  459.     base64_len = base64_encode(passwold, encoded_data, blocks * 16);
  460.     encoded_data[base64_len] = '\0'; // 確保字符串以'\0'結尾
  461.     free(str);
  462.     free(encoded_data);
  463.     return encoded_data;
  464.                
  465. }

復制代碼

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏4 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 日本小视频网站 | 97久久精品| 国产综合久久 | 国产98色在线 | 日韩 | 在线中文字幕av | 精品视频久久久久久 | 成人av一区二区在线观看 | 久久久久久黄 | 成人在线亚洲 | 黄色网址大全在线观看 | 亚洲精品久久久9婷婷中文字幕 | 免费一区二区三区 | 久久国产精品视频 | 国产欧美精品区一区二区三区 | 亚洲一区视频 | 亚洲在线视频 | 毛片在线视频 | 国产精品美女久久久久久免费 | 国产精品久久久久久久久 | 国产一级在线观看 | 欧美日韩精品在线一区 | 日本三级做a全过程在线观看 | 91久久精品一区 | 在线精品一区二区 | 成人小视频在线观看 | 日日摸夜夜爽人人添av | 一区二区三区四区免费在线观看 | 福利社午夜影院 | 国产不卡在线观看 | 水蜜桃久久夜色精品一区 | 色眯眯视频在线观看 | 久久久久精 | 久久毛片| 精品1区 | 日韩在线观看中文字幕 | 欧美8一10sex性hd| 国产成人99久久亚洲综合精品 | aaa级片 | 中文字幕视频在线看 | 国产福利视频网站 | 中文字幕欧美一区二区 |