电信小灵通SMGP1.32解包组包示例(C#版)

因为SMGP1.32协议有多个消息包,在这里我就用Login包进行示例

/// <summary>
  /// 凳录包
  /// </summary>
  public class SMGP_MSG_Login
  {
   /// <summary>
   /// SPCP编号或者SMGW编号
   /// 编号规则参见总册第六6节
   /// </summary>
   string _ClientID;
   /// <summary>
   /// 客户端密码
   /// 用于鉴别客户端的接入请求  。
   /// 其值通过单向MD5 hash计算得出,
   /// 表示如下:
   /// AuthenticatorClient =MD5(ClientID+7 字节的0
   ///  +shared secret+timestamp)
   ///  Shared secret 由中国电信服务器端与ICP客户端
   ///  事先商定,timestamp格式为:MMDDHHMMSS,
   ///  即月日时分秒,10位。
   /// </summary>
   byte[] _AuthenticatorClient;
   string _Password;
   /// <summary>
   /// 登录类型(0=发送短消息, 1=接收短消息,
   /// 2=转发收发短消息,其他保留)
   /// </summary>
   uint _LoginMode;
   /// <summary>
   /// 时间戳的明文,由客户端产生,
   /// 格式为MMDDHHMMSS,即月日时分秒,
   /// 10位数字的整型,右对齐
   /// </summary>
   uint _TimeStamp;
   /// <summary>
   /// 客户端支持的版本号(高位4bit表示主版本号,
   /// 低位4bit表示次版本号)
   /// </summary>
   byte _Version=0x13;

   SMGP_MSG_Header header;
   byte[] initValue;
   byte[] body;

   public SMGP_MSG_Login(uint sequenceid)
   {
    header=new SMGP.StateObject.SMGP_MSG_Header(SMGP.StateObject.SMGP_COMMAND_ID.SMGP_Login);    
    header.SequenceID =sequenceid;   
    header.PacketLength =(uint)(this.BodyLength
     +SMGP_MSG_Header.HeaderLength);    
    body=new byte[this.BodyLength];
   }
   /// <summary>
   /// 解析消息包
   /// </summary>
   /// <param name="bs"></param>
   public SMGP_MSG_Login(byte[] bs)
   {
    initValue=new byte[bs.Length];
    bs.CopyTo(initValue,0);
//    byte [] tmp=new byte[SMGP_MSG_Header.HeaderLength];
//    for(int i=0;i<tmp.Length;i++)
//    {
//     tmp[i]=initValue[i];
//    }
    body=new byte[initValue.Length-SMGP_MSG_Header.HeaderLength];
    for(int i=0;i<body.Length;i++)
    {
     body[i]=initValue[SMGP_MSG_Header.HeaderLength+i];
    }
    this._ClientID=Encoding.ASCII.GetString(body,0,8);
    for(int i=0;i<16;i++)
    {
     this._AuthenticatorClient[i]=body[8+i];
    }
    
    this._LoginMode=(uint)body[24];
    this._TimeStamp=BIConvert.Bytes2UInt(body,25);
    this._Version=body[29];

   }
   /// <summary>
   /// 返回当前对象的字节数组对象
   /// </summary>
   /// <returns></returns>
   public byte[] ToBytes()
   {
    byte[] reVal=new Byte[SMGP.StateObject.SMGP_MSG_Header.HeaderLength
     +this.BodyLength];    
    header.ToBytes().CopyTo(reVal,0);       //消息头
    //加入登录编号
    BIConvert.Str2byteLeft(this._ClientID,8).CopyTo(reVal,SMGP_MSG_Header.HeaderLength);
    //加入登录密码
    //BIConvert.Str2byte(this._AuthenticatorClient,16).CopyTo(reVal,SMGP_MSG_Header.HeaderLength+8);
    //this._AuthenticatorClient.CopyTo(reVal,SMGP_MSG_Header.HeaderLength+8);
    this._AuthenticatorClient=this.getmd5Authenticator();
    this._AuthenticatorClient.CopyTo(reVal,SMGP_MSG_Header.HeaderLength+8);
    //加入登录类型
    byte [] l;
    l=new byte [1];
    l[0]=(byte)this._LoginMode;
    l.CopyTo(reVal,SMGP_MSG_Header.HeaderLength+8+16);
    //加入时间戳
    BIConvert.Int2Bytes(this._TimeStamp).CopyTo(reVal,SMGP_MSG_Header.HeaderLength+8+16+1);
    //加入版本号
    l=new byte[1];
    l[0]=this._Version;
    l.CopyTo(reVal,SMGP_MSG_Header.HeaderLength+8+16+1+4);
    return reVal;
   }
   /// <summary>
   /// 登录用户密码
   /// </summary>
   public string Password
   {
    get
    {
     return this._Password;
    }
    set
    {
     this._Password=value;
    }
   }
   /// <summary>
   /// 消息体长度
   /// </summary>
   public int BodyLength
   {
    get
    {
     return (8+16+1+4+1);
    }
   }
   /// <summary>
   /// SPCP编号或者SMGW编号
   /// 编号规则参见总册第六6节
   /// </summary>
   public string ClientID
   {
    get
    {
     return this._ClientID;
    }
    set
    {
     this._ClientID=value;
    }
   }
   /// <summary>
   /// 客户端密码
   /// 用于鉴别客户端的接入请求  。
   /// 其值通过单向MD5 hash计算得出,
   /// 表示如下:
   /// AuthenticatorClient =MD5(ClientID+7 字节的0
   ///  +shared secret+timestamp)
   ///  Shared secret 由中国电信服务器端与ICP客户端
   ///  事先商定,timestamp格式为:MMDDHHMMSS,
   ///  即月日时分秒,10位。
   /// </summary>
   public byte[] AuthenticatorClient
   {
    get
    {
     return this._AuthenticatorClient;
    }
//    set
//    {
//     this._AuthenticatorClient=value;
//    }
   }
   /// <summary>
   /// 登录类型(0=发送短消息, 1=接收短消息,
   /// 2=转发收发短消息,其他保留)
   /// </summary>
   public uint LoginMode
   {
    get
    {
     return this._LoginMode;
    }
    set
    {
     this._LoginMode=value;
    }
   }
   /// <summary>
   /// 时间戳的明文,由客户端产生,
   /// 格式为MMDDHHMMSS,即月日时分秒,
   /// 10位数字的整型,右对齐
   /// </summary>
   public uint TimeStamp
   {
    get
    {
     return this._TimeStamp;
    }
    set
    {
     this._TimeStamp=value;
    }
   }
   /// <summary>
   /// 客户端支持的版本号(高位4bit表示主版本号,
   /// 低位4bit表示次版本号)
   /// </summary>
   public byte Version
   {
    get
    {
     return this._Version;
    }
    set
    {
     this._Version=value;
    }
   }
   //
   /// <summary>
   /// 取得客户端Authenticator
   /// </summary>
   /// <returns></returns>
   private byte [] getmd5Authenticator()
   {
    byte [] buf=new byte[8+7+this._Password.Length+10];
    byte [] a_id=Encoding.ASCII.GetBytes(this._ClientID);
    byte [] a_0={0,0,0,0,0,0,0};
    byte [] a_p=Encoding.ASCII.GetBytes(this._Password);
    SMSAPI.Time.SMSDateTime sdt=new SMSAPI.Time.SMSDateTime();
    byte [] a_t=Encoding.ASCII.GetBytes(sdt.getThisMinShortDateStr()+sdt.getThisLongTimeStr());
    a_id.CopyTo(buf,0);
    a_0.CopyTo(buf,8);
    a_p.CopyTo(buf,15);
    a_t.CopyTo(buf,15+a_p.Length);

    MD5CryptoServiceProvider md5=new MD5CryptoServiceProvider();
    return md5.ComputeHash(buf);
   }

  }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值