C # PDU encoding 8bit encryption and decryption functions and sms gsm encryption and decryption use a good class

Source: Internet
Author: User
Tags sca

Address: http://www.wangchao.net.cn/bbsdetail_50338.html

 

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Namespace smstest
{
/// <Summary>
/// By popcorn 2004.5.
// Cnpopcorn@hotmail.com
/// </Summary>
Public class cntext
{
Public cntext ()
{
}
/// <Summary>
/// Encoding format
/// </Summary>
Public Enum gsmcode
{
Bit7 = 0,
Bit8 = 1,
Ucs2 = 2
}
/// <Summary>
/// Decode the entire Short Message
/// </Summary>
/// <Param name = "S"> information to be decoded </param>
/// <Param name = "phone"> decoded phone number </param>
/// <Param name = "text"> decoded text message content </param>
/// <Param name = "sendtime"> SMS timestamp </param>
/// <Param name = "code"> encoding method </param>
/// <Returns> success returns true </returns>
Static public bool decodingmsg (string S, ref string phone, ref string text, ref datetime sendtime, ref gsmcode code, ref string SCA)
{
Try
{
// Short Message Center
Int ilength = int. parse (S. substring (0, 2), system. Globalization. numberstyles. allowhexspecifier );
If (ilength> 0)
{
If (S. substring (2, 2) = "91 ")
{
SCA + = "+ ";
Ilength --;
}
For (INT I = 0; I <ilength * 2; I + = 2)
{
SCA + = S. substring (5 + I, 1 );
SCA + = S. substring (4 + I, 1 );
}
If (SCA. endswith ("F") SCA = SCA. Remove (SCA. Length-1, 1 );
}
S = S. Remove (0, ilength * 2 + 6 );
// Sender's number
Ilength = int. parse (S. substring (0, 2), system. Globalization. numberstyles. allowhexspecifier );
If (S. substring (2, 2) = "91 ")
{
Phone = "+ ";
}
If (ilength % 2 = 1) ilength ++;
For (INT I = 0; I <ilength; I + = 2)
{
Phone + = S. substring (5 + I, 1 );
Phone + = S. substring (4 + I, 1 );
}
If (phone. endswith ("F") Phone = phone. Remove (phone. Length-1, 1 );
S = S. Remove (0, ilength + 6 );
// Encoding method
If (S. substring (0, 2) = "08 ")
Code = gsmcode. ucs2;
Else if (S. substring (0, 2) = "00 ")
Code = gsmcode. bit7;
Else
Code = gsmcode. bit8;
S = S. Remove (0, 2 );
// Timestamp
Sendtime = new datetime (Int. parse ("20" + S. substring (1, 1) + S. substring (0, 1 )),
Int. parse (S. substring (3, 1) + S. substring (2, 1 )),
Int. parse (S. substring (5, 1) + S. substring (4, 1 )),
Int. parse (S. substring (7, 1) + S. substring (6, 1 )),
Int. parse (S. substring (9, 1) + S. substring (8, 1 )),
Int. parse (S. substring (11, 1) + S. substring (10, 1 )));
S = S. Remove (0, 16 );
// Received information
If (code = gsmcode. bit7)
{
TEXT = decodingbit7 (s );
}
Else if (code = gsmcode. ucs2)
{
TEXT = decodingucs2 (s );
}
Else
{
TEXT = decodingbit8 (s );
}
Return true;
}
Catch
{
Return false;
}
}
/// <Summary>
/// Encode the short message center
/// </Summary>
/// <Param name = "S"> number to be encoded </param>
/// <Returns> encoded number </returns>
Static Public String encodingsca (string S)
{
Stringbuilder sb = new stringbuilder ();
If (S. Length = 0)
{
SB. append ("00 ");
Return sb. tostring ();
}
If (S. startswith ("+ "))
{
SB. append ("91"); // number in international format (Add '+' in front ')
S = S. Remove (0, 1 );
}
Else
{
SB. append ("C8 ");
}
If (S. Length % 2 = 1) S + = "F ";
For (INT I = 0; I <S. length; I + = 2)
{
SB. append (S. substring (I + 1, 1 ));
SB. append (S. substring (I, 1 ));
}
String Len = (sb. Length/2). tostring ("X2 ");
Return Len + sb. tostring ();
}
/// <Summary>
/// Encode the phone number
/// </Summary>
/// <Param name = "mobileno"> phone number to be encoded </param>
/// <Returns> encoded phone number </returns>
Static Public String encodingnumber (string inclueno)
{
Stringbuilder sb = new stringbuilder ();
If (inclueno. startswith ("+ "))
{
SB. append ("91 ");
Required Eno = required Eno. Remove (0, 1 );
}
Else
{
SB. append ("C8 ");
}
String Len = inclueno. length. tostring ("X2 ");
If (then Eno. Length % 2 = 1) Then Eno + = "F ";
For (INT I = 0; I <inclueno. length; I + = 2)
{
SB. append (mobileno. substring (I + 1, 1 ));
SB. append (mobileno. substring (I, 1 ));
}
Return Len + sb. tostring ();
}
/// <Summary>
/// Use 7-bit for encoding
/// </Summary>
/// <Param name = "S"> English string to be encoded </param>
/// <Returns> information length and encoded string </returns>
Static Public String encodingbit7 (string S)
{
Int ileft = 0;
String sreturn = "";
Stringbuilder sb = new stringbuilder ();
For (INT I = 0; I <S. length; I ++)
{
// Obtain the minimum three bits of the Count value of the source string
Int ichar = I & 7;
Byte bsrc = (byte) Char. parse (S. substring (I, 1 ));
// Process each byte of the source string
If (ichar = 0)
{
// The first byte in the group, which is only saved and used for processing the next byte
Ileft = (INT) Char. parse (S. substring (I, 1 ));
}
Else
{
// Add the right part of the other bytes in the group to the residual data to obtain a target encoded byte.
Sreturn = (bsrc <(8-ichar) | ileft). tostring ("X4 ");
// Save the remaining left part of the byte as residual data
Ileft = bsrc> ichar;
// Modify the pointer and count pdst ++ of the target string;
SB. append (sreturn. substring (2, 2 ));
}
}
SB. append (sreturn. substring (0, 2 ));
Return (sb. Length/2). tostring ("X2") + sb. tostring ();
}
/// <Summary>
/// Decodes 7-bit encoding
/// </Summary>
/// <Param name = "S"> string to be decoded </param>
/// <Returns> decoded English string </returns>
Static Public String decodingbit7 (string S)
{
Int ibyte = 0;
Int ileft = 0;
// Divides the source data into a group of seven bytes and decompress the data into eight bytes.
// Loop the processing process until the source data is processed
// If the group contains less than 7 bytes, it can be processed correctly.
System. Text. stringbuilder sb = new system. Text. stringbuilder ();
For (INT I = 0; I <S. length; I + = 2)
{
Byte bsrc = byte. parse (S. substring (I, 2), system. Globalization. numberstyles. allowhexspecifier );
// Add the right part of the Source byte to the residual data, remove the highest bit, and obtain a target decoded byte.
SB. append (bsrc <ibyte) | ileft) & 0x7f). tostring ("X2 "));
// Save the remaining left part of the byte as residual data
Ileft = bsrc> (7-ibyte );
// Modify the Byte Count value
Ibyte ++;
// To the last byte of a group
If (ibyte = 7)
{
// Obtain an extra target decoded byte
SB. append (ileft. tostring ("X2 "));
// Initialize the byte numbers and residual data in the group
Ibyte = 0;
Ileft = 0;
}
}
String sreturn = sb. tostring ();
Byte [] Buf = new byte [sreturn. Length/2];
For (INT I = 0; I <sreturn. length; I + = 2)
{
Buf [I/2] = byte. parse (sreturn. substring (I, 2), system. Globalization. numberstyles. allowhexspecifier );
}
Return System. Text. encoding. ASCII. getstring (BUF );
}
/// <Summary>
/// Use 8-bit for encoding
/// </Summary>
/// <Param name = "S"> string to be encoded </param>
/// <Returns> information length and encoded string </returns>
Static Public String encodingbit8 (string S)
{
Stringbuilder sb = new stringbuilder ();
Byte [] Buf = encoding. ASCII. getbytes (s );
SB. append (BUF. length. tostring ("X2 "));
For (INT I = 0; I <Buf. length; I ++)
{
SB. append (BUF [I]. tostring ("X2 "));
}
Return sb. tostring ();
}
/// <Summary>
/// Use 8-bit for decoding
/// </Summary>
/// <Param name = "S"> string to be decoded </param>
/// <Returns> decoded string </returns>
Static Public String decodingbit8 (string S)
{
Byte [] Buf = new byte [S. Length/2];
Stringbuilder sb = new stringbuilder ();
For (INT I = 0; I <S. length; I + = 2)
{
Buf [I/2] = byte. parse (S. substring (I, 2), system. Globalization. numberstyles. allowhexspecifier );
}
Return encoding. ASCII. getstring (BUF );
}
/// <Summary>
/// Ucs2 encoding of Chinese Short Message
/// </Summary>
/// <Param name = "S"> Chinese character string to be encoded </param>
/// <Returns> information length and encoded string </returns>
Static Public String encodingucs2 (string S)
{
Stringbuilder sb = new stringbuilder ();
Byte [] Buf = encoding. Unicode. getbytes (s );
SB. append (BUF. length. tostring ("X2 "));
For (INT I = 0; I <Buf. length; I + = 2)
{
SB. append (BUF [I + 1]. tostring ("X2 "));
SB. append (BUF [I]. tostring ("X2 "));
}
Return sb. tostring ();
}
/// <Summary>
/// Chinese Short Message ucs2 Decoding
/// </Summary>
/// <Param name = "S"> information to be decoded </param>
/// <Returns> decoded Chinese string </returns>
Static Public String decodingucs2 (string S)
{
Byte [] Buf = new byte [S. Length];
For (INT I = 0; I <S. length; I + = 4)
{
Buf [I/2] = byte. parse (S. substring (2 + I, 2), system. Globalization. numberstyles. allowhexspecifier );
Buf [I/2 + 1] = byte. parse (S. substring (I, 2), system. Globalization. numberstyles. allowhexspecifier );
}
Return encoding. Unicode. getstring (BUF );
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.