java和android文件加密小结
最近遇到一个文件加密的问题,自己读写的,安全性虽然还可以,但是速度慢,影响体验。
Cipher虽然速度相当快,但是android和java有某些api存在不兼容;
问题解决:
方法引用自:https://www.jianshu.com/p/2aa5e1a1df1a
还是使用cipher,算法中用到了一个填充向量,即VIPARA, 这个向量的取值是不固定的,但是加密秘钥必须为16个字节,譬如“abcdefghabcdefgh”
package com.example; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException; import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec; public class MyCipher { public static final String VIPARA = "";
private static final String _FILE_NAME_OF_SECRET_ = "file-secret";
private static final String _FILE_LOC_FILE_NAME_ = "file-local"; private static final String _SOURCE_FILE_PATH_ = "path" + File.separator + _FILE_LOC_FILE_NAME_; private static final String _OUTPUT_FILE_PATH_ = "path" + File.separator + _FILE_NAME_OF_SECRET_; public static void main(String[] args) {
encryptFile(_FILE_NAME_OF_SECRET_, _SOURCE_FILE_PATH_, _OUTPUT_FILE_PATH_);
} /**
* 初始化 AES Cipher
*
* @param sKey
* @param cipherMode
* @return
*/
private static Cipher initAESCipher(String sKey, int cipherMode) {
//创建Key gen
KeyGenerator keyGenerator = null;
Cipher cipher = null;
try {
IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes());
SecretKeySpec key = new SecretKeySpec(sKey.getBytes(), "AES");
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(cipherMode, key, zeroIv);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
return cipher;
} /**
* 对文件进行AES加密
*
* @param key
* @param sourceFilePath
* @param destFilePath
* @return
*/
public static File encryptFile(String key, String sourceFilePath, String destFilePath) {
System.out.printf(sourceFilePath);
FileInputStream in = null;
FileOutputStream out = null;
File destFile = null;
File sourceFile = null;
try {
sourceFile = new File(sourceFilePath); destFile = new File(destFilePath);
if (sourceFile.exists() && sourceFile.isFile()) {
if (!destFile.getParentFile().exists()) {
destFile.getParentFile().mkdirs();
}
destFile.createNewFile();
in = new FileInputStream(sourceFile);
out = new FileOutputStream(destFile);
Cipher cipher = initAESCipher(key, Cipher.ENCRYPT_MODE);
//以加密流写入文件
CipherInputStream cipherInputStream = new CipherInputStream(in, cipher);
byte[] cache = new byte[];
int nRead = ;
while ((nRead = cipherInputStream.read(cache)) != -) {
out.write(cache, , nRead);
out.flush();
}
cipherInputStream.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return destFile;
} /**
* AES方式解密文件
*
* @param key
* @param sourceFilePath
* @param destFilePath
* @return
*/
public static File decryptFile(String key, String sourceFilePath, String destFilePath) {
FileInputStream in = null;
FileOutputStream out = null;
File destFile = null;
File sourceFile = null;
try {
sourceFile = new File(sourceFilePath);
destFile = new File(destFilePath);
if (sourceFile.exists() && sourceFile.isFile()) {
if (!destFile.getParentFile().exists()) {
destFile.getParentFile().mkdirs();
}
destFile.createNewFile();
in = new FileInputStream(sourceFile);
out = new FileOutputStream(destFile);
Cipher cipher = initAESCipher(key, Cipher.DECRYPT_MODE);
CipherOutputStream cipherOutputStream = new CipherOutputStream(out, cipher);
byte[] buffer = new byte[];
int r;
while ((r = in.read(buffer)) >= ) {
cipherOutputStream.write(buffer, , r);
}
cipherOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return destFile;
}
}
java和android文件加密小结的更多相关文章
- Java 或者android 的加密技术
可以将Java文件编译之后得到的class文件(字节码)进行加密. 然后自定义一个classloader-类加载器,在载入class文件之后,对它进行解密,然后就可以正常运行了. 猜测,android ...
- java 和 Android Base64加密
Java8 Base64 Java 8 新特性 在Java 8中,Base64编码已经成为Java类库的标准. Java 8 内置了 Base64 编码的编码器和解码器. Base64工具类提供了一套 ...
- java实现MD5文件加密
package me.zhengjie.modules.logdump.util; import java.io.FileInputStream; import java.io.IOException ...
- Java和Android文件操作
File这是文件基类,抽象地代表一个文件实体,它有四个不同的构造方法: File(File dir, String name) File(String path) File(String dir ...
- 用poi-3.6-20091214.jar 实现java给excel资料加密
用poi-3.6-20091214.jar 实现java给excel文件加密我用了网上的很多方法,但是都没有成功! HSSFWorkbook wb = new HSSFWorkbook(new Fil ...
- IOS, Android, Java Web Rest : RSA 加密和解密问题
IOS, Android, Java Web Rest : RSA 加密和解密问题 一对公钥私钥可以使用 OpenSSL创建, 通常 1024位长度够了. 注意: 1. 公钥私钥是BASE64编码的 ...
- Android之zip文件加密解压及进度条的实现
zip文件的解压能够使用java的zip库,可是没有实现对加密文件的解压功能,这里能够使用zip4j来实现.详细能够參看该文<Android下zip压缩文件加密解密的完美解决方式>.该文件 ...
- Atitit.视频文件加密的方法大的总结 java c# php
Atitit.视频文件加密的方法大的总结 java c# php 1. 加密的算法 aes 3des des xor等.1 2. 性能1 3. 解密1 4. 播放器的事件扩展1 5. 自定义格式 ...
- Java代码加密与反编译(二):用加密算法DES修改classLoader实现对.class文件加密
Java代码加密与反编译(二):用加密算法DES修改classLoader实现对.class文件加密 二.利用加密算法DES实现java代码加密 传统的C/C++自动带有保护机制,但java不同,只要 ...
随机推荐
- ES : 软件工程学的复杂度理论及物理学解释
系统论里面总是有一些通用的专业术语 比如复杂度.熵.焓,复杂度专门独立出来,成为复杂度理论 文章摘抄于:<非线性动力学> 刘秉政 编著 5.5 复杂性及其测度 热力学的几个专业术语 熵. ...
- 创建一个dynamics CRM workflow (二) - Build in Workflows
这里我们不着重讲解build in workflow. 但是, 如果要上手custom workflow, 我们必须要了解 build in workflow. build-in workflow 在 ...
- jQuery 插入元素
在被选元素的内部 prepend() 方法在被选元素的开头(仍位于内部)插入指定内容. append() 方法在被选元素的结尾(仍然在内部)插入指定内容.
- javase 异常处理
1.简述什么是异常.异常的继承体系? 异常就是java代码块在运行时出现的错误,有编译错误和运行错误, Throwable是所有异常的父类它包含了error和Exception两个子类. 其中e ...
- node——通过express模拟Apache实现静态资源托管
1.express中处理静态资源的函数 创建一个app.js作为入口文件,创建一个public文件夹作为静态资源文件夹 var app=express();var fn=express.static( ...
- python中的全局变量、局部变量、实例变量
1.全局变量:在模块内,在所有函数.类外面. 2.局部变量:在函数内,在类方法内(未加self修饰的) 3.静态变量:在类内,但不在类方法内.[共同类所有,值改变后,之后所有的实例对象也改变] 4.实 ...
- [USACO4.2] 草地排水 Drainage Ditches (最大流)
题目背景 在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫约翰修建了一套排水系统来使贝茜的草地免除被大水淹没 ...
- FFMpeg 常用命令格式转换,视频合成
FFmpeg都是命令行的,用起来肯定不方便.但是,这对技术宅应该不成问题.下面,我就罗列一些比较实用的使用方法吧. FFmpeg的下载与安装 FFmpeg是开源的.但我们不必去下载它的源代码.下载已经 ...
- Jdk和Cglib 的区别
一.原理区别: java动态代理是利用反射机制生成一个实现代理接口的代理类,在调用具体方法前调用InvokeHandler来处理. 而cglib动态代理是利用asm开源包,对代理对象类的class文件 ...
- js 现给数字加三位一逗号间隔的种方法
方法一:(ie下有问题) <script type= "text/javascript"> var num_s = "1232134456.546 " ...