# -*- coding: utf-8 -*-
#@File name : binbase64.py
#@Auther : GhostFromHeaven[csdn]
#@date : 2012-07-19
from filehelper import *
#For more information about Base64 please read: http://zh.wikipedia.org/wiki/Base64
_CODE_CHAR = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
def binbase64(data):
"""
Convert binary data to Base64 format string.
"""
base64str = ""
for i in range(len(data)/3):
datavalue = ((data[3*i] << 16) | (data[3*i+1] << 8) | data[3*i+2])
for j in range(4):
base64str += _CODE_CHAR[(datavalue >> 6*(3-j)) & 0x3F]
dataremain = len(data) % 3
if dataremain == 1:
datavalue = data[-1] << 16;
base64str += _CODE_CHAR[(datavalue >> 18) & 0x3F]
base64str += _CODE_CHAR[(datavalue >> 12) & 0x3F]
base64str += "=="
elif dataremain == 2:
datavalue = (data[-2] << 16) | (data[-1] << 8);
base64str += _CODE_CHAR[(datavalue >> 18) & 0x3F]
base64str += _CODE_CHAR[(datavalue >> 12) & 0x3F]
base64str += _CODE_CHAR[(datavalue >> 6) & 0x3F]
base64str += "="
return base64str
def strbase64(astr):
"""
Convert a string to Base64 format string.
"""
return binbase64(map(ord, astr))
class Base64Error(Exception):
"""
Exception for Base64 error.
"""
pass
def base64bin(encodedstr):
"""
Convert Base64 format string to binary data.
"""
if len(encodedstr) % 4:
raise Base64Error("The length of input 'base64str' MUST be multiple of 4.")
rawbase64str = encodedstr.rstrip("=")
if (len(rawbase64str) % 4) == 1:
raise Base64Error("Too many '=' characters, MUST NOT be more than 2.")
for x in rawbase64str:
if x not in _CODE_CHAR:
raise Base64Error("Unexpected character %s.", x)
data=[]
for i in range(len(rawbase64str)/4):
datavalue = (_CODE_CHAR.find(rawbase64str[4*i]) << 18) \
| (_CODE_CHAR.find(rawbase64str[4*i+1]) << 12) \
| (_CODE_CHAR.find(rawbase64str[4*i+2]) << 6) \
| (_CODE_CHAR.find(rawbase64str[4*i+3]))
data.append((datavalue >> 16) & 0xFF)
data.append((datavalue >> 8) & 0xFF)
data.append((datavalue) & 0xFF)
strremain = len(rawbase64str) % 4
if strremain == 2:
datavalue = (_CODE_CHAR.find(rawbase64str[-2]) << 18) \
| (_CODE_CHAR.find(rawbase64str[-1]) << 12)
data.append((datavalue >> 16) & 0xFF)
elif strremain == 3:
datavalue = (_CODE_CHAR.find(rawbase64str[-3]) << 18) \
| (_CODE_CHAR.find(rawbase64str[-2]) << 12) \
| (_CODE_CHAR.find(rawbase64str[-1]) << 6)
data.append((datavalue >> 16) & 0xFF)
data.append((datavalue >> 8) & 0xFF)
return data
def base64str(encodedstr):
"""
Convert Base64 format string to a string.
"""
return "".join(map(chr,base64bin(encodedstr)))
def binfiletobase64(inp, out):
"""
Convert binary file to Base64 format text file.
"""
blocksize = 76 / 4 * 3
def _binfiletobase64(fin, fout):
while True:
chunk = fin.read(blocksize)
if chunk:
fout.write(strbase64(chunk))
fout.write("\n")
else:
break
fileinoutpattern(inp, out, _binfiletobase64, inmode="rb", outmode="w")
def base64filetobin(inp, out):
"""
Convert Base64 format text file to binary file.
"""
def _base64filetobin(fin, fout):
for line in fin:
fout.write(base64str(line.rstrip()))
fileinoutpattern(inp, out, _base64filetobin, inmode="r", outmode="wb")
def test():
"""
Self testing.
"""
rawstr = "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure."
encodedstr = binbase64(map(ord, rawstr))
for x in range(0, len(encodedstr), 76):
print encodedstr[x:x+76]
encodedstr = strbase64(rawstr)
for x in range(0, len(encodedstr), 76):
print encodedstr[x:x+76]
data = base64bin(encodedstr)
decodedstr = "".join(map(chr,data))
print decodedstr
assert decodedstr == rawstr
decodedstr = base64str(encodedstr)
print decodedstr
assert decodedstr == rawstr
#base64str(encodedstr[:-1]);#Not multiple of 4
#base64str(encodedstr[:-3]+"==");#Too many '='
#base64str(encodedstr[:-2]+"()");#Invaild characters '(' and ')'
binfiletobase64(r"D:\2.zip", r"D:\2.txt")
base64filetobin(r"D:\2.txt", r"D:\2.1.zip")
print "OK"
if __name__ == "__main__":
test()
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
binbase64[GhostFromHeaven].zip (3个子文件)
binbase64[GhostFromHeaven]
2.zip 3KB
binbase64.py 4KB
filehelper.py 2KB
共 3 条
- 1
资源评论
GhostFromHeaven
- 粉丝: 39
- 资源: 27
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 使用 C 语言实现的计算非负整数的阶乘
- 2011-2021最新版本北京大学数字普惠金融指数(PKU-DFIIC).xlsx
- 县域数字乡村指数2018-2020(1).xlsx
- Docker容器配置进阶
- tensorflow-gpu-2.7.4-cp37-cp37m-manylinux2010-x86-64.whl
- 多段线、 圆、弧转多段线(仅我可见)
- tensorflow-2.7.2-cp38-cp38-manylinux2010-x86-64.whl
- 李慧琴C语言基础部分.zip
- yeyue-p8Yi4-ve4a83792.apk
- tensorflow-gpu-2.7.3-cp38-cp38-manylinux2010-x86-64.whl
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功