注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

石蕴玉而山辉,水怀珠而川媚!

 
 
 

日志

 
 

C#读取SIM卡信息  

2010-12-15 13:58:37|  分类: 开发入门 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

C#读取SIM卡信息完全可以参照之前的获取 Sim 卡 ICCID 的方式来进行。不过,重要的是找准资料。这里参考的是《GSM 协议标准 5.0 GSM 11.11》,1995年版。《Digital cellular telecommunications system (Phase 2+);Specification of the Subscriber Identity Module - Mobile Equipment (SIM - ME) interface (GSM 11.11)》,数字蜂窝通讯系统(Phase 2+)-移动设备用户信息识别接口规范(GSM 11.11)。

还是在 dot net 中,采用 C# 。

需要用到的数据结构(EF:Elementary Files):
[StructLayout(LayoutKind.Sequential)]
private struct SimRecord
{
    public IntPtr cbSize;
    public IntPtr dwParams;
    public IntPtr dwRecordType;
    public IntPtr dwItemCount;
    public IntPtr dwSize;
}

[StructLayout(LayoutKind.Sequential)]
private struct EF_Content
{
    public int dwAddress;
    public string Description;

    public EF_Content(int addr, string desc)
    {
        dwAddress = addr;
        Description = desc;
    }
}
需要获取的资料以及描述(这里包含 Sim 卡中的各种信息,包括一些私密的东西 -_-!!,不过有些是读不出来的):
private EF_Content[] efs =

    new EF_Content(0x2FE2, "ICC identification"),
    new EF_Content(0x6F05, "Language preference"),
    new EF_Content(0x6F07, "IMSI"),
    new EF_Content(0x6F20, "Ciphering key Kc"),
    new EF_Content(0x6F30, "PLMN selector"),
    new EF_Content(0x6F31, "HPLMN search period"),
    new EF_Content(0x6F37, "ACM maximum value"),
    new EF_Content(0x6F38, "SIM service table"),
    new EF_Content(0x6F39, "Accumulated call meter"),
    new EF_Content(0x6F3E, "Group identifier level 1"),
    new EF_Content(0x6F3F, "Group identifier level 2"),
    new EF_Content(0x6F41, "PUCT"),
    new EF_Content(0x6F45, "CBMI"),
    new EF_Content(0x6F46, "Service provider name"),
    new EF_Content(0x6F74, "BCCH"),
    new EF_Content(0x6F78, "Access control class"),
    new EF_Content(0x6F7B, "Forbidden PLMNs"),
    new EF_Content(0x6F7E, "Location information"),
    new EF_Content(0x6FAD, "Administrative data"),
    new EF_Content(0x6FAE, "Phase identification"),
    new EF_Content(0x6F3A, "Abbreviated dialling numbers"),
    new EF_Content(0x6F3B, "Fixed dialling numbers"),
    new EF_Content(0x6F3C, "Short messages"),
    new EF_Content(0x6F3D, "Capability configuration parameters"),
    new EF_Content(0x6F40, "MSISDN storage"),
    new EF_Content(0x6F42, "SMS parameters"),
    new EF_Content(0x6F43, "SMS status"),
    new EF_Content(0x6F44, "Last number dialled"),
    new EF_Content(0x6F4A, "Extension 1"),
    new EF_Content(0x6F4B, "Extension 2")
    // And so on….
};

    /// <summary>
    /// 获取 Sim 卡中的信息。
    /// </summary>
    /// <returns> 按行返回所有能够获取的 Sim 卡信息,详细请参考《GSM 标准 5.0(11.11)》。</returns>
    public string GetAllSection()
    {
        StringBuilder sb = new StringBuilder();
        try
        {
            IntPtr res, hSim;
            res = SimInitialize(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, out hSim);
            if (res != IntPtr.Zero)
                throw new Exception("Can not Initialize Sim(" + res + ").");

            for (int j = 0; j < efs.Length; j++)
            {
                try
                {
                    SimRecord rec = new SimRecord();
                    rec.cbSize = (IntPtr)Marshal.SizeOf(rec);
                    res = SimGetRecordInfo(hSim, (IntPtr)efs[j].dwAddress, ref rec);
                    if (res != IntPtr.Zero)
                        throw new Exception("Could not read \"" + efs[j].Description + "\"’s record information from the SIM(" + res + ").");
                    byte[] bData = new byte[(int)rec.dwSize + 1];
                    IntPtr dwBytesRead = IntPtr.Zero;
                    res = SimReadRecord(hSim, (IntPtr)efs[j].dwAddress, rec.dwRecordType, IntPtr.Zero, bData, (IntPtr)bData.Length, ref dwBytesRead);
                    if (res != IntPtr.Zero)
                        throw new Exception("Could not read \"" + efs[j].Description + "\"’s content from the SIM(" + res + ")");

                    string str = "";
                    for (int i = 0; i < bData.Length - 1; i++)
                    {
                        str = str + bData[i].ToString("X2");//ReversByte(bData[i]);
                    }
                    str = efs[j].Description + "(len: " + dwBytesRead.ToInt32() + "): " + str + "\r\n";
                    sb.Append(str);
                }
                catch(Exception ex)
                {
                    sb.Append(ex.Message + "\r\n");
                }
                Application.DoEvents();
            }
            SimDeinitialize(hSim);
        }
        catch (Exception ex)
        {
            throw ex;// ex.Message;
        }
        return sb.ToString();
    }
}

返回能读取的 Sim 卡信息,很多很多,

  评论这张
 
阅读(2441)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017