兜兜    2018-10-18 21:20:29    2018-10-18 21:20:29   

图片解码 base64
### 获取到.jpg文件,windows打开显示已损坏 #### 查看文件的类型 ```bash $ file test.jpg ``` ``` test.jpg: ASCII text, with very long lines, with no line terminators ``` #### 显示文件为ASCII编码,进一步查看文件内容 ```bash $ cat test.jpg ``` ``` /43/DgABpGSUZAAQEBAABgAGAAD/vQA0AHBQUGBQQHBgUGCAcHCAoBGwoJCQoFHwAcARgVGhkYFRgXGx4XISsdFS0XGBIuIiUoKSssKyoQLyM... ``` #### 猜测应该是应用了base64编码,通过base64解码 ```bash $ cat test.jpg|base64 -d >test2.jpg ``` #### 再次查看文件的类型 ```bash $ file test2.jpg ``` ``` test2.jpg: data ``` 文件base64解码之后还不是jpeg格式 #### 查看文件的十六进制编码 ```bash $ cat test2.jpg|xxd |more ``` ```bash 0000000: ff8d ff0e 0001 a464 9464 0010 1010 0006 .......d.d...... 0000010: 0006 0000 ffbd 0034 0070 5050 6050 4070 .......4.pPP`P@p 0000020: 6050 6080 7070 80a0 11b0 a090 90a0 51f0 `P`.pp........Q. 0000030: 01c0 1181 51a1 9181 5181 71b1 e172 12b1 ....Q...Q.q..r.. 0000040: d152 d171 8122 e222 5282 92b2 c2b2 a102 .R.q."."R....... 0000050: f233 f2a2 2372 a2b2 a2ff bd00 3410 7080 .3..#r......4.p. 0000060: 80a0 90a0 41b0 b041 a2c1 81c1 a2a2 a2a2 ....A..A........ 0000070: a2a2 a2a2 a2a2 a2a2 a2a2 a2a2 a2a2 a2a2 ................ 0000080: a2a2 a2a2 a2a2 a2a2 a2a2 a2a2 a2a2 a2a2 ................ 0000090: a2a2 a2a2 a2a2 a2a2 a2a2 a2a2 a2a2 ff0c ................ 00000a0: 0011 8010 0e20 0830 1022 0020 1110 3011 ..... .0.". ..0. 00000b0: 10ff 4c00 f100 0010 5010 1010 1010 1000 ..L.....P....... 00000c0: 0000 0000 0000 0010 2030 4050 6070 8090 ........ 0@P`p.. 00000d0: a0b0 ff4c 005b 0100 2010 3030 2040 3050 ...L.[.. .00 @0P 00000e0: 5040 4000 0010 d710 2030 0040 1150 2112 P@@..... 0.@.P!. 00000f0: 1314 6031 1516 7022 1741 2318 191a 8032 ..`1..p".A#....2 ``` #### 通过谷歌搜索ff8dff0e字符串显示 ![](https://files.ynotes.cn/ff8dff0e.png) 结果显示文件内容中的每一个字节,高4位和低4位调转了位置 #### 编辑python脚本实现转换 ```bash $ vim conv_jpg.py ``` ```python from __future__ import print_function import struct import ntpath import sys import os import base64 src_file=sys.argv[1] dest_dir=sys.argv[2] src_filename=ntpath.basename(src_file) desc_full_path=os.path.join(dest_dir,src_filename) f=open(src_file,'rb') f2=open(desc_full_path,'wb+') for bit in base64.b64decode(f.read()): #for bit in f.read(): c=ord(bit) c1=(c & 0xf0)>>4 c2=(c & 0x0f)<<4 c_n=c1|c2 b_c=struct.pack('B',c_n) f2.write(b_c) f.close() f2.close() ``` ```bash $ python conv_jpg.py test.jpg test_new.jpg ``` #### 再次查看文件的内容,显示文件格式为JPEG,说明解码成功! ```bash $ file test_new.jpg ``` ``` test_new.jpg: JPEG image data, JFIF standard 1.01 ```
阅读 882 评论 0 收藏 0
阅读 882
评论 0
收藏 0