java两个二进制互换位置_二进制文件每两个的字节位置交换

(一).写作缘由:

写这篇博客的目的是是为了方便下次使用或者帮助其他需要的人。在打CTF的时候,偶尔会遇到还原一些文件,笔者遇到的是分析数据流量的时候,提取出了一个未知文件,用二进制编辑器打开,搜所文件头,发现和某个文件头有点相似,但是每两个字节位置颠倒了,于是就想到把每两个字节交换位置,就像下面这种:

02b294298b49ddb92260d2c447c99e5d.png

5e5ca1420ffeba5ea2033bffb288adaa.png

(二).演示及效果:

在命令行执行前后如下图:

dd019fba0f340a40767ac41f064d4f94.png

d116ef2f13eaa1c127a6afa4e3e22f63.png

a11ab08500d3dc569e79b8a720544525.png

(三).贴上代码:

代码是C写的,有点多不太美观,功能太单一, 也没弄啥模块化,编写环境是windows。#include 

#include 

#include 

int main(int argc, const char** argv)

{

//The pointer of input file and output file

FILE *fin;

FILE* fout;

//The file name of input file and output

char* inFile;

char* outFile;

//The pointer of single and double byte type

unsigned short* pDouble = NULL;

unsigned char* pSingle = NULL;

//The memery buffer of header and tail pointer about input file

void* fBuffStart = NULL;

void* fBuffEnd = NULL;

//The size of input file (Byte)

unsigned long fileSize = 0;

//Judge the count of parameter and the file name limit

if(argc != 3 || strlen(argv[1]) > 255 || strlen(argv[2]) > 255)

{

printf("\n[-]Usage: %s infile outfile\n", argv[0]);

printf("[-]Filename limited: 255 Byte\n");

exit(-1);

}

inFile = argv[1];

outFile = argv[2];

//Exception handling

if(!(fin = fopen(inFile, "rb")))

{

printf("Error: open %s failed!\n", inFile);

exit(-1);

}

//Obtain file size

fseek(fin, 0, SEEK_END);

fileSize = ftell(fin);

fseek(fin, 0, SEEK_SET);

//Alloc memery  for input file and read its data to memery

fBuffStart = (unsigned char*)malloc(fileSize);

memset(fBuffStart, 0, fileSize);

fread(fBuffStart, 1, fileSize, fin);

fclose(fin);

//The position of start and end input file memery

fBuffEnd = (unsigned char*)fBuffStart + fileSize;

pDouble = (unsigned short*)fBuffStart;

//Exception handling

if(!(fout = fopen(outFile,"wb")))

{

printf("Error: open %s failed!\n", outFile);

exit(-1);

}

//Exchange position of each two byte

while(pDouble != fBuffEnd)

{

pSingle = pDouble;

fwrite(pSingle+1, 1, 1, fout);

fwrite(pSingle, 1, 1, fout);

pDouble++;

}

fclose(fout);

return 0;

}

(四).若有不足之处,还请斧正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值