python-base64编码与解码
base64编码原理:
例如:
实例一:
#-*- coding: UTF-8 -*- __author__ = ''
__date__ = '2015/12/23' import base64 code = "aGV5LOatpOWkhOWtmOWcqGpvb21sYea8j+a0nu+8jOivt+WPiuaXtuiBlOezuyB4eHh4eHhAMTI2LmNvbSDkv67lpI3mraTmvI/mtJ4=" print type(code)
cc = base64.decodestring(code)
print cc u = u'hey,此处存在joomla漏洞,请及时联系 xxxxxx@126.com 修复此漏洞'
print type(u)
d = u.encode('utf8')
print type(d) dd = base64.encodestring(d)
print dd
运行结果:
<type 'str'>
hey,此处存在joomla漏洞,请及时联系 xxxxxx@126.com 修复此漏洞
<type 'unicode'>
<type 'str'>
aGV5LOatpOWkhOWtmOWcqGpvb21sYea8j+a0nu+8jOivt+WPiuaXtuiBlOezuyB4eHh4eHhAMTI2LmNvbSDkv67lpI3mraTmvI/mtJ4=
实例二:
#-*- coding: UTF-8 -*- __author__ = ''
__date__ = '2016/2/15' import base64 #对字符串进行base64编码
def str2base64():
str_encode = raw_input("请输入字符串:")
en = base64.encodestring(str_encode)
print en
#对字符串进行base64解码
def base64tostr():
str_decode = raw_input("请输入base64串:")
de = base64.decodestring(str_decode)
print de
#对url字符串进行base64编码
def url2base64():
url_encode = raw_input("请输入URL字符串:")
en = base64.urlsafe_b64encode(url_encode)
print en
#对url字符串进行base64解码
def base64tourl():
url_decode = raw_input("请输入url的base64串:")
de = base64.urlsafe_b64decode(url_decode)
print de #对文件里面的字符串进行base64编码
def file_base64_en():
f1 = raw_input("请输入您要读取的文件:")
f2 = raw_input("请输入您要写入的文件:")
rf = open(f1,'r')
lines = rf.readlines()
wf = open(f2,'w')
for line in lines:
word = line.strip()
en = base64.encodestring(word)
#print en
wf.write(word+"的base64编码结果是:"+en)
#wf.write("\n")
rf.close()
wf.close()
#对文件里面的字符串进行base64解码
def file_base64_de():
f1 = raw_input("请输入您要读取的文件:")
f2 = raw_input("请输入您要写入的文件:")
rf = open(f1,'r')
lines = rf.readlines()
wf = open(f2,'w')
for line in lines:
de = base64.decodestring(line)
#print de
wf.write(line+"的base64解码结果是:"+de)
wf.write("\n")
rf.close()
wf.close() def main():
print u"a.字符串"
print u"b.url字符串"
print u"c.读取文件操作"
ch = raw_input("请选择操作数据类型:") if ch == "a":
print u"1.base64编码"
print u"2.base64解码" choice = raw_input("请选择编码或解码:")
if choice == "":
str2base64()
elif choice == "":
base64tostr()
else:
print u"您的选择不是合理的编码或解码!"
elif ch == "b":
print u"1.base64编码"
print u"2.base64解码" choice = raw_input("请选择编码或解码:")
if choice == "":
url2base64()
elif choice == "":
base64tourl()
else:
print u"您的选择不是合理的编码或解码!"
elif ch == "c":
print u"1.base64编码"
print u"2.base64解码" choice = raw_input("请选择编码或解码:")
if choice == "":
file_base64_en()
elif choice == "":
file_base64_de()
else:
print u"您的选择不是合理的编码或解码!"
else:
print u"未找到您所需要编码或解码的数据类型!" if __name__ == "__main__":
main()
python-base64编码与解码的更多相关文章
- python base64编码和解码图片
简介 在实际项目中,可能需要对图片进行大小的压缩,较为常见的方法则是将图片转换为base64的编码,本文就python编码和解码图片做出一定的介绍. 代码 import base64 import o ...
- Python中进行Base64编码和解码
Base64编码 广泛应用于MIME协议,作为电子邮件的传输编码,生成的编码可逆,后一两位可能有“=”,生成的编码都是ascii字符.优点:速度快,ascii字符,肉眼不可理解缺点:编码比较长,非常容 ...
- Python Base64 编码
0x00 Base64简介 0x01 常用场景举例 0x02 编.解码流程 0x03 Python中Base64编码与解码 0x00 Base64简介 我们知道在计算机中任何数据都是按ascii码存储 ...
- BASE64编码和解码(VC源代码) 并 内存加载 CImage 图像
BASE64可以用来将binary的字节序列数据编码成ASCII字符序列构成的文本.完整的BASE64定义可见 RFC1421和 RFC2045.编码后的数据比原始数据略长,为原来的4/3.在电子 ...
- base64编码、解码的C语言实现
转自:http://www.cnblogs.com/yejianfei/archive/2013/04/06/3002838.html base64是一种基于64个可打印字符来表示二进制数据的表示方法 ...
- android Java BASE64编码和解码二:图片的编码和解码
1.准备工作 (1)在项目中集成 Base64 代码,集成方法见第一篇博文:android Java BASE64编码和解码一:基础 (2)添加 ImgHelper 工具类 package com.a ...
- Javascript Base64编码与解码
原文:[转]Javascript Base64编码与解码 <html> <head> <META HTTP-EQUIV="MSThemeCompatible&q ...
- Python的编码和解码
Python的编码和解码 在不同的国家,存在不同的文字,由于现在的软件都要做到国际化通用,所以必须要有一种语言或编码方式,来实现各种编码的解码,然后重新编码. 在西方国家,没有汉字,只有英文,所以最开 ...
- C# base64编码、解码
public class TransferCode { #region base-64编码.解码 /// <summary> /// BASE64编码 /// </summary&g ...
- Url的Base64编码以及解码
Base64可以将二进制转码成可见字符方便进行http传输,但是base64转码时会生成“+”,“/”,“=”这些被URL进行转码的特殊字符,导致两方面数据不一致.我们可以在发送前将“+”,“/”,“ ...
随机推荐
- codeforces #330 div2
A: #include<cstdio> #include<algorithm> #include<cmath> #include<map> #inclu ...
- #ifndef #define #endif 防止头文件被重复引用
想必很多人都看过“头文件中的 #ifndef/#define/#endif 防止该头文件被重复引用”.但是是否能理解“被重复引用”是什么意思?是不能在不同的两个文件中使用include来包含这个头文件 ...
- drupal7创始人root忘记密码的解决办法
在index.php中的drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);之后加入 require_once 'includes/password.inc'; echo ...
- MVC4 Win2008 aspnet_isapi配置问题
win2008下配置共2步 1.先确保你的应该程序池选择了asp.net 4.0框架 2.添加脚本映射(解决URL不能重写) 3.添加通配符脚本映射(解决图片不能显示) IIS中的Web应用程序有两种 ...
- IEnumerable
C#基础之IEnumerable 1.IEnumerable的作用 在使用Linq查询数据时经常以IEnumerable<T>来作为数据查询返回对象,在使用foreach进行遍历时需要该对 ...
- MYSQL insert
准备: create table T4(X int ,Y int); 方法 1. insert [low_priority][high_priority][delayed] into table_na ...
- HeadFirst设计模式读书笔记(3)-装饰者模式(Decorator Pattern)
装饰者模式:动态地将责任附件到对象上.若要扩展功能,装饰者提东了比继承更有弹性的替代方案. 装饰者和被装饰对象有相同的超类型 你可以用一个或者多个装饰者包装一个对象. 既然装饰者和被装饰对象有相同的超 ...
- nginx grok 正则错误的输出情况
nginx 配置: http { include mime.types; default_type application/octet-stream; log_format main '$http_h ...
- JIRA官方:JIRA源代码集成
防火墙后的Git 使用Atlassian Stash创建和管理Git存储库,设置细粒度的权限并在代码上协作.这一切—安全.快速.可靠,更重要的是,可以部署在防火墙后面.JIRA问题关键字自动将JIRA ...
- https://pta.patest.cn/pta/test/15/exam/3/question/724
List Reverse( List L ){ if(L==NULL) return; List head=(List)malloc(sizeof(struct Node)); head->Ne ...