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不同,只要 ...
随机推荐
- Spring依赖注入:@Autowired,@Resource和@Inject区别与实现原理
一.spring依赖注入使用方式 @Autowired是spring框架提供的实现依赖注入的注解,主要支持在set方法,field,构造函数中完成bean注入,注入方式为通过类型查找bean,即byT ...
- curl及wget使用说明
Date: 2019-06-19 Author: Sun 1. curl的使用 注意:安装的时候可能会遇到报错,有可能是openssl没装, apt install curl apt install ...
- BZOJ1150 [CTSC2007] 数据备份Backup 贪心_堆_神题
Description 你在一家 IT 公司为大型写字楼或办公楼(offices)的计算机数据做备份.然而数据备份的工作是枯燥乏味 的,因此你想设计一个系统让不同的办公楼彼此之间互相备份,而你则坐在家 ...
- Matrix Matcher UVA - 11019AC_自动机 + 代价提前计算
Code: #include<cstdio> #include<cstring> #include<algorithm> #include<vector> ...
- js判断当前移动设备平台
//js判断当前移动设备平台 var isiOs = false; var isAndroid = false; var isWindowsPhone = false; if(/(iPhone|iPa ...
- Java包名称中通配符的含义
"com.abc 表示的意义为:系统从com.abc这个包及其子孙包扫描组件 "com.abc.* 表示的意义为:系统从com.abc这个包的子孙包扫描组件
- MAVEN 构建包的引用
1.什么叫构建包的引用? 当你存在两个maven项目分别是项目A,项目B时,且项目B要引用项目A的方法,那么你就用把项目A打成*.jar架包,放到本地的Maven仓库提供给项目B去引用. A.用命令到 ...
- Mybatis拦截器执行过程解析
上一篇文章 Mybatis拦截器之数据加密解密 介绍了 Mybatis 拦截器的简单使用,这篇文章将透彻的分析 Mybatis 是怎样发现拦截器以及调用拦截器的 intercept 方法的 小伙伴先按 ...
- 【Manthan, Codefest 18 (rated, Div. 1 + Div. 2) A】Packets
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 多重背包的二进制优化. 就是将数量x分成接近log2x份 然后这log2x份能组合成1..x内的所有数字. 从而将多重背包转化成01 ...
- JavaScript 事件对内存和性能的影响
程序代码: <%-- Created by IntelliJ IDEA. User: 乔克叔叔 Date: 2017/12/26 Time: 16:45 To change this templ ...