示例#1
0
 public override byte[] ToBytes()
 {
     return(TLUtils.Combine(
                TLUtils.SignatureToBytes(Signature),
                Id.ToBytes(),
                AccessHash.ToBytes()));
 }
示例#2
0
        public static TLString EncryptMessage(TLObject decryptedMessage, TLEncryptedChatCommon chat)
        {
            var random = new Random();

            var key            = chat.Key.Data;
            var keyHash        = Utils.ComputeSHA1(key);
            var keyFingerprint = new TLLong(BitConverter.ToInt64(keyHash, 12));
            var decryptedBytes = decryptedMessage.ToBytes();
            var bytes          = Combine(BitConverter.GetBytes(decryptedBytes.Length), decryptedBytes);
            var sha1Hash       = Utils.ComputeSHA1(bytes);
            var msgKey         = sha1Hash.SubArray(sha1Hash.Length - 16, 16);

            var padding      = (bytes.Length % 16 == 0) ? 0 : (16 - (bytes.Length % 16));
            var paddingBytes = new byte[padding];

            random.NextBytes(paddingBytes);
            var bytesWithPadding = Combine(bytes, paddingBytes);

            var x      = 0;
            var sha1_a = Utils.ComputeSHA1(Combine(msgKey, key.SubArray(x, 32)));
            var sha1_b = Utils.ComputeSHA1(Combine(key.SubArray(32 + x, 16), msgKey, key.SubArray(48 + x, 16)));
            var sha1_c = Utils.ComputeSHA1(Combine(key.SubArray(64 + x, 32), msgKey));
            var sha1_d = Utils.ComputeSHA1(Combine(msgKey, key.SubArray(96 + x, 32)));

            var aesKey = Combine(sha1_a.SubArray(0, 8), sha1_b.SubArray(8, 12), sha1_c.SubArray(4, 12));
            var aesIV  = Combine(sha1_a.SubArray(8, 12), sha1_b.SubArray(0, 8), sha1_c.SubArray(16, 4), sha1_d.SubArray(0, 8));

            var encryptedBytes = Utils.AesIge(bytesWithPadding, aesKey, aesIV, true);

            var resultBytes = Combine(keyFingerprint.ToBytes(), msgKey, encryptedBytes);

            return(TLString.FromBigEndianData(resultBytes));
        }