标题:
STM32 16字节加解密的AES算法程序源码
[打印本页]
作者:
wwchang
时间:
2018-4-26 15:19
标题:
STM32 16字节加解密的AES算法程序源码
stm32 AES动态加密算法,大家可以下载进行测试
1、该算法工程是基于Keil5环境进行编译下载,单片机型号是stm32f030c8.
2、打开文件夹Project,双击"16BytesAES.uvprojx",即可打开工程。
3、如果不想利用该工程,只需要拷贝文件夹USER下的AES.c,AES.h,main.c到自己的工程,修改头文件即可。
stm32单片机源程序如下:
/**
******************************************************************************
* @file GPIO/GPIO_IOToggle/main.c
* @author MCD Application Team
* @version V1.4.0
* @brief Main program body
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2>
*
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.st.com/software_license_agreement_liberty_v2
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx.h"
#include "stm32f0xx_conf.h"
#include "stdio.h"
#include "stdlib.h"
#include "Mcu_config.h"
#include "Serial.h"
#include "AES.h"
/** @addtogroup STM32F0xx_StdPeriph_Examples
* @{
*/
/** @addtogroup GPIO_IOToggle
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
* @brief Main program.
* @param None
* @retval None
*/
char expressText[1024]; //存放待加密的明文数据,具体缓存大小根据用户待加密数据长度自己任意修改
char cipherText[1024];//存放已加密的密文数据,具体缓存大小根据用户解密后的数据长度自己任意修改
char aesKey[16];//加解密的密钥,注意:此算法只适用于AES16位密钥加解密,不适用于32位密钥加解密
//该AES算法加密方式为:AES-128bit/ECB/PKCS5Padding
/*
欢迎加入
二手开发板供求交易群 68936520
arm交流学习群 324753668
stm32交流学习群 227043677
*/
int main(void)
{
MCU_Init();
delay_1ms(2000);
memcpy(aesKey , "1uweiIDAS7awOas8" , 16); //AES加密密钥,16字节(128bit)
memset(expressText ,0 ,1024);
memset(cipherText , 0 ,1024);
strcpy(expressText , "Hello!!!Are you ready???");
AES_Encrypt(expressText , cipherText , aesKey); //use aesKey encrypt
memset(expressText ,0 ,1024);
AES_Decrypt(expressText , cipherText , aesKey);//use aesKey decrypt
while(1)
{
delay_1ms(2000);
}
}
/**
* @brief Retargets the C library printf function to the USART.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART2, (uint8_t) ch);
/* Loop until transmit data register is empty */
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
{}
return ch;
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
void fault_handler_c(unsigned int * hardfault_args, unsigned int type);
……………………
…………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
16字节加解密的AES算法.zip
(1.59 MB, 下载次数: 400)
2018-4-26 15:18 上传
点击文件名下载附件
AES加密算法
下载积分: 黑币 -5
作者:
aestest
时间:
2018-4-28 16:20
好资料,学习下
作者:
aestest
时间:
2018-5-2 15:20
学习
作者:
sunzhilei
时间:
2018-6-21 11:00
很好,下来学习一下。
作者:
jdzczz
时间:
2018-8-28 09:49
好好学习一下!
作者:
gmy2171
时间:
2018-8-31 15:38
加密模式是什么?ECB,CBC,CTR?
作者:
sf116
时间:
2018-9-3 14:44
测试没通过
作者:
hayppy
时间:
2019-1-23 14:52
好资料不错
作者:
hfahuang
时间:
2019-1-23 16:13
好资料,不错
作者:
Steven1996
时间:
2019-1-24 10:16
好资料 试一下
作者:
南国枫火
时间:
2019-3-7 10:56
代码有问题,测试结果错误
作者:
dabendan001
时间:
2019-6-4 11:51
//去除末尾补码
buIndex = str2[aesDataLen-1];
for(i=0; i<buIndex; i++)
{
if(str2[aesDataLen-i-1] != buIndex)
{
//UART_Printf("\nit is a wrong data. \n");
//free(str2); //pming,2019.06.04
//return;
break;
}
else
str2[aesDataLen-i-1] = '\0';
}
strcpy(pExpressText , str2);
free(str2);
解密返回为空。我将 加日期批注的地方2行,修改之后,可以解密。但长度似乎不对。
作者:
allenlinhao
时间:
2019-7-4 19:34
楼主的东西我测试了的,ok,没问题,谢谢
作者:
oulk
时间:
2019-8-26 09:46
下载几次都失败了
作者:
zwei99999999
时间:
2019-8-29 04:39
好资料,学习下
作者:
浅笑无痕殇
时间:
2019-9-5 16:02
遇到了12楼一样的问题,解密后返回为空
作者:
liao12338392
时间:
2019-9-20 15:11
好资料,学习下
作者:
ssack
时间:
2019-11-15 09:45
看起来资料不错, 没分下载, 楼主能不能分少点
作者:
No1
时间:
2019-12-6 17:20
解密数据为空的啊
作者:
canghaiwuhen
时间:
2019-12-9 10:20
试试看能不能用
作者:
mingda33
时间:
2020-2-24 11:20
谢谢分享,刚好想参考一下,学习AES加密
作者:
dbwu8280
时间:
2020-4-2 22:18
正好用到,谢谢分享!
作者:
baonng
时间:
2023-12-14 10:12
我现在也需要,下载学习。
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1