#include <stdio.h>
#include <string.h>
#include <stdlib.h>//free
#define MAXBINSIZE 200*1024
int main(int argc,char **argv)
{
FILE *fp1,*fp2,*fp3;
char ch,j=0;
int lSize=0,rename0isOK=0;
char *name = NULL;
unsigned char buffer[MAXBINSIZE]={0};
unsigned char loop[17]={"\0\0\0\0\0\0\0\0\1\2\3\4\4\3\2\1"};//[16]不行 err
//unsigned char loop[16]={0,0,0,0,0,0,0,0,1,2,3,4,4,3,2,1};
if(argc==1)
{
name = (char *)"TSL-TAG-RC-G10-SW-AIOT-GTB-V1.0.0.hex";
printf("HEXNAME %s\r\n",name);
}
else if(argc==2)
{
name = argv[1];
printf("HEXNAME %s\r\n",name);
}
fp1 = fopen("STM32F412RE/STM32F412REbootloader.hex", "r");
fp3 = fopen(name, "w");
ch = fgetc(fp1);
while(ch!=EOF)
{
fputc(ch , fp3);
ch = fgetc(fp1);
}
fclose(fp1);
fseek (fp3 , -13 , SEEK_END);//从尾部往左边走13 正好丢弃最后1行
fp2 = fopen("STM32F412RE/STM32F412REapp.hex", "r");
ch = fgetc(fp2);
while(ch!=EOF)
{
fputc(ch , fp3);
ch = fgetc(fp2);
}
fclose(fp2);
fclose(fp3);
free(fp1);
free(fp2);
free(fp3);
printf("******GKOSON FINISHED*******");
#if 0
//文件重命名 MV或者CP 把前面的bin MV到外面去
//https://blog.csdn.net/qq_41668547/article/details/85345581
lSize = strlen(name);
name[lSize-1]='n';
name[lSize-2]='i';
name[lSize-3]='b';
rename0isOK = rename("STM32F412RE/STM32F412REapp.bin",name);
printf("******RENAME BIN %d*******",rename0isOK);
//scanf("%d\n",&ch);//人为堵塞一下
#else
lSize = strlen(name);
name[lSize-1]='n';
name[lSize-2]='i';
name[lSize-3]='b';
printf("BINNAME %s\r\n",name);
fp1 = fopen("STM32F412RE/STM32F412REapp.bin", "rb");
fseek(fp1,0,SEEK_END);//光标到尾巴
lSize = ftell(fp1);//光标的位置就是长度了
printf("bin len =%d \n",lSize);
fclose(fp1);
fp1 = fopen("STM32F412RE/STM32F412REapp.bin", "rb");
fp2 = fopen(name, "wb");//之前错误 是这里没有b 导致每个不是正好4096
memset(buffer,0,MAXBINSIZE);
fread (buffer, sizeof(char), lSize, fp1);//开始加密
//开始简单加密
//for(int i=0;i<lSize;i++)
//buffer[i] ^= 0XAB;
//结束简单加密
//开始加密V2.0
//1--希望有个滚子 让别人看上去在前面80你没有加密
//2--希望后面的00 00不要暴露自己
for(int i=0;i<lSize;i++)
{
buffer[i] ^= loop[j];
j=(j+1)%16; //[0--1--2---15]--0
};
fwrite(buffer, sizeof(char), lSize, fp2);
fclose(fp1);
fclose(fp2);
free(fp1);
free(fp2);
printf("******BIN SUCCESS*******");
#endif
return 1;
}
类似文章:这个加密比较简单 https://mp.weixin.qq.com/s/4e44TCm2BrSagfFtG5LDrA