English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Comprehensive analysis of Java pdu short message decoding

Long SMS not verified, those interested can try it

Rewritten based on the Python method

/**
* PDU SMS parsing
*
*
* @param pduPayload
* @return
*/
public static String retrieveSMSInfo(byte[] pduPayload) throws UnsupportedEncodingException {
int startPos = 3;
//#Originator address
int mRP_OA_len = pduPayload[startPos];
byte[] mRP_OA = new byte[mRP_OA_len];
System.arraycopy(pduPayload, startPos + 1, mRP_OA, 0, mRP_OA_len);
startPos = startPos + 1 + mRP_OA_len;
int mTPDU_len = pduPayload[startPos];
//#BIT No. 7 6 5 4 3 2 1 0 
//#uplink TP-RP TP-UDHI TP-SPR TP-VPF TP-RD TP-MTI 
//#downlink TP-RP TP-UDHI TP-SRI TP-MMS TP-MTI 
byte TP_Header = pduPayload[startPos + 1];
byte TP_Msg_Ref = pduPayload[startPos + 2];
int TP_UDHI = (TP_Header >> 6) & 1; //# Whether the SMS content includes protocol header information, 0 does not include, 1 Include (long SMS, push SMS)
int TP_VPF = (TP_Header >> 3) & 3; //# Whether to include the validity period byte, 0 does not include, others include 
// #00 indicates no validity period, TP-VP set to 00. 
// #10indicates relative format, TP-VP usage1byte. 
// #01indicates incremented format, TP-VP usage7byte. 
// #11indicates absolute format, TP-VP usage7Byte
int TP_MMS = (TP_Header >> 2) & 1;//# TP-MMS(TP-More-Message-to-Send):1 The SMS Center has no more messages to send
startPos = startPos + 3;
//# Opposite Number
byte smsNumberLen = pduPayload[startPos];
int mTP_DA_len = (smsNumberLen + 1)}} / 2 + 1;
byte[] mTP_DA = new byte[mTP_DA_len];
System.arraycopy(pduPayload, startPos + 1, mTP_DA, 0, mTP_DA_len * 1);
byte mTP_DA_format = mTP_DA[0];
byte[] smsNumberRaw = new byte[mTP_DA.length - 1];
System.arraycopy(mTP_DA, 1, smsNumberRaw, 0, mTP_DA.length - 1);
String smsNumber = "";
int j = 0;
for (int i = 0; i < smsNumberLen; i++) {
if ((i & 1) == 0) {
smsNumber = smsNumber + (int) (smsNumberRaw[j] & 0xF);
} else {
smsNumber = smsNumber + (int) ((smsNumberRaw[j] & 0x0FF) >> 4);
j++;
}
}
startPos = startPos + 1 + mTP_DA_len;
byte mTP_PID = pduPayload[startPos];
byte mTP_DCS = pduPayload[startPos] + 1];//#“00”表示使用7位编码,设置为“02”使用8位编码,设置为“08”使用UCS2编码。
startPos = startPos + 2;
if (TP_VPF == 2) {
startPos = startPos + 1;
} else if (TP_VPF == 1 || TP_VPF == 3) {
startPos = startPos + 7;
}
//# 长短信:内容前面需要增加6个字段 
//# 1、 字节一:包头长度,固定填写0x05; 
//# 2、 字节二:包头类型标识,固定填写0x00,表示长短信; 
//# 3、 字节三:子包长度,固定填写0x03,表示后面三个字节的长度; 
//# 4、 字节四到字节六:包内容: 
//# a) 字节四:长消息参考号,每个SP给每个用户发送的每条参考号都应该不同,可以从0开始,每次加1,最大255,便于同一个终端对同一个SP的消息的不同的长短信进行识别; 
//# b) 字节五:本条长消息的的总消息数,从1到255,一般取值应该大于2; 
//# c) 字节六:本条消息在长消息中的位置或序号,从1到255,第一条为1,第二条为2,最后一条等于第四字节的值。 
//# 例子: 
//# 05 00 03 00 02 01 
//# 05 00 03 00 02 02 
int smsPayloadLen = pduPayload[startPos];
startPos = startPos + 1;
String smsContent = "";
if (TP_UDHI == 1) {
//#长短信--未验证 可能需要转无符号
byte smsTotal = pduPayload[startPos + 4];
byte smsIdx = pduPayload[startPos + 5];
startPos = startPos + 6;
smsContent = "长短信(" + byteToHex(smsIdx) + ""/"" + byteToHex(smsTotal) + "}";
smsContent = new String(smsContent.getBytes("gbk"));
smsPayloadLen = smsPayloadLen - 6;
}
byte[] smsPayload = new byte[pduPayload.length - startPos];
System.arraycopy(pduPayload, startPos, smsPayload, 0, pduPayload.length - startPos);
if (mTP_DCS == 0) {
//#7位编码--已验证
smsPayloadLen = (smsPayloadLen * 7 + 7)}} / 8;
int asciiData = 0;
int lastByteRemain = 0;
for (int i = 0; i < smsPayloadLen; i++) {
asciiData = asciiData + ((smsPayload[i] & 0x0FF) << lastByteRemain);
smsContent = smsContent + (char) ((asciiData & 0x0FF) & 0x7f);
asciiData = asciiData >> 7;
lastByteRemain = lastByteRemain + 1;
if (lastByteRemain >= 7) {
smsContent = smsContent + (char) ((asciiData & 0x0FF) & 0x7f);
asciiData = asciiData >> 7;
lastByteRemain = lastByteRemain - 7;
}
}
} else if (mTP_DCS == 8) {
//# UCS-2 --Verified, can be parsed normally
for (int i = 0; i < smsPayloadLen; i = i + 2) {
int cc1 = (smsPayload[i] & 0x0FF) * 256;
int cc2 = smsPayload[i + 1] & 0x0FF;
smsContent = smsContent + (char) (cc1 + cc2);
}
}
return smsNumber + " + smsContent;
}

The above is a comprehensive analysis of Java pdu SMS decoding introduced by the editor to everyone, hoping it will be helpful to everyone. If you have any questions, please leave a message, and the editor will reply to everyone in time. Here we also thank everyone for their support of the Yelling Tutorial website!

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)

You May Also Like