当前位置:   article > 正文

C语言 - AES软件加解密算法_c语言 aes加密

c语言 aes加密

概述

       (AES)RIJNDAEL算法是一个数据块长度盒密钥长度都可变的分组加密算法,其数据块长度和密钥长度都可独立地选定为大于等于128位且小于等于256位的32位任意倍数。深入学习请参考《密码学》书籍,谢谢各位参阅。

验证环境:STM32F401CEU6,已通过。

1、code

aes.h

  1. //
  2. // Created by asus on 2023/7/7.
  3. //
  4. #ifndef TEST_C_PROJECT_AES_H
  5. #define TEST_C_PROJECT_AES_H
  6. #define Nb 4 //加解密数据块大小,固定为4
  7. //加密类型对应的密匙长度,单位bit
  8. typedef enum {
  9. AES128 = 128,
  10. AES192 = 192,
  11. AES256 = 256,
  12. } AESType_t;
  13. //加解密模式
  14. typedef enum {
  15. AES_MODE_ECB = 0, // 电子密码本模式
  16. AES_MODE_CBC = 1, // 密码分组链接模式
  17. } AESMode_t;
  18. typedef struct {
  19. int Nk; //用户不需要填充,密钥长度,单位字节, AES128:Nk=16、AES192:Nk=24、AES256:Nr=32
  20. int Nr; //用户不需要填充,加密的轮数 AES128:Nr=10、AES192:Nr=12、AES256:Nr=14
  21. int type;//用户需填充,关联AESType_t
  22. int mode;//用户需填充,关联AESMode_t
  23. const void* key;//用户需填充,密匙
  24. const void* pIV;//用户需填充,初始化向量, 当mode=AES_MODE_CBC时需要设置,指向unsigned char IV[4*Nb];
  25. //AES拓展密匙, 空间大小 AES128:4*Nb*(10+1):4*Nb*(12+1)、AES256:4*Nb*(14+1)
  26. unsigned char expandKey[4 * Nb * (14 + 1)];//用户不需要填充,[4*Nb*(Nr+1)]、这里按最大的AES256进行初始化
  27. } AESInfo_t;
  28. /*****************************************************************************
  29. * 函数名: AESInit
  30. * 功能描述: 初始化
  31. * 输入参数: aesInfoP -- 用户需要填充
  32. * 输出参数: 无。
  33. * 返回值: 无。
  34. *****************************************************************************/
  35. void AESInit(AESInfo_t* aesInfoP);
  36. /*****************************************************************************
  37. * 函数名: AESEncrypt
  38. * 功能描述: 加密数据
  39. * 输入参数: aesInfoP -- 包含key、加密方式等初始化信息
  40. * pPlainText -- 要加密的数据,其长度为nDataLen字节。
  41. * dataLen -- 数据长度,以字节为单位,需要是整倍数,AES128:16倍数、AES192:24倍数、AES256:32倍数。
  42. * 输出参数: pCipherText -- 密文,即由明文加密后的数据,可以与pPlainText相同。
  43. * 返回值: 解密后的数据长度。。
  44. *****************************************************************************/
  45. unsigned int AESEncrypt(AESInfo_t* aesInfoP, const unsigned char* pPlainText, unsigned char* pCipherText, unsigned int nDataLen);
  46. /*****************************************************************************
  47. * 函数名: AESDecrypt
  48. * 描述: 解密数据
  49. * 输入参数: aesInfoP -- 包含key、加密方式等初始化信息
  50. * pPlainText -- 要加密的数据,其长度为nDataLen字节。
  51. * dataLen -- 数据长度,以字节为单位,需要是整倍数,AES128:16倍数、AES192:24倍数、AES256:32倍数。
  52. * 输出参数: pCipherText -- 加密后的数据,可以与pCipherText相同
  53. * 返回值: 返回解密后的数据长度。
  54. *****************************************************************************/
  55. unsigned int AESDecrypt(AESInfo_t* aesInfoP, unsigned char* pPlainText, const unsigned char* pCipherText, unsigned int nDataLen);
  56. #endif //TEST_C_PROJECT_AES_H

