Base64的加密与解密
2015-12-20 21:48
344 查看
最近在项目中关于客户端文件上传问题,原本我webservice接口这边提供的是byte[]类型,确不知道ios客户端没这类型,只能传用Base64加密后的字符串给我.....因为学习的少,不知道这东东是怎么回事,于是找了很多资料,终于把问题解决了。
Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,大家可以查看RFC2045~RFC2049,上面有MIME的详细规范。Base64编码可用于在HTTP环境下传递较长的标识信息。例如,在Java Persistence系统Hibernate中,就采用了Base64来将一个较长的唯一标识符(一般为128-bit的UUID)编码为一个字符串,用作HTTP表单和HTTP GET URL中的参数。在其他应用程序中,也常常需要把二进制数据编码为适合放在URL(包括隐藏表单域)中的形式。此时,采用Base64编码不仅比较简短,同时也具有不可读性,即所编码的数据不会被人用肉眼所直接看到......
示例代码:
String类型 进行Base64编码
String encoded = Base64.encode("Hello, world!");
String类型 进行Base64解码
String decoded = Base64.decode(encoded);
指定字符编码方式
String encoded = Base64.encode("Hello, world!", "UTF-8");
String decoded = Base64.decode(encoded, "UTF-8");
对文件进行编码:
如果文件比较小,可以通过以下方式,直接读取到内存中进行编码处理
byte[] source = ...; // load your data here
byte[] encoded = Base64.encode(source);
byte[] decoded = Base64.decode(encoded);
如果大件比较大,则建议使用stream:
代码示例Base64编码:
InputStream inputStream = new FileInputStream("source.jpg");
OutputStream outputStream = new FileOutputStream("encoded.b64");
Base64.encode(inputStream, outputStream);
outputStream.close();
inputStream.close();
代码示例Base64解码:
InputStream inputStream = new FileInputStream("encoded.b64");
OutputStream outputStream = new FileOutputStream("decoded.jpg");
Base64.decode(inputStream, outputStream);
outputStream.close();
inputStream.close();
Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,大家可以查看RFC2045~RFC2049,上面有MIME的详细规范。Base64编码可用于在HTTP环境下传递较长的标识信息。例如,在Java Persistence系统Hibernate中,就采用了Base64来将一个较长的唯一标识符(一般为128-bit的UUID)编码为一个字符串,用作HTTP表单和HTTP GET URL中的参数。在其他应用程序中,也常常需要把二进制数据编码为适合放在URL(包括隐藏表单域)中的形式。此时,采用Base64编码不仅比较简短,同时也具有不可读性,即所编码的数据不会被人用肉眼所直接看到......
示例代码:
String类型 进行Base64编码
String encoded = Base64.encode("Hello, world!");
String类型 进行Base64解码
String decoded = Base64.decode(encoded);
指定字符编码方式
String encoded = Base64.encode("Hello, world!", "UTF-8");
String decoded = Base64.decode(encoded, "UTF-8");
对文件进行编码:
如果文件比较小,可以通过以下方式,直接读取到内存中进行编码处理
byte[] source = ...; // load your data here
byte[] encoded = Base64.encode(source);
byte[] decoded = Base64.decode(encoded);
如果大件比较大,则建议使用stream:
代码示例Base64编码:
InputStream inputStream = new FileInputStream("source.jpg");
OutputStream outputStream = new FileOutputStream("encoded.b64");
Base64.encode(inputStream, outputStream);
outputStream.close();
inputStream.close();
代码示例Base64解码:
InputStream inputStream = new FileInputStream("encoded.b64");
OutputStream outputStream = new FileOutputStream("decoded.jpg");
Base64.decode(inputStream, outputStream);
outputStream.close();
inputStream.close();
相关文章推荐
- JS实现密码加密
- jsp页面清除缓存
- jsp页面清除缓存
- Assigning to 'AppDelegate *' from incompatible type 'id<UIApplicationDelegate>'
- hdoj2033人见人爱A+B
- mysql获取表或字段的注释
- 好听的中文歌曲整理
- 再撸一次简单的NODE.JS
- iOS开发中的Self-Manager 模式
- 【ITOO】工作流
- UVA 题目11089 Fi-binary Number(找规律)
- resizableImageWithCapInsets:resizingMode 图片拉伸 使用方法
- 部署nginx+django
- 李笑来对《把时间当朋友》的高度概括
- StackOverflowError&OutOfMemoryError区别
- StackOverflowError&OutOfMemoryError区别
- 【CUDA】三、存储器层级结构及共享存储器
- StackOverflowError&OutOfMemoryError区别
- leetcode Combination Sum python
- React-Flux 介绍及实例演示