图片,音频,视频的文件加密,解密,保护自身资源
2017-01-03 18:34
766 查看
private static String PASSWORD = "123456";
文件加密方法,支持MP3,jpg,png,wav.rmvb等所有图片,音频,视频格式
亲测可用
/**
*文件路径
*输出路径
*/
public static void unCode(String filePath, String cachePath) {
InputStream is = null;
OutputStream out = null;
try {
File file = new File(filePath);
if (!file.exists()) {
return;
}
File dest = new File(cachePath);
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
is = new FileInputStream(filePath);
out = new FileOutputStream(cachePath);
byte[] buffer = new byte[1024];
byte[] buffer2 = new byte[1024];
byte bMax = (byte) 255;
long size = file.length() - PASSWORD.length();
int mod = (int) (size % 1024);
int div = (int) (size >> 10);
int count = mod == 0 ? div : (div + 1);
int k = 1, r;
while ((k <= count && (r = is.read(buffer)) > 0)) {
if (mod != 0 && k == count) {
r = mod;
}
for (int i = 0; i < r; i++) {
byte b = buffer[i];
buffer2[i] = b == 0 ? bMax : --b;// 文件的每一个byte减1
}
out.write(buffer2, 0, r);
k++;
}
} catch (Exception e) {
} finally {
try {
if (out != null) {
out.close();
}
if (is != null) {
is.close();
}
} catch (Exception e2) {
}
}
}
====================机智的分割线==================================
文件解密方法,亲测可用
两个参数:1.文件路径
2.输出路径
public static void enCode(String filePath, String toPath) {
InputStream in = null;
OutputStream out = null;
try {
File file = new File(filePath);
if (!file.exists()) {
return;
}
in = new FileInputStream(filePath);
out = new FileOutputStream(toPath);
byte[] buffer = new byte[1024];
byte[] buffer2 = new byte[1024];
int r;
while ((r = in.read(buffer)) > 0) {
for (int i = 0; i < r; i++) {
byte b = buffer[i];
buffer2[i] = b == 255 ? 0 : ++b;// 文件的每一byte增1
}
out.write(buffer2, 0, r);
out.flush();
}
appendTail(toPath, PASSWORD);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (Exception e2) {
}
}
}
/**
* 在文件尾添加指定的字符串
*
* @param fileName
* @param content
*/
private static void appendTail(String fileName, String content) {
try {
RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
long fileLength = randomFile.length();
randomFile.seek(fileLength);
randomFile.writeBytes(content);
randomFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
文件加密方法,支持MP3,jpg,png,wav.rmvb等所有图片,音频,视频格式
亲测可用
/**
*文件路径
*输出路径
*/
public static void unCode(String filePath, String cachePath) {
InputStream is = null;
OutputStream out = null;
try {
File file = new File(filePath);
if (!file.exists()) {
return;
}
File dest = new File(cachePath);
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
is = new FileInputStream(filePath);
out = new FileOutputStream(cachePath);
byte[] buffer = new byte[1024];
byte[] buffer2 = new byte[1024];
byte bMax = (byte) 255;
long size = file.length() - PASSWORD.length();
int mod = (int) (size % 1024);
int div = (int) (size >> 10);
int count = mod == 0 ? div : (div + 1);
int k = 1, r;
while ((k <= count && (r = is.read(buffer)) > 0)) {
if (mod != 0 && k == count) {
r = mod;
}
for (int i = 0; i < r; i++) {
byte b = buffer[i];
buffer2[i] = b == 0 ? bMax : --b;// 文件的每一个byte减1
}
out.write(buffer2, 0, r);
k++;
}
} catch (Exception e) {
} finally {
try {
if (out != null) {
out.close();
}
if (is != null) {
is.close();
}
} catch (Exception e2) {
}
}
}
====================机智的分割线==================================
文件解密方法,亲测可用
两个参数:1.文件路径
2.输出路径
public static void enCode(String filePath, String toPath) {
InputStream in = null;
OutputStream out = null;
try {
File file = new File(filePath);
if (!file.exists()) {
return;
}
in = new FileInputStream(filePath);
out = new FileOutputStream(toPath);
byte[] buffer = new byte[1024];
byte[] buffer2 = new byte[1024];
int r;
while ((r = in.read(buffer)) > 0) {
for (int i = 0; i < r; i++) {
byte b = buffer[i];
buffer2[i] = b == 255 ? 0 : ++b;// 文件的每一byte增1
}
out.write(buffer2, 0, r);
out.flush();
}
appendTail(toPath, PASSWORD);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (Exception e2) {
}
}
}
/**
* 在文件尾添加指定的字符串
*
* @param fileName
* @param content
*/
private static void appendTail(String fileName, String content) {
try {
RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
long fileLength = randomFile.length();
randomFile.seek(fileLength);
randomFile.writeBytes(content);
randomFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
相关文章推荐
- Unity3d外部加载音频,视频,图片等资源 及根据路径获取制定格式的文件
- 常见的视频、音频、图片文件的格式
- 常用视频、音频、图片、字幕文件类型:
- 获取系统的图片,音频和视频文件
- 如何检测android上的多媒体文件属于音频、视频还是图片
- DRM-内容数据版权加密保护技术学习(上):视频文件打包实现
- Android开发之多媒体文件获取工具类实例【音频,视频,图片等】
- java 对视频和图片进行加密解密
- Android中使用又拍云存储来上传文件(包括图片、音频和视频等)
- 【Cocos2d-x 手游研发小技巧】图片资源加密,Lua文件加密
- 简单的对图片文件进行加密解密
- c# 通过修改图片对象的文件的字节 变相"加密""解密"图片在磁盘的存储
- Android简单加密保护自有图片资源
- bitmap的一些常用操作:读图片文件、图片缩放和旋转、将图片二进制转化为bitmap,以及图片加密,解密
- 【cocos2d-x 手游研发小技巧(7)图片资源加密,Lua文件加密】
- DRM-内容数据版权加密保护技术学习(上):视频文件打包实现(转)
- Android判断文件类型(视频、音频、图片等)
- 图片,音频,视频文件的上传 以及图片缩略图的制作,音频文件时长的获取以及 视频文件缩略图的获取
- 一键自动发布ipa(更新svn,拷贝资源,压缩资源,加密图片资源,加密数据文件,加密lua脚本,编译代码,ipa签名,上传ftp)
- 图片资源加密,Lua文件加密