138.s1-杀毒功能的实现

本文介绍了一款杀毒软件的工作原理和技术实现细节。通过病毒库比对文件的MD5值来检测并清除病毒。文章详细解释了从初始化杀毒引擎到扫描文件、获取MD5值的过程,并展示了如何在界面上显示扫描进度。

杀毒的原理是首先拥有一个病毒库,病毒库是一个数据库,每个病毒会有md5值,通过遍历病毒库,查看是否有病毒的md5,因此首先需要把assert目录下面的病毒库拷贝到file文件夹才能处理

杀病毒的界面activity_antivirous.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView 
        style="@style/TitleStyle"
        android:text="病毒查杀"
        />
    <LinearLayout 
        android:layout_width="match_parent"
    	android:layout_height="wrap_content"
    	android:orientation="horizontal"
    	android:layout_marginTop="10dp"
        >
        <FrameLayout 
        android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
        	>
		    <ImageView 
		        android:layout_width="wrap_content"
		    	android:layout_height="wrap_content"
		    	android:layout_marginLeft="5dp"
		    	android:layout_marginRight="5dp"
		    	android:background="@drawable/ic_scanner_malware"
		        />
		    <ImageView 
		        android:id="@+id/iv_scanning"
		        android:layout_width="wrap_content"
		    	android:layout_height="wrap_content"
		    	android:layout_marginLeft="5dp"
		    	android:layout_marginRight="5dp"
		    	android:background="@drawable/act_scanning_03"
		        />
	    </FrameLayout>
		<LinearLayout 
			android:layout_width="match_parent"
	    	android:layout_height="wrap_content" 
	    	android:layout_marginTop="10dp"
	    	android:orientation="vertical"
		    >
		    <TextView 
		        android:id="@+id/tv_init_virus"
		        android:layout_width="match_parent"
	    		android:layout_height="wrap_content"
	    		android:gravity="center_horizontal"
	    		android:textColor="@color/black"
	    		android:text="初始化杀毒引擎"
		        />
		    <ProgressBar 
		        android:id="@+id/progressBarVirous"
		        style="?android:attr/progressBarStyleHorizontal"
		        android:layout_width="match_parent"
	    		android:layout_height="wrap_content"
	    		android:layout_marginLeft="5dp" 
	    		android:layout_marginRight="10dp"
		        />
		</LinearLayout>
    </LinearLayout>
    
    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:id="@+id/ll_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>
   
</LinearLayout>

获取文件的md5值MD5Utils.java

package com.ldw.safe.utils;

