最近遇到一个文件加密的问题,自己读写的,安全性虽然还可以,但是速度慢,影响体验。

  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文件加密小结的更多相关文章

  1. Java 或者android 的加密技术

    可以将Java文件编译之后得到的class文件(字节码)进行加密. 然后自定义一个classloader-类加载器,在载入class文件之后,对它进行解密,然后就可以正常运行了. 猜测,android ...

  2. java 和 Android Base64加密

    Java8 Base64 Java 8 新特性 在Java 8中,Base64编码已经成为Java类库的标准. Java 8 内置了 Base64 编码的编码器和解码器. Base64工具类提供了一套 ...

  3. java实现MD5文件加密

    package me.zhengjie.modules.logdump.util; import java.io.FileInputStream; import java.io.IOException ...

  4. Java和Android文件操作

    File这是文件基类,抽象地代表一个文件实体,它有四个不同的构造方法: File(File dir, String name)  File(String path)   File(String dir ...

  5. 用poi-3.6-20091214.jar 实现java给excel资料加密

    用poi-3.6-20091214.jar 实现java给excel文件加密我用了网上的很多方法,但是都没有成功! HSSFWorkbook wb = new HSSFWorkbook(new Fil ...

  6. IOS, Android, Java Web Rest : RSA 加密和解密问题

    IOS, Android, Java Web Rest :  RSA 加密和解密问题 一对公钥私钥可以使用 OpenSSL创建, 通常 1024位长度够了. 注意: 1. 公钥私钥是BASE64编码的 ...

  7. Android之zip文件加密解压及进度条的实现

    zip文件的解压能够使用java的zip库,可是没有实现对加密文件的解压功能,这里能够使用zip4j来实现.详细能够參看该文<Android下zip压缩文件加密解密的完美解决方式>.该文件 ...

  8. Atitit.视频文件加密的方法大的总结 java c# php

    Atitit.视频文件加密的方法大的总结 java c# php 1. 加密的算法  aes  3des  des xor等.1 2. 性能1 3. 解密1 4. 播放器的事件扩展1 5. 自定义格式 ...

  9. Java代码加密与反编译(二):用加密算法DES修改classLoader实现对.class文件加密

    Java代码加密与反编译(二):用加密算法DES修改classLoader实现对.class文件加密 二.利用加密算法DES实现java代码加密 传统的C/C++自动带有保护机制,但java不同,只要 ...

随机推荐

  1. 关于Eclipse安装Scala插件不显示

    关于Eclipse安装Scala插件不显示, 改变java版本仍然不能使用, 办法还是有的:下载Eclipse Scala版本 解压使用 下载在这里:http://scala-ide.org/down ...

  2. Ruby. Vs . Python

    前言:从语言的本质上来分析,我对Ruby持反对态度,毕竟语言是为了交流,在表达的效率层面为了正确性必须适当放弃复杂性.且有句老话说的好,Ruby In Rails 才是语言,而Ruby只是这个语言的工 ...

  3. 【技术累积】【点】【java】【6】时间戳

    闲聊 加班多诶,写博客诶. 基本 时间戳,直观理解就是时间上面盖个戳罢了,在时间这个轴上面记录个点: unix时间戳表示从开始的时间点开始,经过了多少秒: 可以简单的看做是一个计时器: 基本定义可以直 ...

  4. C#的split函数分割

    C#的split函数分割 string str = textBox1.Text; string[] strlist = str.Split("\r\n".ToCharArray() ...

  5. VGG 19

    关于VGG19的一些参考资料 http://www.cnblogs.com/vipyoumay/archive/2017/11/23/7884472.html https://cloud.tencen ...

  6. Centos 搭建activemq

    Centos 搭建activemq 1,官方下载  http://activemq.apache.org/activemq-5122-release.html apache-activemq-5.15 ...

  7. 训练1-E

    有二个整数,它们加起来等于某个整数,乘起来又等于另一个整数,它们到底是真还是假,也就是这种整数到底存不存在,实在有点吃不准,你能快速回答吗?看来只能通过编程. 例如: x + y = 9,x * y ...

  8. j2ee消息中间件

    http://blog.csdn.net/apanious/article/details/51014396

  9. TensorFlow实现LeNet5模型

    # -*- coding: utf-8 -*-import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_ ...

  10. 2、深入学习基本结构——CNN

    这节课主要简单复习一下CNN 从图中例子,1.3共享参数,2.4共享. 要看明白以上参数. 后面就是举例了. 比如声音信号 下面是zero padding 下面是pooling 还可以有mass po ...