[Android]网络数据的简单加密
来源:互联网 发布:js同源是什么意思 编辑:程序博客网 时间:2024/05/21 01:56
原理:
最简单的数据加密就是采用Base64,虽然会带有数据冗余,但是写法简单,不用做过多的修改,其实也可以采用异或加密。
代码:
这里就直接上代码吧,写法很简单。
public static final boolean DownlaodAppFile(Context mContext, String url, String cacheName, boolean replace) {File root = mContext.getExternalCacheDir();if (!root.exists()) {root.mkdir();}String root_path = root.getAbsolutePath();File cacheFile = new File(root_path + "/" + cacheName);if (cacheFile.exists()) {if (!replace) {return true;}}File tmpFIle = new File(root_path + "/" + cacheName + ".tmp");URL mURL = null;try {mURL = new URL(url);} catch (MalformedURLException e) {Log.i("Finals", "URL error");e.printStackTrace();return false;}HttpURLConnection conn = null;FileOutputStream fos = null;Base64OutputStream bos = null;try {fos = new FileOutputStream(tmpFIle);bos = new Base64OutputStream(fos, 45);conn = (HttpURLConnection) mURL.openConnection();if (conn.getResponseCode() == 200) {// 创建连接InputStream is = conn.getInputStream();byte[] buffer = new byte[1024];// 循环获取数据int len = 0;while ((len = is.read(buffer)) != -1) {bos.write(buffer, 0, len);}// 释放资源bos.close();fos.close();is.close();conn.disconnect();bos = null;fos = null;is = null;conn = null;tmpFIle.renameTo(cacheFile);System.out.println("下载完成");}} catch (IOException e) {e.printStackTrace();Log.i("Finals", "Url connection error");return false;} finally {if (fos != null) {try {fos.close();} catch (IOException e) {e.printStackTrace();}}}return true;}public static final String ReadAppFile(Context context, String cacheName) {File root = context.getExternalCacheDir();String rootpath = root.getAbsolutePath();File cacheFile = new File(rootpath + "/" + cacheName);if (!cacheFile.exists()) {return "";}String result = "";try {FileInputStream fis = new FileInputStream(cacheFile);Base64InputStream bis = new Base64InputStream(fis, 45);ByteArrayOutputStream bos = new ByteArrayOutputStream();byte[] buffer = new byte[512];int len = 0;while ((len = bis.read(buffer)) != -1) {bos.write(buffer, 0, len);}result = bos.toString();bos.close();bis.close();fis.close();bos = null;bis = null;fis = null;} catch (IOException e) {e.printStackTrace();return "";}return result;}
0 0
- [Android]网络数据的简单加密
- 一个简单的Android Native(C/C++)网络数据加密方案
- Android网络数据加密传输
- 网络数据加密的三种技术
- 网络数据加密的三种技术
- 网络数据加密的三种技术
- 网络传输数据的加密和编码
- Android自定义网络数据加载等待框的简单封装
- 数据加密与解密的简单原理
- 简单的数据加密与解密
- Base64-不重要数据的简单加密
- 网络数据加密技术
- 加密网络数据
- 加密网络数据
- 加密网络数据
- 网络数据加密理论
- Android本地数据安全问题,常用加密简单汇总,网上搜集的资料,方便查看
- 网络数据加密, 数据安全
- 设置Table Cell的背景图的类
- 黑马程序员——JAVA 数组
- LGame学习笔记1-----LGame游戏引擎大体架构
- C++第十题20150327
- p51 8
- [Android]网络数据的简单加密
- 性能优化:使用SparseArray代替HashMap<Integer,Object>
- 如何将C#/.NET 将json字符串格式数据转换成对象
- OJ 系列之去重复字符串并排序
- 关于ViewGroup
- 编译 googleBreakpad
- JAVA数据源连接方式汇总
- C++ STL模板与容器 知识 初学 小结 ( 三 )
- java 中的 final