Android编程之杀毒的实现原理及具体实例
程序员文章站
2023-12-17 20:34:40
本文实例讲述了android杀毒的实现原理。分享给大家供大家参考,具体如下:
一个杀毒软甲最核心的部分一个是病毒库一个是杀毒引擎,病毒库从服务器中获得,杀毒引擎实际上是判...
本文实例讲述了android杀毒的实现原理。分享给大家供大家参考,具体如下:
一个杀毒软甲最核心的部分一个是病毒库一个是杀毒引擎,病毒库从服务器中获得,杀毒引擎实际上是判断程序中的包名和签名是否匹配病毒库中的包名和签名,如果匹配则为病毒,界面使用帧动画来显示。
思路:
1.从服务器端把病毒的版本库信息下载下来将解析的数据存放到list集合中
2.获取到手机中所有应用程序的包名以及程序的签名
3.将病毒库匹配手机应用程序包名及签名
4.用scrollview标签进行自动滚动显示
关键代码如下:
特洛伊木马病毒库的信息:
<?xml version="1.0" encoding="utf-8"?> <list> <virus> <name>tory.virus</name> <packname> cn.itcast.virus </packname> <description> 恶意软件,读取用户日志</description> <signature> 3082020730820170a00302010202044ea7598f300d06092a864886f70d010105050030483 10a30080603550406130131310a30080603550408130131310a3008060355040713013131 0a3008060355040a130131310a3008060355040b130131310a30080603550403130131301 e170d3131313032363030353132375a170d3231313032333030353132375a3048310a3008 0603550406130131310a30080603550408130131310a30080603550407130131310a30080 60355040a130131310a3008060355040b130131310a3008060355040313013130819f300d 06092a864886f70d010101050003818d0030818902818100d915d7a98cde8bcd69b87ec52 11012ace847de42129a71bf679a059c2c55e893bc0ea886874432ab8b9097724211df6769 eacd3381ccac779ab7422d8101320b1e0b14e06ac8ee095b20e52cbe6163e10a87dc410b8 a91fb73d53c5bdb4a22d1295c61e04b8f8b68c475e69c1754a1dc35745e7c6ae0275c2620 b863b0d9ea8f0203010001300d06092a864886f70d01010505000381810038e1119fbb710 4180fddba4bc8b2c275df63f0df418b7480d8eba2891da20d34d3d083cfed7bb3eb546863 c76bc67cc93f2fa0e9377c470881c9a763c99cc035093184bb50f76e74155592eca3566a3 10af55e5fec19d6fdc1a74f226aef485f84389126e8e3f4b59fe2797cbfcac660b9f2cc81 e6f3dcaa7cb2001ecc496a7b </signature> </virus> </list>
杀毒引擎:
/* * 杀毒引擎(下载病毒库、获取程序的包名及签名并进行匹配) * (non-javadoc) * @see android.app.activity#ontouchevent(android.view.motionevent) */ @override public boolean ontouchevent(motionevent event) { packagenames = new arraylist<string>(); virusresult = new arraylist<string>(); infos = new arraylist<applicationinfo>(); animationdrawable.start();//播放扫描病毒的动画 new thread(){ @override public void run() { try { url url = new url("http://192.168.1.168:8080/virus.xml"); httpurlconnection conn = (httpurlconnection) url.openconnection(); inputstream is = conn.getinputstream(); //从服务器解析病毒库并获取到病毒库的集合 virusbeans = virusinfo.getvirusinfos(is); taskinfo taskinfo = new taskinfo(killvirusactivity.this); //实例化包资源管理器 //获取到当前手机里面所有的包名 infos = pm.getinstalledapplications(0); for(applicationinfo info : infos ){ packagenames.add(info.packagename); } int count=0; // 杀毒引擎 根据病毒库 比对当前系统里面的程序包名 签名进行 杀毒 stringbuilder sb = new stringbuilder(); for(string packname : packagenames){ sb.append("正在扫描 "+ packname); sb.append("\n"); message msg = new message(); msg.what = scanning; msg.obj = sb; handler.sendmessage(msg); //检查当前的packname 和对应签名 是不是跟病毒库里面的信息一样 for(virusbean virusbean : virusbeans){ if(packname.equals(virusbean.getpackname())&& taskinfo.getappsignature(packname).equals(virusbean.getsignature())) { virusresult.add(packname);//添加一个病毒 } } count ++;//记录病毒的总数 } message msg = new message(); msg.what = scanning_finish; msg.obj = count; handler.sendmessage(msg); } catch (exception e) { e.printstacktrace(); } } }.start(); return super.ontouchevent(event); }
显示病毒扫描信息:
handler handler = new handler(){ @override public void handlemessage(message msg) { super.handlemessage(msg); switch (msg.what) { case scanning: stringbuilder sb = (stringbuilder) msg.obj; tv_killvirus_info.settext(sb.tostring()); sv.scrollby(0, 25);//每次增加都会自动向下移动画面 break; case scanning_finish: int i = (integer) msg.obj; stringbuilder sb1 = new stringbuilder(); sb1.append("扫描完毕 共扫描 "+ i+ " 个程序"); if(virusresult.size()>0){ sb1.append("发现病毒 \n"); for(string packname : virusresult){ sb1.append("病毒名"+ packname); sb1.append("\n"); } } tv_killvirus_info.settext(sb1.tostring()); animationdrawable.stop(); break; } } };
获取到程序的签名:
/* * 获取程序的签名 */ public string getappsignature(string packname){ try { packageinfo packinfo =pm.getpackageinfo(packname, packagemanager.get_signatures); //获取到所有的权限 return packinfo.signatures[0].tocharsstring(); } catch (namenotfoundexception e) { e.printstacktrace(); return null; } }
显示扫描的文件页面并自动滚动:
<scrollview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/iv_killvirus_am" android:id="@+id/sv_killvirus" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_killvirus_info" ></textview> </scrollview>
希望本文所述对大家android程序设计有所帮助。