Example #1
0
        public EncryptedMessage(byte[] authKey, byte[] plainData)
        {
            _authKey = authKey;
            using (var ms = new MemoryStream(plainData))
            {
                using (var br = new BinaryReader(ms))
                {
                    AuthKeyId = br.ReadInt64();
                    MsgKey = new BigInteger(br.ReadBytes(16));

                    // дешифруем эту дату
                    byte[] aesKey = CalculateAesKey(8, MsgKey.GetBytes());
                    byte[] aesIv = CalculateIV(8, MsgKey.GetBytes());

                    var aesIge = new Aes256IgeManaged(aesKey, aesIv);
                    Data = new EncryptedData(aesIge.Decrypt(br.ReadBytes(plainData.Length - 8 - 16)));
                }
            }
        }
Example #2
0
        /// <summary>
        /// Обработка DH параметров
        /// </summary>
        /// <param name="dhparams"></param>
        /// <returns></returns>
        private Combinator ProcessDhParams(Combinator dhparams)
        {
            var ea = dhparams.Get<byte[]>("encrypted_answer");

            // Обновим server nonce
            _serverNonce = dhparams.Get<BigInteger>("server_nonce");

            // Расшифровка строки
            var aes = new Aes256IgeManaged(CalculateTmpAesKey(_newNonce, _serverNonce),
                CalculateTmpAesIV(_newNonce, _serverNonce));

            byte[] answerWithHash = aes.Decrypt(ea);
            if (answerWithHash.Length % 16 != 0)
                throw new ArgumentException("Неверный ответ внутри сообщения");

            byte[] answer = answerWithHash.Skip(20).ToArray();

            return new Combinator(answer, "Server_DH_inner_data");
        }