private static bool Pack(ref byte[] packBuf, MMTLVHeader tlvHeader, byte[] data) { TLVPack pack = new TLVPack(0, 1); pack.addByte(1, 190); pack.AddInt(2, (int)tlvHeader.Uin); pack.AddWord(3, tlvHeader.CmdId); pack.AddInt(4, tlvHeader.ServerId.Length); pack.addByteArray(5, tlvHeader.ServerId, tlvHeader.ServerId.Length); pack.addByteArray(6, tlvHeader.DeviceId, 0x10); pack.AddWord(7, tlvHeader.CompressVersion); pack.AddWord(8, tlvHeader.CompressAlogrithm); pack.AddWord(9, tlvHeader.CryptAlgorithm); pack.AddInt(10, (int)tlvHeader.CompressLen); pack.AddInt(11, (int)tlvHeader.CompressedLen); pack.addByteArray(12, data, data.Length); pack.AddInt(13, tlvHeader.Ret); pack.addByte(14, 0xed); pack.AddInt(15, (int)tlvHeader.CertVersion); pack.addByte(0x10, 0xed); packBuf = new byte[pack.getUsedSize() + 2]; packBuf[0] = 190; packBuf[packBuf.Length - 1] = 0xed; int apiSize = 0; apiSize = pack.getUsedSize(); if (pack.CopyTo(packBuf, 1, ref apiSize) != 0) { Log.e("MMPack", "tlvPack.CopyTo failed"); return(false); } return(true); }
public static bool UnPack(byte[] packBuf, ref MMTLVHeader tlvHeader, ref byte[] cipherText) { if ((packBuf == null) || (packBuf.Length < 2)) { Log.e("MMPack", " UnPack failed: invlaide args"); return(false); } if ((190 != packBuf[0]) || (0xed != packBuf[packBuf.Length - 1])) { Log.e("MMPack", " UnPack failed: invalid args packBuf"); return(false); } TLVPack pack = new TLVPack(0, 1); if (pack.CopyFrom(packBuf, packBuf.Length - 2, 1) != 0) { return(false); } int apiVal = 0; if (pack.GetInt(2, ref apiVal) != 0) { Log.e("MMPack", " UnPack failed : uin"); return(false); } tlvHeader.Uin = (uint)apiVal; short aphVal = 0; if (pack.GetShort(3, ref aphVal) != 0) { Log.e("MMPack", " UnPack failed : cmdId"); return(false); } tlvHeader.CmdId = (ushort)aphVal; byte[] apoVal = null; if (pack.getByteArray(5, ref apoVal) != 0) { Log.e("MMPack", " UnPack failed : serverId"); return(false); } tlvHeader.ServerId = apoVal; byte[] buffer2 = null; if (pack.getByteArray(6, ref buffer2) != 0) { Log.e("MMPack", " UnPack failed : deviceId"); return(false); } tlvHeader.DeviceId = buffer2; short num3 = 0; if (pack.GetShort(7, ref num3) != 0) { Log.e("MMPack", " UnPack failed : compressVersion"); return(false); } tlvHeader.CompressVersion = num3; short num4 = 0; if (pack.GetShort(8, ref num4) != 0) { Log.e("MMPack", " UnPack failed : compressAlgorithm"); return(false); } tlvHeader.CompressAlogrithm = num4; short num5 = 0; if (pack.GetShort(9, ref num5) != 0) { Log.e("MMPack", " UnPack failed : cryptAlgorithm"); return(false); } tlvHeader.CryptAlgorithm = num5; uint apuVal = 0; if (pack.GetUInt(10, ref apuVal) != 0) { Log.e("MMPack", " UnPack failed : compressLen"); return(false); } tlvHeader.CompressLen = apuVal; uint num7 = 0; if (pack.GetUInt(11, ref num7) != 0) { Log.e("MMPack", " UnPack failed : compressedLen"); return(false); } tlvHeader.CompressedLen = num7; if (pack.getByteArray(12, ref cipherText) != 0) { Log.e("MMPack", " UnPack failed : cipherText"); return(false); } int num8 = 0; if (pack.GetInt(13, ref num8) != 0) { Log.e("MMPack", " UnPack failed : ret"); return(false); } tlvHeader.Ret = num8; uint num9 = 0; if (pack.GetUInt(15, ref num9) != 0) { Log.e("MMPack", " UnPack failed : certversion"); return(false); } tlvHeader.CertVersion = num9; return(true); }
public static bool DecodePackOld(ref byte[] decryptedData, byte[] packBuf, byte[] key, ref byte[] serverId, ref int ret) { try { if ((packBuf != null) && (packBuf.Length >= 2)) { MMTLVHeader tlvHeader = new MMTLVHeader(); byte[] cipherText = null; if (!UnPack(packBuf, ref tlvHeader, ref cipherText)) { Log.e("MMPack", " UnPack failed"); return(false); } ret = tlvHeader.Ret; // uin = tlvHeader.Uin; if (tlvHeader.Ret == -13) { Log.e("MMPack", "DecodPack failed : session timeout"); return(false); } if (tlvHeader.Ret != 0) { Log.e("MMPack", "DecodPack failed : retCode = " + tlvHeader.Ret); return(false); } serverId = tlvHeader.ServerId; byte[] destinationArray = null; if (tlvHeader.CryptAlgorithm == 5) { decryptedData = AES.Decrypt(cipherText, key); if (decryptedData == null) { Log.e("MMPack", "AES: AESDecrypt failed"); return(false); } destinationArray = decryptedData; } else if (tlvHeader.CryptAlgorithm == 0) { destinationArray = cipherText; } else { int outLen = cipherText.Length + 0x20; decryptedData = new byte[outLen]; if (DES.Using_DES(decryptedData, ref outLen, cipherText, cipherText.Length, key, key.Length, 2) == 0) { Log.e("MMPack", "DES: DESDecrypt failed"); return(false); } destinationArray = new byte[outLen]; Array.Copy(decryptedData, destinationArray, destinationArray.Length); } if (tlvHeader.CompressAlogrithm == 1) { if (!Zlib.Decompress(destinationArray, (int)tlvHeader.CompressLen, ref decryptedData)) { Log.e("MMPack", " Zlib Compress failed"); return(false); } return(true); } if (tlvHeader.CompressAlogrithm == 2) { decryptedData = destinationArray; return(true); } } return(false); } catch (Exception) { return(false); } }
public static bool EncodePack(SessionPack sessionPack) { try { byte[] inBuf = sessionPack.requestToByteArray(); byte[] key = sessionPack.getSessionKey(true); MMTLVHeader tlvHeader = new MMTLVHeader { Ret = (int)ConstantsProtocol.CLIENT_MIN_VERSION, Uin = (uint)SessionPackMgr.getAccount().getUin(), CmdId = (ushort)sessionPack.getMMFuncID(), ServerId = SessionPackMgr.getSeverID(), DeviceId = Util.StringToByteArray(Util.getDeviceUniqueId()), CompressLen = (uint)inBuf.Length, CompressVersion = 0x3e9 }; byte[] outBuf = null; //Log.e("MMPack", " ServerId:" + Util.byteToHexStr(tlvHeader.ServerId)); if (sessionPack.mNeedCompress) { tlvHeader.CompressAlogrithm = 1; if (!Zlib.Compress(inBuf, ref outBuf)) { Log.e("MMPack", " Zlib Compress failed"); return(false); } } else { tlvHeader.CompressAlogrithm = 2; outBuf = inBuf; } tlvHeader.CompressedLen = (uint)outBuf.Length; short mEncrypt = sessionPack.mEncrypt; byte[] encryptText = null; if ((key == null) || (key.Length <= 0)) { //if (MicroMsg.Common.Algorithm.RSA.RSAEncrypt(out encryptText, outBuf) != 0) //{ // Log.e("MMPack", " RSAEncrypt failed"); // return false; //} //RsaCertInfo new2 = sessionPack.getRsaCertInfo(); //if (new2 == null) //{ // Log.e("MMPack", "getRsaCertInfo failed"); // return false; //} tlvHeader.CertVersion = 99; //data = CdnRuntimeComponent.RSAEncrypt(outBuf, new2.strRsaKeyN, new2.strRsaKeyE); //Util.RsaEncrypt(out encryptText, outBuf, new2.strRsaKeyN, new2.strRsaKeyE); //Util.RSAEncrypt(out encryptText, outBuf, "DFE56EEE6506E5F9796B4F12C3A48121B84E548E9999D834E2C037E3CD276E9C4A2B1758C582A67F6D12895CE5525DDE51D0B92D32B8BE7B2C85827729C3571DCC14B581877BC634BCC7F9DA3825C97A25B341A64295098303C4B584EC579ECCA7C8B96782F65D650039EE7A0772C195DBEFC4488BDFB0B9A58C5C058E3AB04D", "010001"); encryptText = Util.RSAEncryptBlock(outBuf, "DFE56EEE6506E5F9796B4F12C3A48121B84E548E9999D834E2C037E3CD276E9C4A2B1758C582A67F6D12895CE5525DDE51D0B92D32B8BE7B2C85827729C3571DCC14B581877BC634BCC7F9DA3825C97A25B341A64295098303C4B584EC579ECCA7C8B96782F65D650039EE7A0772C195DBEFC4488BDFB0B9A58C5C058E3AB04D", "010001"); //MicroMsg.Common.Algorithm.RSA.RSAEncrypt(out data, outBuf); if ((encryptText == null) || (encryptText.Length <= 0)) { Log.e("MMPack", " RSAEncrypt failed"); return(false); } return(Pack(ref sessionPack.mCacheBodyBuffer, tlvHeader, encryptText)); } if (mEncrypt == 5) { tlvHeader.CryptAlgorithm = 5; encryptText = AES.Encrypt(outBuf, key); if (encryptText == null) { Log.e("MMPack", "AES: AESEncrypt failed"); return(false); } return(Pack(ref sessionPack.mCacheBodyBuffer, tlvHeader, encryptText)); } tlvHeader.CryptAlgorithm = 4; encryptText = DES.EncryptBytes(outBuf, key, 1); if (encryptText == null) { Log.e("MMPack", "DES: DESEncrypt failed"); return(false); } return(Pack(ref sessionPack.mCacheBodyBuffer, tlvHeader, encryptText)); } catch (Exception exception) { Log.e("MMPack", exception.Message); return(false); } }