『原创』+『参考』PPC丢失后,手机信息如何保护?(C#)

 

现在,好多人都开始使用Windows Phone了,其提供了个人信息管理功能十分强大,无奈的是,如果当我们过分依赖于这些辅助功能,那么一旦设备丢失或被盗(结果一样吧,哈哈),个人信息将遗漏无疑啊。所以,为了保护我们的个人信息,在这里写一个Sim卡识别程序,如果是非本人的SIM卡插入,则删除本机的相关个人信息,以防被盗!

首先,如何识别我们的Sim卡呢?它有一组20位(也许吧)的识别码,应该是比较全球唯一吧。而怎么去获得这个ID呢?这里用到一个P/V invoke技术,实际上就是使用cellcore.dll的Sim卡功能类SimInitialize等,用这些功能读出插在手机中的SIM卡的ID。

其次,应该在何种场合进行判断呢?其实大家因该有这种经验:WM的PPC一般不会关机,除非换电池和SIM卡。所以,我们的程序需要在机器开机的时候运行,而且最好不要有什么用户界面,所以,我选择新建一个智能设备的控制台项目,作为我们的开发环境。

当然,还是选用.net cf 2.0的环境,模拟器么,选择WM6吧。

至此,我们已经建立起一个项目,下面我们需要往其中加入代码,其中,核心代码如下:

 

SIM类
using System;

using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace SmartDeviceProject1
{
    
public class SIM
    {

        
#region 平台调用
        [DllImport(
"cellcore.dll")]
        
public extern static int SimInitialize(int dwFlags, IntPtr lpfnCallback, int dwParam, ref IntPtr lphSim);

        [DllImport(
"cellcore.dll")]
        
public extern static int SimDeinitialize(IntPtr hSim);

        [DllImport(
"cellcore.dll")]
        
public extern static int SimReadRecord(IntPtr hSim, int dwAddress, int dwRecordType, int dwIndex, byte[] lpData, int dwBufferSize, ref int dwSize);

        
int EF_ICCID = 0x2fe2;
        
int SIM_RECORDTYPE_TRANSPARENT = 1;
        
#endregion
        
        
//返回Sim卡背面的20位ICCID 
        public string SimSerialNumber()
        {
            IntPtr hSim 
= default(IntPtr);
            
byte[] iccid = new byte[10];
            
int dwsize = 0;
            SimInitialize(
0, IntPtr.Zero, 0ref hSim);
            SimReadRecord(hSim, EF_ICCID, SIM_RECORDTYPE_TRANSPARENT, 
0, iccid, iccid.Length, ref dwsize);//在这里获取了SIM卡ID
            SimDeinitialize(hSim);
            
return FormatAsSimString(iccid);
        }
        
/// <summary>
        
/// 对SIM卡ID进行格式化
        
/// </summary>
        
/// <param name="iccid">byte格式的ID</param>
        
/// <returns>字符串格式</returns>
        private static string FormatAsSimString(byte[] iccid)
        {
            
string rawString = GetRawIccIDString(iccid);
            
return String.Format("{0} {1} {2} {3}", rawString.Substring(06), rawString.Substring(65), rawString.Substring(114), rawString.Substring(154));
        }
        
/// <summary>
        
/// 格式转换函数
        
/// </summary>
        
/// <param name="iccid"></param>
        
/// <returns></returns>
        private static string GetRawIccIDString(byte[] iccid)
        {
            System.Text.StringBuilder builder 
= new System.Text.StringBuilder();
            
int i = 0;
            
while (i < iccid.Length)
            {
                
byte b = iccid[i];
                builder.Append(ConvertInt4PairToString(b));
                Math.Max(System.Threading.Interlocked.Increment(
ref i), i - 1);
            }
            
return builder.ToString();
        }

        
private static string ConvertInt4PairToString(byte byteValue)
        {
            
return ((byte)(byteValue << 4| (byteValue >> 4)).ToString("x2");
        } 

        

    }
}

 

核心代码我们有了,大家应该通过查资料和注释大概能理解程序要做的事情了吧,其实很简单,对不对?好,下一步我们在Programs.cs中添加功能调用代码如下:

Main
using System;

using Microsoft.WindowsMobile.PocketOutlook;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using Microsoft.VisualBasic; 

namespace SmartDeviceProject1
{
    
class Program
    {
        
        
static void Main(string[] args)
        {
            SIM card 
= new SIM();
            
//获取注册表
            RegistryKey reg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\TEST"true);
            
if ((reg == null))
            {
                reg 
= Registry.LocalMachine.CreateSubKey("SOFTWARE\\TEST");//如果为空则添加一个键值
               
            }

            
if (reg.GetValue("sim")==null)
            {
                
if (Interaction.MsgBox("是否设定当Sim CardID不符时,自动发送短信", MsgBoxStyle.OkCancel,"提示"== MsgBoxResult.Ok)
                {

                    reg.SetValue(
"sim", card.SimSerialNumber());
                }
            }
            
else
            {
                
if (reg.GetValue("sim").ToString() != card.SimSerialNumber())
                {
                    OutlookSession Session 
= new OutlookSession();
                    
//删除所有联系人
                    Session.Contacts.Items.Clear();
                    
//删除所有约会
                    Session.Appointments.Items.Clear();
                    
//删除所有工作计划
                    Session.Tasks.Items.Clear();
                    
                    Microsoft.WindowsMobile.PocketOutlook.SmsMessage sms 
= new Microsoft.WindowsMobile.PocketOutlook.SmsMessage();
                    sms.Body 
= card.SimSerialNumber();
                    
//发一封短信到自己的手机,內容包含目前sim iccid
                    sms.To.Add(new Recipient("1234"));
                    sms.Send();
                }
                
else
                {
                    reg.Close();
                    Microsoft.WindowsMobile.PocketOutlook.SmsMessage sms 
= new Microsoft.WindowsMobile.PocketOutlook.SmsMessage();
                    sms.Body 
= card.SimSerialNumber();
                    sms.To.Add(
new Recipient("1234"));
                    sms.Send();
                    Interaction.MsgBox(
"欢迎回来,主人!", MsgBoxStyle.OkCancel, "提示");
                }
                
            } 


        }
        

    }
}

 话说至此程序已经完成,我们还需设置项目的生成属性,其中:

输出文件夹为:Start Menu Startup文件夹,子目录为空,Ok.

下面我们就点击调试吧,系统打开WM6 Professional的模拟器,然后自动运行程序,会显示如下画面:

我们点击确定,之后,新建一个联系人

软启动设备,直至系统进入桌面,我们并没有看到什么提示,但是看看我们的Cellular Emulator,它是不是收到一条短信啊:

呵呵,再看看联系人一览:

哈哈~~~~

到此,整个程序就完成了。当然,还可以发挥你的想象力,加入更牛A的操作,这就看你了,最后弱弱的温馨提示一句:真机调试本程序前,最好备份好你自己的联系人等信息,造成的任何意外损害,我概不负责啊~~~~~

参考文档

AppleSeeker的系列文章,很不错啊http://www.cnblogs.com/appleseeker/archive/2008/03/29/1129031.html
这里是一个相关讨论,大家也可以看看http://blog.opennetcf.com/ncowburn/CommentView,guid,309b8b8e-ebc7-4078-a8ba-f174761ea7af.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值