码迷,mamicode.com
首页 > 移动开发 > 详细

android 文件简单的自定义加密和解密

时间:2014-09-23 19:12:55      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:android   io   os   ar   for   文件   on   c   代码   

在android或其他项目中常常会下载和上传文件,为了这些文件的安全我们与服务器统一加密的key,即可进行加密解密文件.

代码:

/**
* 文件file进行加密解密
*
* @param fileUrl
* 文件路径
* @param key
* 密码
* @throws Exception
*/
public static boolean decryptOrEncrypt(String fileUrl) {
try {
File file = new File(fileUrl);
if (!file.exists()) {
return false;
}
FileInputStream fileForInput = new FileInputStream(file);
byte[] bytes = new byte[fileForInput.available()];
fileForInput.read(bytes);
String strEn = CommonConfig.DECRYPT_KEY;
int nKeyLen = strEn.length();
int nIndex = 0;
FileOutputStream out = new FileOutputStream(fileUrl);
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) (bytes[i] ^ strEn.getBytes()[nIndex]);
nIndex++;
if (nIndex >= nKeyLen) {
nIndex = 0;
}
}
out.write(bytes, 0, bytes.length);
out.flush();
fileForInput.close();
out.close();

} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}

android 文件简单的自定义加密和解密

标签:android   io   os   ar   for   文件   on   c   代码   

原文地址:http://www.cnblogs.com/chentingcai/p/3988917.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!