aes.c

  1. //
  2. // Created by asus on 2023/7/7.
  3. //
  4. #include "aes.h"
  5. #include <string.h>
  6. // GF(2^8) 多项式
  7. #define BPOLY 0x1B //x^4 + x^3 + x^1 + x^0= 从右边开始算,bit0、bit1、bit3、bit4、为1,bit2、bit5、bit6、bit7为0,即00011011=0x1B
  8. /*
  9. SubstituteBytes()
  10. 加密时:使用S盒,将待加密数据为S盒索引将加密数据替换为S盒的内容
  11. 解密时:使用逆S盒,将已加密数据为逆S盒索引将已加密数据替换为逆S盒子的内容
  12. 其实就是将数据按表替换,
  13. 例如待加密数据unsigned char data = 9;
  14. 加密数据:encryptData = SBox[data] = SBox[9] = 0x01;//注意索引从0开始
  15. 解密数据:decryptData = InvSBox[encryptData] = InvSBox[0x01] = 9;
  16. SBox和InvSBox的关系是 data = InvSBox[SBox[data]];还跟GF(2^8) 多项式有关
  17. */
  18. // 加密用的S盒
  19. static const unsigned char SBox[256] =
  20. {
  21. 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
  22. 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
  23. 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
  24. 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
  25. 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
  26. 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
  27. 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
  28. 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
  29. 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
  30. 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
  31. 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
  32. 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
  33. 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
  34. 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
  35. 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
  36. 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
  37. };
  38. // 解密用的SBox
  39. static const unsigned char InvSBox[256] =
  40. {
  41. 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
  42. 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
  43. 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
  44. 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
  45. 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92,
  46. 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
  47. 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,
  48. 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,
  49. 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
  50. 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
  51. 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
  52. 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
  53. 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
  54. 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
  55. 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
  56. 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d
  57. };
  58. /*****************************************************************************
  59. * 函数名: RShiftWord
  60. * 功能描述: 对一个pWord 4字节数据进行循环右移。
  61. * 输入参数: pWord -- 要右移的4字节数据。
  62. * 输出参数: pWord -- 右移后的4字节数据。
  63. * 返回值: 无。
  64. *****************************************************************************/
  65. static void RShiftWord(unsigned char* pWord)
  66. {
  67. unsigned char temp = pWord[0];
  68. pWord[0] = pWord[1];
  69. pWord[1] = pWord[2];
  70. pWord[2] = pWord[3];
  71. pWord[3] = temp;
  72. }
  73. /*****************************************************************************
  74. * 函数名: XorBytes
  75. * 功能描述: 异或两组数据。
  76. * 输入参数: pData1 -- 要异或的第一组数据。
  77. * pData2 -- 要异或的第二组数据。
  78. * nCount -- 参与异或的数据长度。
  79. * 输出参数: pData1 -- 异或后的结果。
  80. * 返回值: 无。
  81. *****************************************************************************/
  82. static void XorBytes(unsigned char* pData1, const unsigned char* pData2, unsigned char nCount)
  83. {
  84. unsigned char i;
  85. for (i = 0; i < nCount; i++) {
  86. pData1[i] ^= pData2[i];
  87. }
  88. }
  89. /*****************************************************************************
  90. * 函数名: AddKey
  91. * 功能描述: 把 pData数据 加上(异或)pKey密钥,数据长度为16字节。
  92. * 输入参数: pData -- 数据。
  93. * pKey -- 密钥。
  94. * 输出参数: pData -- 加上子密钥后的数据。
  95. * 返回值: 无。
  96. *****************************************************************************/
  97. static void AddKey(unsigned char* pData, const unsigned char* pKey)
  98. {
  99. XorBytes(pData, pKey, 4 * Nb);
  100. }
  101. /*****************************************************************************
  102. * 函数名: SubstituteBytes
  103. * 功能描述: 通过S盒子置换数据。
  104. * 输入参数: pData -- 数据。
  105. * dataCnt -- 数据长度。
  106. * pBox -- 置换盒子,加密时使用SBox, 解密时使用InvSBox
  107. * 输出参数: pData -- 置换后的状态数据。
  108. * 返回值: 无。
  109. *****************************************************************************/
  110. static void SubstituteBytes(unsigned char* pData, unsigned char dataCnt, const unsigned char* pBox)
  111. {
  112. unsigned char i;
  113. for (i = 0; i < dataCnt; i++) {
  114. pData[i] = pBox[pData[i]];
  115. }
  116. }
  117. /*****************************************************************************
  118. * 函数名: ShiftRows
  119. * 功能描述: 把状态数据移行。
  120. * 输入参数: pState -- 状态数据。
  121. * bInvert -- 是否反向移行(解密时使用)。
  122. * 输出参数: pState -- 移行后的状态数据。
  123. * 返回值: 无。
  124. *****************************************************************************/
  125. static void ShiftRows(unsigned char* pState, unsigned char bInvert)
  126. {
  127. // 注意:状态数据以列形式存放!
  128. unsigned char r; // row, 行
  129. unsigned char c; // column,列
  130. unsigned char temp;
  131. unsigned char rowData[4];
  132. for (r = 1; r < 4; r++) {
  133. // 备份一行数据
  134. for (c = 0; c < 4; c++) {
  135. rowData[c] = pState[r + 4 * c];
  136. }
  137. temp = bInvert ? (4 - r) : r;
  138. for (c = 0; c < 4; c++) {
  139. pState[r + 4 * c] = rowData[(c + temp) % 4];
  140. }
  141. }
  142. }
  143. /*****************************************************************************
  144. * 函数名: GfMultBy02
  145. * 功能描述: 在GF(28)域的 乘2 运算。
  146. * 输入参数: num -- 乘数。
  147. * 输出参数: 无。
  148. * 返回值: num乘以2的结果。
  149. *****************************************************************************/
  150. static unsigned char GfMultBy02(unsigned char num)
  151. {
  152. if (0 == (num & 0x80)) {
  153. num = num << 1;
  154. }
  155. else {
  156. num = (num << 1) ^ BPOLY;
  157. }
  158. return num;
  159. }
  160. /*****************************************************************************
  161. * 函数名: MixColumns
  162. * 功能描述: 混合各列数据。
  163. * 输入参数: pData -- 数据。
  164. * bInvert -- 是否反向混合(解密时使用)。
  165. * 输出参数: pData -- 混合列后的状态数据。
  166. * 返回值: 无。
  167. *****************************************************************************/
  168. static void MixColumns(unsigned char* pData, unsigned char bInvert)
  169. {
  170. unsigned char i;
  171. unsigned char temp;
  172. unsigned char a0Pa2_M4; // 4(a0 + a2)
  173. unsigned char a1Pa3_M4; // 4(a1 + a3)
  174. unsigned char result[4];
  175. for (i = 0; i < 4; i++, pData += 4) {
  176. temp = pData[0] ^ pData[1] ^ pData[2] ^ pData[3];
  177. result[0] = temp ^ pData[0] ^ GfMultBy02((unsigned char)(pData[0] ^ pData[1]));
  178. result[1] = temp ^ pData[1] ^ GfMultBy02((unsigned char)(pData[1] ^ pData[2]));
  179. result[2] = temp ^ pData[2] ^ GfMultBy02((unsigned char)(pData[2] ^ pData[3]));
  180. result[3] = temp ^ pData[3] ^ GfMultBy02((unsigned char)(pData[3] ^ pData[0]));
  181. if (bInvert) {
  182. a0Pa2_M4 = GfMultBy02(GfMultBy02((unsigned char)(pData[0] ^ pData[2])));
  183. a1Pa3_M4 = GfMultBy02(GfMultBy02((unsigned char)(pData[1] ^ pData[3])));
  184. temp = GfMultBy02((unsigned char)(a0Pa2_M4 ^ a1Pa3_M4));
  185. result[0] ^= temp ^ a0Pa2_M4;
  186. result[1] ^= temp ^ a1Pa3_M4;
  187. result[2] ^= temp ^ a0Pa2_M4;
  188. result[3] ^= temp ^ a1Pa3_M4;
  189. }
  190. memcpy(pData, result, 4);
  191. }
  192. }
  193. /*****************************************************************************
  194. * 函数名: BlockEncrypt
  195. * 功能描述: 对单块数据加密。
  196. * 输入参数: pData -- 要加密的块数据。
  197. * 输出参数: pData -- 加密后的块数据。
  198. * 返回值: 无。
  199. *****************************************************************************/
  200. static void BlockEncrypt(AESInfo_t* aesInfoP, unsigned char* pData)
  201. {
  202. unsigned char i;
  203. AddKey(pData, aesInfoP->expandKey);
  204. for (i = 1; i <= aesInfoP->Nr; i++) {
  205. SubstituteBytes(pData, 4 * Nb, SBox);
  206. ShiftRows(pData, 0);
  207. if (i != aesInfoP->Nr) {
  208. MixColumns(pData, 0);
  209. }
  210. AddKey(pData, &aesInfoP->expandKey[4 * Nb * i]);
  211. }
  212. }
  213. /*****************************************************************************
  214. * 函数名: BlockDecrypt
  215. * 功能描述: 对单块数据解密。
  216. * 输入参数: pData -- 要解密的数据。
  217. * 输出参数: pData -- 解密后的数据。
  218. * 返回值: 无。
  219. *****************************************************************************/
  220. static void BlockDecrypt(AESInfo_t* aesInfoP, unsigned char* pData)
  221. {
  222. unsigned char i;
  223. AddKey(pData, &aesInfoP->expandKey[4 * Nb * aesInfoP->Nr]);
  224. for (i = aesInfoP->Nr; i > 0; i--) {
  225. ShiftRows(pData, 1);
  226. SubstituteBytes(pData, 4 * Nb, InvSBox);
  227. AddKey(pData, &aesInfoP->expandKey[4 * Nb * (i - 1)]);
  228. if (1 != i) {
  229. MixColumns(pData, 1);
  230. }
  231. }
  232. }
  233. /*****************************************************************************
  234. * 函数名: AESAddPKCS7Padding
  235. * 描述: PKCS7 方式填充数据
  236. * 输入参数: data -- 后面最多预留16个字节空间用于存放填充值
  237. * len -- 数据的长度
  238. * 输出参数: data -- 添加填充码后的数据
  239. * 返回值: 填充后的长度
  240. *****************************************************************************/
  241. static unsigned int AESAddPKCS7Padding(unsigned char* data, unsigned int len)
  242. {
  243. unsigned int newLen;
  244. newLen = len + 16 - (len % 16);
  245. memset(&data[len], newLen - len, newLen - len);
  246. return newLen;
  247. }
  248. /*****************************************************************************
  249. * 函数名: AESDelPKCS7Padding
  250. * 描述: PKCS7Padding 填充密文解密后剔除填充值
  251. * 输入参数: pData -- 解密后的数据
  252. * len -- 数据的长度
  253. * 输出参数: pData -- 删除填充码后的数据
  254. * 返回值: 删除后的实际有效数据长度,为0表示传入的数据异常
  255. *****************************************************************************/
  256. static unsigned int AESDelPKCS7Padding(unsigned char* pData, unsigned int len)
  257. {
  258. if (0 != (len & 0x0F)) {//1组16字节,(0 != (len & 0x0F)说明不是16的倍数
  259. return 0;
  260. }
  261. if (pData[len - 1] > len) {
  262. return 0;
  263. }
  264. return len - pData[len - 1];
  265. }
  266. /*****************************************************************************
  267. * 函数名: AESInit
  268. * 功能描述: 初始化
  269. * 输入参数: aesInfoP -- 用户需要填充
  270. * 输出参数: 无。
  271. * 返回值: 无。
  272. *****************************************************************************/
  273. void AESInit(AESInfo_t* aesInfoP)
  274. {
  275. unsigned char i;
  276. unsigned char* pExpandKey;//扩展密钥
  277. unsigned char Rcon[4] = { 0x01, 0x00, 0x00, 0x00 };
  278. switch (aesInfoP->type) {
  279. case AES128:
  280. aesInfoP->Nr = 10;
  281. aesInfoP->Nk = 16;
  282. break;
  283. case AES192:
  284. aesInfoP->Nr = 12;
  285. aesInfoP->Nk = 24;
  286. break;
  287. case AES256:
  288. aesInfoP->Nr = 14;
  289. aesInfoP->Nk = 32;
  290. break;
  291. default:
  292. aesInfoP->Nr = 10;
  293. aesInfoP->Nk = 16;
  294. break;
  295. }
  296. //拓展密匙
  297. memcpy(aesInfoP->expandKey, aesInfoP->key, 4 * aesInfoP->Nk);//第一个是原始密匙,
  298. pExpandKey = &aesInfoP->expandKey[4 * aesInfoP->Nk]; //拓展密匙AES128:10个、AES192:12个、AES256:14个
  299. for (i = aesInfoP->Nk; i < Nb * (aesInfoP->Nr + 1); pExpandKey += 4, i++) {
  300. memcpy(pExpandKey, pExpandKey - 4, 4);
  301. if (0 == i % aesInfoP->Nk) {
  302. RShiftWord(pExpandKey);
  303. SubstituteBytes(pExpandKey, 4, SBox);
  304. XorBytes(pExpandKey, Rcon, 4);
  305. Rcon[0] = GfMultBy02(Rcon[0]);
  306. }
  307. else if (6 < aesInfoP->Nk && i % aesInfoP->Nk == Nb) {
  308. SubstituteBytes(pExpandKey, 4, SBox);
  309. }
  310. XorBytes(pExpandKey, pExpandKey - 4 * aesInfoP->Nk, 4);
  311. }
  312. }
  313. /*****************************************************************************
  314. * 函数名: AESEncrypt
  315. * 功能描述: 加密数据
  316. * 输入参数: aesInfoP -- 包含key、加密方式等初始化信息
  317. * pPlainText -- 要加密的数据,其长度为nDataLen字节。
  318. * dataLen -- 数据长度,以字节为单位,需要是整倍数,AES128:16倍数、AES192:24倍数、AES256:32倍数。
  319. * 输出参数: pCipherText -- 加密后的数据,预留长度:dataLen+16
  320. * 返回值: 解密后的数据长度。
  321. *****************************************************************************/
  322. unsigned int AESEncrypt(AESInfo_t* aesInfoP, const unsigned char* pPlainText, unsigned char* pCipherText, unsigned int nDataLen)
  323. {
  324. unsigned int i;
  325. const void* pIV = NULL;
  326. if (pPlainText != pCipherText) {
  327. memcpy(pCipherText, pPlainText, nDataLen);
  328. }
  329. //必须是16的整倍数,不够的填充,pkcs7算法是缺n补n个n,比如13字节数据缺了3个,后面就补3个3;如果刚好是16的倍数,就填充16个16
  330. nDataLen = AESAddPKCS7Padding(pCipherText, nDataLen);
  331. if (aesInfoP->pIV) {
  332. pIV = aesInfoP->pIV;
  333. }
  334. for (i = nDataLen / (4 * Nb); i > 0; i--, pCipherText += 4 * Nb) {
  335. if (AES_MODE_CBC == aesInfoP->mode) {
  336. XorBytes(pCipherText, pIV, 4 * Nb);
  337. }
  338. BlockEncrypt(aesInfoP, pCipherText);
  339. if (aesInfoP->pIV) {
  340. pIV = pCipherText;
  341. }
  342. }
  343. return nDataLen;
  344. }
  345. /*****************************************************************************
  346. * 函数名: AESDecrypt
  347. * 功能描述: 解密数据
  348. * 输入参数: aesInfoP -- 包含key、加密方式等初始化信息
  349. * pPlainText -- 要加密的数据,其长度为dataLen字节。
  350. * dataLen -- 数据长度,以字节为单位,需要是整倍数,AES128:16倍数、AES192:24倍数、AES256:32倍数。
  351. * 输出参数: pCipherText -- 加密后的数据,可以与pCipherText相同
  352. * 返回值: 返回解密后的数据长度。
  353. *****************************************************************************/
  354. unsigned int AESDecrypt(AESInfo_t* aesInfoP, unsigned char* pPlainText, const unsigned char* pCipherText, unsigned int dataLen)
  355. {
  356. unsigned int i;
  357. unsigned char* pPlainTextBack = pPlainText;
  358. if (pPlainText != pCipherText) {
  359. memcpy(pPlainText, pCipherText, dataLen);
  360. }
  361. //当mode=AES_MODE_CBC时需要从最后一块数据开始解密
  362. pPlainText += dataLen - 4 * Nb;
  363. for (i = dataLen / (4 * Nb); i > 0; i--, pPlainText -= 4 * Nb) {
  364. BlockDecrypt(aesInfoP, pPlainText);
  365. if (AES_MODE_CBC == aesInfoP->mode) {
  366. if (1 == i) {//原来的第一块数据是初始变量加密的
  367. XorBytes(pPlainText, aesInfoP->pIV, 4 * Nb);
  368. }
  369. else {
  370. XorBytes(pPlainText, pPlainText - 4 * Nb, 4 * Nb);
  371. }
  372. }
  373. }
  374. //因为数据需要16字节对齐,可能有填充数据,需要去除后面的填充数据
  375. return AESDelPKCS7Padding(pPlainTextBack, dataLen);
  376. }

