Android简单加密网络数据
发布时间:2020-05-25 15:26:25 所属栏目:Java 来源:互联网
导读:Android简单加密网络数据
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 原理:最简单的数据加密就是采用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,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,len);
}
result = bos.toString();
bos.close();
bis.close();
fis.close();
bos = null;
bis = null;
fis = null;
} catch (IOException e) {
e.printStackTrace();
return "";
}
return result;
}
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
