AspectJ截获操作
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截获操作的更多相关文章
- Spring的入门学习笔记 (AOP概念及操作+AspectJ)
AOP概念 1.aop:面向切面(方面)编程,扩展功能不通过源代码实现 2.采用横向抽取机制,取代了传统的纵向继承重复代码 AOP原理 假设现有 public class User{ //添加用户方法 ...
- WCF 已知类型和泛型解析程序 KnownType
数据协定继承 已知类型和泛型解析程序 Juval Lowy 下载代码示例 自首次发布以来,Windows Communication Foundation (WCF) 开发人员便必须处理数据协定继承方 ...
- KVM 实现机制
1.1. KVM简介 KVM是一个基于Linux内核的虚拟机,它属于完全虚拟化范畴,从Linux-2.6.20开始被包含在Linux内核中.KVM基于x86硬件虚拟化技术,它的运行要求Intel ...
- DPDK support for vhost-user
转载:http://blog.csdn.net/quqi99/article/details/47321023 X86体系早期没有在硬件设计上对虚拟化提供支持,因此虚拟化完全通过软件实现.一个典型的做 ...
- [Spring-AOP-XML] 利用Spirng中的AOP和XML进行事务管理
Spring中的AOP进行事务管理有三种方式 A.自定义事务切面 利用AspectJ来编写事务,我们一般把这个切面作用在service层中.其他代码在下面 编写一个Transaction实现类,通过S ...
- Windows API 查找窗体,发送Windows消息
最近项目中需要做Windows消息截获操作,在网上找了一些资料. public class WindowsAPI { /// <summary> /// 回调函数代理 /// </s ...
- KVM分析报告
转载 KVM分析报告 虚拟化技术工作组 2008-12-31 1. 概述 1.1. KVM简介 KVM是以色列开源组织Qumranet开发的一个开源虚拟机监控器,从Linux-2. ...
- 《Spring源码深度解析》一
Spring整体架构 1.1 Spring整体架构 1.1.1 Core Container: 模块:Core.Beans.Context和Expression Language Core:框架的基础 ...
- Spring_day03--课程安排_基于aspectj的注解aop_Spring的jdbcTemplate操作
Spring_day03 上节内容回顾 今天内容介绍 基于aspectj的注解aop Spring的jdbcTemplate操作 增加 修改 删除 查询 Spring配置c3p0连接池和dao使用jd ...
随机推荐
- 分享几个实用的jquery工具函数
1.$.browser对象属性 属性列表 说明 webkit webkit相关浏览器则返回true,否则返回false,如google,傲游. mozilla mozilla相关浏览器则返回tru ...
- 在head标签里加一个meta标签让指定ie使用特定内核 解决css在ie中的兼容性问题
<meta http-equiv="x-ua-compatible" content="IE=edge, chrome=1"/> IE=edge: ...
- 利用raspberry pi搭建typecho笔记(一) nginx PHP server quick start
前言 因为一直对linux学习很有兴趣,就拿手头的树莓派做了实验,搭建一个简易的php服务器用来跑typecho. 但是过程却是异乎寻常的艰辛,几乎每一步能卡住得地方都卡住了.而且typecho的资料 ...
- linux mysql 优化
第一 在 /etc/my.cnf 中加入 skip-name-resolve ,重启mysql,这样就能禁用DNS解析,连接速度会快很多.不过,这样的话就不能在MySQL的授权表中使用主机名了而只能用 ...
- Ubuntu14.0.4 64位安装ADT问题
将ADT 解压之后,新建Android工程后没有R文件: google之后说要安装 ia32-libs 提示如下: 安装lib32z1 安装完成后,再次新建工程,报错如下: 编译存在问题:则继续安装以 ...
- Log4net 可直接使用的配置
config配置 <xml version="1.0"> <configuration> <configSections> <!--配置一 ...
- js 属性类型
1.访问器属性 var book = { _year: 2004, edition: 1 }; Object.defineProperty(book, "year", { get: ...
- QWidget可以设置QStyle,它可以绘制很多东西(具体内容没研究,待续)
QStyle * QWidget::style() const See also QWidget::setStyle(), QApplication::setStyle(), and QApplica ...
- VC使用#定义方便控制版本号的宏
一个 VC Project 中,可能有很多地方需要用到版本号,比如 About 对话框.版本资源等.如果每次版本更改都一一去改变这些值,不但非常麻烦,而且有悖唯一原则. 巧妙地使用宏定义,可以很好地解 ...
- UESTC_冰雪奇缘 CDOJ 843
艾莎女王又开始用冰雪魔法盖宫殿了. 她决定先造一堵墙,于是释放魔法让形为直角梯形的冰砖从天而降,定入冻土之中. 现在你将回答女王的询问:某段冻土上冰砖的面积. 注:多块冰砖之间会互相重叠,重叠部分要多 ...