import java.io.File;
import java.io.FileInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Utils {

	/**
	 * md5加密,md5永远是32位
	 * 
	 * @param password
	 * @return
	 */
	public static String encode(String password) {
		try {
			MessageDigest instance = MessageDigest.getInstance("MD5");//获取MD5算法对象
			byte[] digest = instance.digest(password.getBytes());//对字符串加密,返回字符数组

			StringBuffer sb = new StringBuffer();
			for (byte b : digest) {
				int i = b & 0xff;//取字节的低8位有效值
				String hexString = Integer.toHexString(i);//将整数转为16进制

				if (hexString.length() < 2) {
					hexString = "0" + hexString;//如果只有1位就补零
				}

				sb.append(hexString);
			}

			return sb.toString();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			//没有该算法的时候,抛出异常
		}

		return "";
	}

	/*
	 * 获取到文件的MD5值,文件的特征码
	 */
	public static String getFileMd5(String sourceDir) {
		
		
		try {
			//创建一个流来读取文件
			File file = new File(sourceDir);
			FileInputStream fis = new FileInputStream(file);
			byte[] buffer = new byte[1024];
			int len = -1;
			
			MessageDigest messageDigest = MessageDigest.getInstance("md5");
			
			while((len = fis.read(buffer)) != -1){
				//获取到数字摘要
				messageDigest.update(buffer, 0, len);
			}
			byte[] result = messageDigest.digest();
			
			StringBuffer sb = new StringBuffer();
			for (byte b : result) {
				int i = b & 0xff;//取字节的低8位有效值
				String hexString = Integer.toHexString(i);//将整数转为16进制

				if (hexString.length() < 2) {
					hexString = "0" + hexString;//如果只有1位就补零
				}

				sb.append(hexString);
			}
			return sb.toString();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}

杀病毒的逻辑文AntiVirousActivity.java

package com.ldw.safe.Activity;

import java.util.List;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;

import com.ldw.safe.R;
import com.ldw.safe.db.dao.AntiVirous;
import com.ldw.safe.utils.MD5Utils;

public class AntivirusActivity extends Activity {
	
	protected static final int BEGIN = 1;
	protected static final int SCANING = 2;
	protected static final int FINISH = 3;
	private Message message;
	private TextView tv_init_virus;
	private ProgressBar pb;
	private ImageView iv_scanning;
	private LinearLayout ll_content;
	private ScrollView scrollView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        initUi();
        initData();
        }
	
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg){
			switch(msg.what){
			case BEGIN:
				tv_init_virus.setText("初始化八核引擎");
				break;

			case SCANING:
				// 病毒扫描中:
				//创建一个TextView,存放应用的名字使用scanInfo
				TextView child = new TextView(AntivirusActivity.this);

				ScanInfo scanInfo = (ScanInfo) msg.obj;
				// 如果为true表示有病毒
				if (scanInfo.desc) {
					child.setTextColor(Color.RED);
					child.setText(scanInfo.appName + "有病毒");

				} else {
					child.setTextColor(Color.BLACK);
					// 为false表示没有病毒
					child.setText(scanInfo.appName + "扫描安全");

				}
				//更新下面的扫描的文件
				ll_content.addView(child, 0);
				//自动滚动
				scrollView.post(new Runnable() {
					
					@Override
					public void run() {
						//一直往下面进行滚动
						scrollView.fullScroll(scrollView.FOCUS_DOWN);
						
					}
				});
	
				System.out.println(scanInfo.appName + "扫描安全");
				break;
			case FINISH:
				// 当扫描结束的时候。停止动画
				iv_scanning.clearAnimation();
				break;
			}
		};
	};


	//初始化数据
	private void initData() {
		
		new Thread(){

			public void run(){
				
				message = Message.obtain();
				//扫描病毒开始
				message.what = BEGIN;
				
				//获取到包管理器
				PackageManager packageManager = getPackageManager();
				// 获取到所有安装的应用程序
				List<PackageInfo> installedPackages = packageManager
						.getInstalledPackages(0);
				// 返回手机上面安装了多少个应用程序
				int size = installedPackages.size();
				//设置进度条的最大值
				pb.setMax(size); 
				//初始化进度条状态
				int progress = 0;
				
				for(PackageInfo packageInfo:installedPackages){
					
					ScanInfo scanInfo = new ScanInfo();
					
					// 获取到当前手机上面的app的名字
					String appName = packageInfo.applicationInfo.loadLabel(
							packageManager).toString();
					//初始化scanInfo
					scanInfo.appName = appName;
					//获取到应用的包名
					String packageName = packageInfo.applicationInfo.packageName;
					scanInfo.packageName = packageName;
					
					// 首先需要获取到每个应用程序的目录
					String sourceDir = packageInfo.applicationInfo.sourceDir;
					// 获取到文件的md5
					String md5 = MD5Utils.getFileMd5(sourceDir);
					// 判断当前的文件是否是病毒数据库里面
					String desc = AntiVirous.checkFileVirous(md5);
					
					// 如果当前的描述信息等于null说明没有病毒
					if (desc == null) {
						scanInfo.desc = false;
					} else {
						scanInfo.desc = true;
					}
					//进度条更新
					progress ++;
					//杀一次,休眠100
					SystemClock.sleep(100);
					pb.setProgress(progress);
					
					message =Message.obtain();
					//扫描病毒中
					message.what = SCANING;
					message.obj = scanInfo;
					
					handler.sendMessage(message);
				}
				
				message =Message.obtain();
				//扫描病毒后
				message.what = FINISH;
				handler.sendMessage(message);
			};
		}.start();
		
	}
	
	//扫描的信息
	static class ScanInfo {
		boolean desc;
		String appName;
		String packageName;
	}

	//初始化UI
	private void initUi() {
		setContentView(R.layout.activity_antivirous);

		tv_init_virus = (TextView) findViewById(R.id.tv_init_virus);

		pb = (ProgressBar) findViewById(R.id.progressBarVirous);
		
		iv_scanning = (ImageView) findViewById(R.id.iv_scanning);
		
		ll_content = (LinearLayout) findViewById(R.id.ll_content);
		
		scrollView = (ScrollView) findViewById(R.id.scrollView);
		
		// 第一个参数表示开始的角度 第二个参数表示结束的角度 第三个参数表示参照自己 初始化旋转动画
		RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		// 设置动画的时间
		rotateAnimation.setDuration(5000);
		// 设置动画无限循环
		rotateAnimation.setRepeatCount(Animation.INFINITE);
		// 开始动画
		iv_scanning.startAnimation(rotateAnimation);
		
	}

}

工具类实现病毒的库的遍历(查杀病毒)AntiVirous.java

package com.ldw.safe.db.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/*
 * 杀病毒的dao,检查当前的md5是否在病毒数据库
 */
public class AntiVirous {

	
	public static String checkFileVirous(String md5){
		//返回定都的描述
		String desc = null;
		
		//数据库的路径,前面是data/data后面跟着包的名字,路径必须要这样写
		final String path ="data/data/com.ldw.safe/files/antivirus.db";
		//读取数据库
		SQLiteDatabase database = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
		
		//查询当前传过来的md5是否在病毒数据库里面
		Cursor cursor = database.rawQuery("select desc from datable where md5 = ?", new String[]{md5});
		//判断当前的游标是否可以移动
		if(cursor.moveToNext()){
			desc = cursor.getString(0);
		}
		cursor.close();
		return desc;
	}
	
	/*
	 * 添加病毒
	 */
	public static void addVirus(String md5,String desc){
		
	}
}

 

 

 

 

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值