android java加密_java和android文件加密小结

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 classMyCipher {public static final String VIPARA = "0102030405060708";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 voidmain(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, intcipherMode) {//创建Key gen

KeyGenerator keyGenerator = null;

Cipher cipher= null;try{

IvParameterSpec zeroIv= newIvParameterSpec(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();

}returncipher;

}/**

* 对文件进行AES加密

*

* @param key

* @param sourceFilePath

* @param destFilePath

* @return*/

public staticFile encryptFile(String key, String sourceFilePath, String destFilePath) {

System.out.printf(sourceFilePath);

FileInputStreamin = null;

FileOutputStreamout = null;

File destFile= null;

File sourceFile= null;try{

sourceFile= newFile(sourceFilePath);

destFile= newFile(destFilePath);if (sourceFile.exists() &&sourceFile.isFile()) {if (!destFile.getParentFile().exists()) {

destFile.getParentFile().mkdirs();

}

destFile.createNewFile();in = newFileInputStream(sourceFile);out = newFileOutputStream(destFile);

Cipher cipher=initAESCipher(key, Cipher.ENCRYPT_MODE);//以加密流写入文件

CipherInputStream cipherInputStream = new CipherInputStream(in, cipher);byte[] cache = new byte[1024];int nRead = 0;while ((nRead = cipherInputStream.read(cache)) != -1) {out.write(cache, 0, 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();

}

}returndestFile;

}/**

* AES方式解密文件

*

* @param key

* @param sourceFilePath

* @param destFilePath

* @return*/

public staticFile decryptFile(String key, String sourceFilePath, String destFilePath) {

FileInputStreamin = null;

FileOutputStreamout = null;

File destFile= null;

File sourceFile= null;try{

sourceFile= newFile(sourceFilePath);

destFile= newFile(destFilePath);if (sourceFile.exists() &&sourceFile.isFile()) {if (!destFile.getParentFile().exists()) {

destFile.getParentFile().mkdirs();

}

destFile.createNewFile();in = newFileInputStream(sourceFile);out = newFileOutputStream(destFile);

Cipher cipher=initAESCipher(key, Cipher.DECRYPT_MODE);

CipherOutputStream cipherOutputStream= new CipherOutputStream(out, cipher);byte[] buffer = new byte[1024];intr;while ((r = in.read(buffer)) >= 0) {

cipherOutputStream.write(buffer,0, 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();

}

}returndestFile;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值