C BIN加密
#include <stdio.h>
#include <string.h>
#include <stdlib.h> #ifndef DWORD
#define DWORD unsigned int
#endif #ifndef BYTE
#define BYTE unsigned char
#endif #ifndef bool
#define bool unsigned int
#endif #define FLASH_HW_KEY 0x08825252
#define FLASH_HW_KEY_0 ((FLASH_HW_KEY <<(32-11)) |(FLASH_HW_KEY>>11))
#define FLASH_HW_KEY_1 ((FLASH_HW_KEY <<(32-20)) |(FLASH_HW_KEY>>20))
#define FLASH_HW_KEY_2 ((FLASH_HW_KEY <<(32-26)) |(FLASH_HW_KEY>>26))
#define FLASH_HW_KEY_3 ((FLASH_HW_KEY <<(32-7)) |(FLASH_HW_KEY>>7)) DWORD cmN_BytesToDWORD(char* bytes)
{
int i;
DWORD value;
value = ;
char* tmpByte;
//printf("cmN_BytesToDWORD %p\n", bytes);
for(i=;i<;i++)
{
tmpByte = &bytes[i];
if(tmpByte)
{
value |= (*tmpByte & 0xFF) <<(*i);
//printf("%x\t ", value);
}
}
return value;
} void cmN_DWORDToBytes(char* bytes, DWORD value)
{
int i;
char* tmpbyte; for(i=;i<;i++)
{
tmpbyte = &bytes[i];
*tmpbyte = (BYTE)(value >> (*i)) & 0xFF;
}
} bool cmN_GetBitFromDword(DWORD data, BYTE i)
{
if( (data>>i)& )
return ;
else
return ;
} DWORD Mix_0(DWORD data)
{
return ( (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<) );
} DWORD Mix_1(DWORD data)
{
return ( (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<) );
} DWORD Mix_2(DWORD data)
{
return ( (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<) );
} DWORD Mix_3(DWORD data)
{
return ( (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<) );
} DWORD unMix_0(DWORD data)
{
return ( (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<) );
}
DWORD unMix_1(DWORD data)
{
return ( (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<) );
} DWORD unMix_2(DWORD data)
{
return ( (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<) );
}
DWORD unMix_3(DWORD data)
{
return ( (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<)
| (cmN_GetBitFromDword(data, )<<) );
} void hwEncodeRawDataForFlash(char * pChar, DWORD length, DWORD ref_address)
{
DWORD tmplength=;
DWORD in_data, data_add, pre_xor_data, out_data;
BYTE type; //to handle for each DWORD
while( tmplength < length)
{
//printf("tmplength %d\n", tmplength);
in_data = cmN_BytesToDWORD(pChar);
data_add = in_data + FLASH_HW_KEY;
type = (BYTE)(ref_address>>) & 0x03; if(type==)
{
pre_xor_data = Mix_0(data_add);
out_data = pre_xor_data^FLASH_HW_KEY_0;
cmN_DWORDToBytes(pChar, out_data); }
else if(type==)
{
pre_xor_data = Mix_1(data_add);
out_data = pre_xor_data^FLASH_HW_KEY_1;
cmN_DWORDToBytes(pChar, out_data);
}
else if(type==)
{
pre_xor_data = Mix_2(data_add);
out_data = pre_xor_data^FLASH_HW_KEY_2;
cmN_DWORDToBytes(pChar, out_data);
}
else if(type==)
{
pre_xor_data = Mix_3(data_add);
out_data = pre_xor_data^FLASH_HW_KEY_3;
cmN_DWORDToBytes(pChar, out_data);
} ref_address += ;
pChar += ;
tmplength += ;
} pChar -= tmplength;
} void hwDecodeRawDataForFlash(char * pChar, DWORD length, DWORD ref_address)
{
DWORD tmplength=;
DWORD in_data, pre_xor_data, out_data;
BYTE type; while( tmplength < length)
{
in_data = cmN_BytesToDWORD(pChar);
type = (BYTE)(ref_address>>) & 0x03;
if(type==)
{
in_data = in_data^FLASH_HW_KEY_0;
pre_xor_data = unMix_0(in_data);
out_data = pre_xor_data - FLASH_HW_KEY;
cmN_DWORDToBytes(pChar, out_data); }
else if(type==)
{
in_data = in_data^FLASH_HW_KEY_1;
pre_xor_data = unMix_1(in_data);
out_data = pre_xor_data - FLASH_HW_KEY;
cmN_DWORDToBytes(pChar, out_data);
}
else if(type==)
{
in_data = in_data^FLASH_HW_KEY_2;
pre_xor_data = unMix_2(in_data);
out_data = pre_xor_data - FLASH_HW_KEY;
cmN_DWORDToBytes(pChar, out_data);
}
else if(type==)
{
in_data = in_data^FLASH_HW_KEY_3;
pre_xor_data = unMix_3(in_data);
out_data = pre_xor_data - FLASH_HW_KEY;
cmN_DWORDToBytes(pChar, out_data);
} ref_address += ;
pChar += ;
tmplength += ;
} pChar -= tmplength;
} #define BUF_SIZE 128 main(int argc,char *argv[])
{
char buf[BUF_SIZE];
char outFile[]; FILE *filein = fopen( argv[], "rb" );
if(filein == NULL) {
printf("open %s fail\n", argv[] );
return ;
}
memset(outFile,,);
sprintf(outFile,"decrypt_%s",argv[]+);
//printf("--->%s\n",outFile);
FILE *fileout=fopen(outFile, "wb" );
if(fileout == NULL) {
printf("open outfile fail\n" );
return ;
} while()
{
int ret = fread(buf, , BUF_SIZE, filein);
if(ret <=)
{
break;
}
hwDecodeRawDataForFlash( &buf[], BUF_SIZE, );
fwrite(buf, , BUF_SIZE, fileout);
}
fclose(filein);
fclose(fileout);
return ; }
C BIN加密的更多相关文章
- AM335X的应用程序自启动流程以及U盘更新应用程序记录
在AM335X的SD卡更新系统学习记录中最后更新完系统后,以后运行应用程序都会从EMMC中取出Linux系统运行.接着介绍Linux系统是怎么自己启动我们编写的应用程序的. 1.在AM335X的SD卡 ...
- Encrypting bootloader (程序BIN文件加密及在线升级)
了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 在上一个博客随笔,我介 ...
- Encrypted bootloader (程序BIN文件加密及在线升级)
了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 在上一个博客随笔,我介 ...
- PHP的学习--RSA加密解密
PHP服务端与客户端交互或者提供开放API时,通常需要对敏感的数据进行加密,这时候rsa非对称加密就能派上用处了. 举个通俗易懂的例子,假设我们再登录一个网站,发送账号和密码,请求被拦截了. 密码没加 ...
- 如何使用DotfuscatorPro_4.9对软件进行加密
DotfuscatorPro_4.9可以防止你的.NET软件被反编译,可以在一定程度上防止你的软件被反编译.现在很多软件都有被反编译的现象,虽然不能做到百分百的防范,但是你至少可以先做些技术上的处理, ...
- node.js下使用RSA加密事例(windows)
1.安装openss 直接下载window下的安装包 http://houjixin.blog.163.com/blog/static/3562841020144143494875/ 以我发博文现在的 ...
- php rsa加密解密实例
1.加密解密的第一步是生成公钥.私钥对,私钥加密的内容能通过公钥解密(反过来亦可以) 下载开源RSA密钥生成工具openssl(通常Linux系统都自带该程序),解压缩至独立的文件夹,进入其中的bin ...
- MySQL的SSL加密连接与性能开销
本文转载自:http://www.innomysql.net/article/23959.html(只作转载, 不代表本站和博主同意文中观点或证实文中信息) Contents [hide] 1 前言 ...
- windows下OpenSSL加密证书安装步骤与使用方法
OpenSSL加密证书一般用于签名认证,含私钥和公钥.在Linux系统中,OpenSSL一般是已经安装好了,可以直接使用.而在Windows系统中,是需要安装使用的. 最近在使用支付平台时,用到了Op ...
随机推荐
- 20145326《Java程序设计》实验二Java面向对象程序设计实验报告
20145326<Java程序设计>实验二Java面向对象程序设计实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O. ...
- Java8 Lambda
Demo: package com.qhong; public class Main { public static void main(String[] args) throws Exception ...
- curl使用介绍
linux curl是通过url语法在命令行下上传或下载文件的工具软件,它支持http,https,ftp,ftps,telnet等多种协议,常被用来抓取网页和监控Web服务器状态. 一.Linux ...
- Maven简单的配置Junit测试及使用简单的mock
1.maven依赖配置如下 <dependency> <groupId>org.mockito</groupId> <artifactId>mockit ...
- shell 输入不显示在监视器上
#!/bin/bash read -s -p "Enter your password:" pass echo "your password is $pass" ...
- Spring IOC 源码简单分析 04 - bean的初始化
### 准备 ## 目标 了解 Spring 如何初始化 bean 实例 ##测试代码 gordon.study.spring.ioc.IOC04_Initialization.java publ ...
- bzoj2152: 聪聪可可 树分治
sb树分治 /************************************************************** Problem: 2152 User: walfy Lang ...
- web sql 基本操作 - 增删改查
不喜欢看md原文的 可以访问这个链接:http://note.youdao.com/noteshare?id=6a91e3dea7cdf5195bb0e851d9fcb5a5 # web sql 增删 ...
- UVA-1149 Bin Packing (贪心)
题目大意:给定n个物品的重量,无限个容量为m的箱子,每个箱子最多装两个物品,要把所有的物品都装下,最少需要多少个箱子. 题目分析:贪心策略:每次将最重和最轻的两个物品放到一个箱子里,如果装不下,则将最 ...
- STL中的map
map 容器 提供 1 对 1 的关系 定义方式: map<string,int>mp; 写在前面的是关键字. 数据插入: 1.使用 insert 插入 pair 数据 mp.insert ...