package com.example.aspectjandroidtest;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import org.apache.http.util.EncodingUtils;

import com.facebook.crypto.Crypto;
import com.facebook.crypto.Entity;
import com.facebook.crypto.keychain.SharedPrefsBackedKeyChain;
import com.facebook.crypto.util.SystemNativeCryptoLibrary;

public aspect FileAspectJ {

	private boolean isEncryption = true;
	//使用秘钥链和原生库的默认实现,来创建一个新的加密对象
	Crypto crypto = new Crypto(
			new SharedPrefsBackedKeyChain(MainActivity.context),
			new SystemNativeCryptoLibrary());

	//创建应用文件的切点集合
	pointcut openFileOutput(String filename,int mode) : !within(FileAspectJ) && args(filename,mode) && call(* openFileOutput(..));
	after (String filename,int mode) returning : openFileOutput(filename, mode){
		//System.out.println("fx Aspectj openFile is start");
		byte[] buffer;
		try {
			System.out.println("fx Aspectj openFileName"+filename);
			buffer = filename.getBytes("UTF8");
			FileOutputStream fileOutputStream = null;
			try {
				//记录本应用加密过的文件
				fileOutputStream = MainActivity.context.openFileOutput("fileList",
						MainActivity.context.MODE_APPEND);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				fileOutputStream.write(buffer);
				fileOutputStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	//读取应用文件的切点集合
		pointcut openFileInput(String filename) : !within(FileAspectJ) && args(filename) && call(* openFileInput(..));
		before(String filename) : openFileInput(filename){

			String result = "";
			try {
				FileInputStream fileInputStream = MainActivity.context.openFileInput("fileList");
				int bufferSize = 0;
				try {
					bufferSize = fileInputStream.available();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} // 取得输入流的字节长度
				byte buffer[] = new byte[bufferSize];
				try {
					fileInputStream.read(buffer);
					fileInputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				result = EncodingUtils.getString(buffer, "UTF-8");
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//判断文件是否加密过
			if(result.indexOf(filename) == -1){
				isEncryption = false;//未加密
				System.out.println("fx 文件未加密");
			}else{
				isEncryption = true;//已加密
				System.out.println("fx 文件已加密");
			}
		}

		//截获到File的new操作
		pointcut filePointcut(String pathname ) :  !within(FileAspectJ) && args(pathname) && call(java.io.File.new(..));
		before(String pathname ) : filePointcut(pathname) {
			System.out.println("fx pathname is " + pathname);
		}

	//写文件切点的集合
	pointcut writePointcut(FileOutputStream fileStream, byte[] buffer) : !within(FileAspectJ) && target(fileStream)&& args(buffer) && call(* write(..));
	void around(FileOutputStream fileStream, byte[] buffer) : writePointcut(fileStream, buffer) {
		System.out.println("fx Aspectj write is start");
		//检查加密功能是否可用
		//如果Android没有正确载入库,则此步骤可能失败
		if (!crypto.isAvailable()) {
			System.out.println("return error");
			return;
		}
		OutputStream fbFileStream = new BufferedOutputStream(fileStream);
		try {
			//创建输出流,当数据写入流的时候进行加密,并将加密后的数据输出到文件
			OutputStream outputStream = crypto.getCipherOutputStream(
					fbFileStream, new Entity("test"));
			outputStream.write(buffer);
			outputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//读文件切点集合
	pointcut readPointcut(FileInputStream fileStream, byte[] buffer) : !within(FileAspectJ) && target(fileStream)&& args(buffer) && call(* read(..));
	int around(FileInputStream fileStream, byte[] buffer) : readPointcut(fileStream, buffer) {
		System.out.println("fx Aspectj read is start");
		int bufferSize = 0;
		if(isEncryption==false){
			return 0;
		}
		try {
			//文件流解密操作
			InputStream inputStream = crypto.getCipherInputStream(fileStream,	new Entity("test"));
			bufferSize = inputStream.available(); // 取得输入流的字节长度
			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
	        byte[] data = new byte[1024];
			int len;
			 if (inputStream != null) {
		            try {
		                while ((len = inputStream.read(data)) != -1) {
		                    outputStream.write(data, 0, len);
		                }
		                data = outputStream.toByteArray();
		            } catch (IOException e) {
		            }
		        }
			 for(int i = 0;i<data.length;i++) {
				 buffer[i] = data[i];
			 }
			inputStream.close();
			outputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return bufferSize;
	}

}

AspectJ截获操作的更多相关文章

  1. Spring的入门学习笔记 (AOP概念及操作+AspectJ)

    AOP概念 1.aop:面向切面(方面)编程,扩展功能不通过源代码实现 2.采用横向抽取机制,取代了传统的纵向继承重复代码 AOP原理 假设现有 public class User{ //添加用户方法 ...

  2. WCF 已知类型和泛型解析程序 KnownType

    数据协定继承 已知类型和泛型解析程序 Juval Lowy 下载代码示例 自首次发布以来,Windows Communication Foundation (WCF) 开发人员便必须处理数据协定继承方 ...

  3. KVM 实现机制

    1.1.    KVM简介 KVM是一个基于Linux内核的虚拟机,它属于完全虚拟化范畴,从Linux-2.6.20开始被包含在Linux内核中.KVM基于x86硬件虚拟化技术,它的运行要求Intel ...

  4. DPDK support for vhost-user

    转载:http://blog.csdn.net/quqi99/article/details/47321023 X86体系早期没有在硬件设计上对虚拟化提供支持,因此虚拟化完全通过软件实现.一个典型的做 ...

  5. [Spring-AOP-XML] 利用Spirng中的AOP和XML进行事务管理

    Spring中的AOP进行事务管理有三种方式 A.自定义事务切面 利用AspectJ来编写事务,我们一般把这个切面作用在service层中.其他代码在下面 编写一个Transaction实现类,通过S ...

  6. Windows API 查找窗体,发送Windows消息

    最近项目中需要做Windows消息截获操作,在网上找了一些资料. public class WindowsAPI { /// <summary> /// 回调函数代理 /// </s ...

  7. KVM分析报告

    转载   KVM分析报告   虚拟化技术工作组 2008-12-31 1.    概述 1.1.    KVM简介 KVM是以色列开源组织Qumranet开发的一个开源虚拟机监控器,从Linux-2. ...

  8. 《Spring源码深度解析》一

    Spring整体架构 1.1 Spring整体架构 1.1.1 Core Container: 模块:Core.Beans.Context和Expression Language Core:框架的基础 ...

  9. Spring_day03--课程安排_基于aspectj的注解aop_Spring的jdbcTemplate操作

    Spring_day03 上节内容回顾 今天内容介绍 基于aspectj的注解aop Spring的jdbcTemplate操作 增加 修改 删除 查询 Spring配置c3p0连接池和dao使用jd ...

随机推荐

  1. Java 一个字符串在另外一个字符串出现次数

    统计一个字符串在另外一个字符串出现次数 代码如下: package me.chunsheng.javatest; import java.util.regex.Matcher; import java ...

  2. Ecstore 2.0 报表显示空白

    INSERT INTO `sdb_ectools_analysis` (`id`, `service`, `interval`, `modify`) VALUES (, ), (, ), (, ); ...

  3. 禁用ubuntu的触摸板和独显

    #!/bin/bash #This is a vgaoff & touchpadoff #By spinestars #-2-18#TouchPad & VGA OFF == ];th ...

  4. 异常处理与调试 - 零基础入门学习Delphi50

    异常处理与调试 让编程改变世界 Change the world by program 异常处理与调试 在应用程序开发中如何检测.处理程序的运行错误是一个很重要的问题. 有些错误是无法控制的. 如何处 ...

  5. Java中的深复制与浅复制

    1.浅复制与深复制概念 ⑴浅复制(浅克隆) 被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用仍然指向原来的对象.换言之,浅复制仅仅复制所考虑的对象,而不 复制它所引用的对象. ...

  6. Keil UV4 BUG(带字库液晶不能显示“数、正、过”问题的请看)

    Keil UV3一直存在汉字显示(0xFD)的bug,以前在用到带字库的12864液晶的时候,“数”字总是不能正常显示,后来有网友告诉我这是keil的bug,解决掉了.后来keil升级了,我也换了新版 ...

  7. 数据库下载word预览功能的研究

    本文参考了这里的一些方法http://tobetobe.blog.51cto.com/1392243/354420 一直想通过缓存来实现,奈何技术不够,走了曲线救国的思路,先下载,然后预览,删除下载文 ...

  8. 【转】 怎么刷入BOOT.IMG(刷机后开机卡在第一屏的童鞋请注意)-------不错不错

    原文网址:http://bbs.gfan.com/android-3440837-1-1.html 之前呢,有好多机油问我关于刷机卡屏的问题,我解答了好多,但一一解答太费事了,在这里给大家发个贴吧.其 ...

  9. 如何为WPF添加Main()函数 程序入口点的修改

    一般的.WPF的Main()函数是自动生成的,不过有时候我们需要为我们的应用程序传参.那么自动生成的Main()函数就不会满足我们的要求. 那么如何为WPF Application 设置Main()函 ...

  10. CentOS、Ubuntu、Debian三个linux比较异同

    Linux有非常多的发行版本,从性质上划分,大体分为由商业公司维护的商业版本与由开源社区维护的免费发行版本. 商业版本以Redhat为代表,开源社区版本则以debian为代表.这些版本各有不同的特点, ...