main.c

  1. #include "aes.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h> //isprint
  5. void PrintData(const char* head, unsigned char* data, unsigned int len)
  6. {
  7. unsigned int i;
  8. printf("%s, len:%u:\r\n", head, len);
  9. //按16进制打印出来
  10. printf("HEX:[");
  11. for (i = 0; i < len; i++) {
  12. printf("%02X ", data[i]);
  13. }
  14. printf("]\r\n");
  15. //按ASCII码打印出来
  16. printf("ASCII:[");
  17. for (i = 0; i < len; i++) {
  18. if (isprint(data[i])) {//可打印字符
  19. printf("'%c' ", data[i]);
  20. }
  21. else {
  22. printf("\\%02X ", data[i]);
  23. }
  24. }
  25. printf("]\r\n");
  26. }
  27. void test_fun(void)
  28. {
  29. unsigned int len;
  30. printf("Build %s %s\r\n", __DATE__, __TIME__);
  31. for (int i = 1; i <= 6; i++)
  32. {
  33. switch (i)
  34. {
  35. case 1: {
  36. //秘钥, AES128 用16字节
  37. unsigned char key[16] = {
  38. 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,
  39. 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38
  40. };
  41. //初始化向量, 固定长度8个, 当mode=AES_MODE_CBC时用到
  42. unsigned char IV[2 * Nb] = { 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38 };
  43. //要加密的内容
  44. unsigned char sourceMsg[] = "Hello AES 128 Hello AES 128 Hello AES 128 Hello AES 128";
  45. //加密后的内容
  46. unsigned char encryptMsg[sizeof(sourceMsg) + 16] = { 0 };
  47. //解密后的内容
  48. unsigned char decryptMsg[sizeof(sourceMsg) + 16] = { 0 };
  49. //设置加密方式、密匙
  50. AESInfo_t aesInfo = {
  51. .type = AES128,
  52. .mode = AES_MODE_CBC,
  53. .key = key,
  54. .pIV = IV
  55. };
  56. printf("\r\n");
  57. printf(" AES MODE CBC .pIV = IV \r\n");
  58. printf("*************************** AES128 ******************************\n");
  59. PrintData("sourceMsg", sourceMsg, strlen((const char*)sourceMsg));
  60. //初始化
  61. AESInit(&aesInfo);
  62. //加密
  63. len = AESEncrypt(&aesInfo, sourceMsg, encryptMsg, strlen((const char*)sourceMsg));
  64. PrintData("encryptMsg", encryptMsg, len);
  65. //解密
  66. len = AESDecrypt(&aesInfo, decryptMsg, encryptMsg, len);
  67. PrintData("decryptMsg", decryptMsg, len);
  68. } break;
  69. case 2: {
  70. //秘钥, AES192 用24字节
  71. unsigned char key[24] = {
  72. 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x41,0x42,
  73. 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x41,0x42
  74. };
  75. //初始化向量, 固定长度12个, 当mode=AES_MODE_CBC时用到
  76. unsigned char IV[3 * Nb] = { 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x41,0x42 };
  77. //要加密的内容
  78. unsigned char sourceMsg[] = "Hello AES 192";
  79. //加密后的内容
  80. unsigned char encryptMsg[sizeof(sourceMsg) + 16] = { 0 };
  81. //解密后的内容
  82. unsigned char decryptMsg[sizeof(sourceMsg) + 16] = { 0 };
  83. //设置加密方式、密匙
  84. AESInfo_t aesInfo = {
  85. .type = AES192,
  86. .mode = AES_MODE_CBC,
  87. .key = key,
  88. .pIV = IV
  89. };
  90. printf("*************************** AES192 ******************************\n");
  91. PrintData("sourceMsg", sourceMsg, strlen((const char*)sourceMsg));
  92. //初始化
  93. AESInit(&aesInfo);
  94. //加密
  95. len = AESEncrypt(&aesInfo, sourceMsg, encryptMsg, strlen((const char*)sourceMsg));
  96. PrintData("encryptMsg", encryptMsg, len);
  97. //解密
  98. len = AESDecrypt(&aesInfo, decryptMsg, encryptMsg, len);
  99. PrintData("decryptMsg", decryptMsg, len);
  100. } break;
  101. case 3: {
  102. //秘钥, AES256 用32字节
  103. unsigned char key[32] = {
  104. 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x41,0x42,0x43,0x44,0x45,0x46,
  105. 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x41,0x42,0x43,0x44,0x45,0x46
  106. };
  107. //初始化向量, 固定长度16个, 当mode=AES_MODE_CBC时用到
  108. unsigned char IV[4 * Nb] = { 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x41,0x42,0x43,0x44,0x45,0x46 };
  109. //要加密的内容
  110. unsigned char sourceMsg[] = "Hello AES 256";
  111. //加密后的内容
  112. unsigned char encryptMsg[sizeof(sourceMsg) + 16] = { 0 };
  113. //解密后的内容
  114. unsigned char decryptMsg[sizeof(sourceMsg) + 16] = { 0 };
  115. //设置加密方式、密匙
  116. AESInfo_t aesInfo = {
  117. .type = AES256,
  118. .mode = AES_MODE_CBC,
  119. .key = key,
  120. .pIV = IV
  121. };
  122. printf("*************************** AES256 ******************************\n");
  123. PrintData("sourceMsg", sourceMsg, strlen((const char*)sourceMsg));
  124. //初始化
  125. AESInit(&aesInfo);
  126. //加密
  127. len = AESEncrypt(&aesInfo, sourceMsg, encryptMsg, strlen((const char*)sourceMsg));
  128. PrintData("encryptMsg", encryptMsg, len);
  129. //解密
  130. len = AESDecrypt(&aesInfo, decryptMsg, encryptMsg, len);
  131. PrintData("decryptMsg", decryptMsg, len);
  132. } break;
  133. case 4: {
  134. //秘钥, AES128 用16字节
  135. unsigned char key[16] = {
  136. 0x2b,0x7e,0x15,0x16,0x17,0x18,0xd2,0xa6,
  137. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  138. };
  139. //要加密的内容
  140. unsigned char sourceMsg[] = {
  141. 0x32,0x43,0xf6,0xa8,0x88,0x89,0x30,0x8d,
  142. 0x31,0x32,0x98,0xa2,0xe0,0x37,0x07,0x34
  143. };
  144. //加密后的内容
  145. unsigned char encryptMsg[sizeof(sourceMsg) + 16] = { 0 };
  146. //解密后的内容
  147. unsigned char decryptMsg[sizeof(sourceMsg) + 16] = { 0 };
  148. //设置加密方式、密匙
  149. AESInfo_t aesInfo = {
  150. .type = AES128,
  151. .mode = AES_MODE_ECB,
  152. .key = key,
  153. .pIV = NULL
  154. };
  155. printf("\r\n");
  156. printf(" AES MODE ECB .pIV = NULL \r\n");
  157. printf("*************************** AES128 ******************************\n");
  158. PrintData("sourceMsg", sourceMsg, sizeof(sourceMsg)/sizeof(unsigned char));
  159. //初始化
  160. AESInit(&aesInfo);
  161. //加密
  162. len = AESEncrypt(&aesInfo, sourceMsg, encryptMsg, sizeof(sourceMsg) / sizeof(unsigned char));
  163. PrintData("encryptMsg", encryptMsg, len);
  164. //解密
  165. len = AESDecrypt(&aesInfo, decryptMsg, encryptMsg, len);
  166. PrintData("decryptMsg", decryptMsg, len);
  167. } break;
  168. case 5: {
  169. //秘钥, AES192 用24字节
  170. unsigned char key[24] = {
  171. 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x41,0x42,
  172. 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x41,0x42
  173. };
  174. //要加密的内容
  175. unsigned char sourceMsg[] = {
  176. 0x32,0x43,0xf6,0xa8,0x88,0x89,0x30,0x8d,0x25,
  177. 0x31,0x32,0x98,0xa2,0xe0,0x37,0x07,0x34,0x11
  178. };
  179. //加密后的内容
  180. unsigned char encryptMsg[sizeof(sourceMsg) + 16] = { 0 };
  181. //解密后的内容
  182. unsigned char decryptMsg[sizeof(sourceMsg) + 16] = { 0 };
  183. //设置加密方式、密匙
  184. AESInfo_t aesInfo = {
  185. .type = AES192,
  186. .mode = AES_MODE_ECB,
  187. .key = key,
  188. .pIV = NULL
  189. };
  190. printf("*************************** AES192 ******************************\n");
  191. PrintData("sourceMsg", sourceMsg, strlen((const char*)sourceMsg));
  192. //初始化
  193. AESInit(&aesInfo);
  194. //加密
  195. len = AESEncrypt(&aesInfo, sourceMsg, encryptMsg, strlen((const char*)sourceMsg));
  196. PrintData("encryptMsg", encryptMsg, len);
  197. //解密
  198. len = AESDecrypt(&aesInfo, decryptMsg, encryptMsg, len);
  199. PrintData("decryptMsg", decryptMsg, len);
  200. } break;
  201. case 6: {
  202. //秘钥, AES256 用32字节
  203. unsigned char key[32] = {
  204. 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x41,0x42,0x43,0x44,0x45,0x46,
  205. 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x41,0x42,0x43,0x44,0x45,0x46
  206. };
  207. //要加密的内容
  208. unsigned char sourceMsg[] = {
  209. 0x32,0x43,0xf6,0xa8,0x88,0x89,0x30,0x8d,0x55,
  210. 0x31,0x32,0x98,0xa2,0xe0,0x37,0x07,0x34,0x53
  211. };
  212. //加密后的内容
  213. unsigned char encryptMsg[sizeof(sourceMsg) + 16] = { 0 };
  214. //解密后的内容
  215. unsigned char decryptMsg[sizeof(sourceMsg) + 16] = { 0 };
  216. //设置加密方式、密匙
  217. AESInfo_t aesInfo = {
  218. .type = AES256,
  219. .mode = AES_MODE_ECB,
  220. .key = key,
  221. .pIV = NULL
  222. };
  223. printf("*************************** AES256 ******************************\n");
  224. PrintData("sourceMsg", sourceMsg, strlen((const char*)sourceMsg));
  225. //初始化
  226. AESInit(&aesInfo);
  227. //加密
  228. len = AESEncrypt(&aesInfo, sourceMsg, encryptMsg, strlen((const char*)sourceMsg));
  229. PrintData("encryptMsg", encryptMsg, len);
  230. //解密
  231. len = AESDecrypt(&aesInfo, decryptMsg, encryptMsg, len);
  232. PrintData("decryptMsg", decryptMsg, len);
  233. } break;
  234. default:
  235. break;
  236. }
  237. }
  238. }
  239. int main(int argc, char* argv[])
  240. {
  241. test_fun();
  242. return 0;
  243. }

3、运行结果

  在线验证工具    1、链接    2、链接 

4、总结

        希望能帮助到需要的攻城狮。

参考文章 https://blog.csdn.net/nanfeibuyi/article/details/125474445

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号