Ejemplo n.º 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)));
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Создание вектора AES
        /// </summary>
        /// <param name="newNonce"></param>
        /// <param name="serverNonce"></param>
        /// <returns></returns>
        private byte[] CalculateTmpAesIV(BigInteger newNonce, BigInteger serverNonce)
        {
            //tmp_aes_iv := substr (SHA1(server_nonce + new_nonce), 12, 8) + SHA1(new_nonce + new_nonce) + substr (new_nonce, 0, 4);

            using (var ms = new MemoryStream())
            using (var bw = new BinaryWriter(ms))
            {
                SHA1 sha1 = SHA1.Create();

                bw.Write(serverNonce.GetBytes());
                bw.Write(newNonce.GetBytes());

                byte[] hash1 = sha1.ComputeHash(ms.ToArray());

                ms.SetLength(0);
                bw.Write(newNonce.GetBytes());
                bw.Write(newNonce.GetBytes());

                byte[] hash2 = sha1.ComputeHash(ms.ToArray());

                ms.SetLength(0);
                bw.Write(hash1, 12, 8);
                bw.Write(hash2);
                bw.Write(newNonce.GetBytes(), 0, 4);

                return ms.ToArray();
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Создание ключа AES
        /// </summary>
        /// <returns></returns>
        private byte[] CalculateTmpAesKey(BigInteger newNonce, BigInteger serverNonce)
        {
            //tmp_aes_key := SHA1(new_nonce + server_nonce) + substr (SHA1(server_nonce + new_nonce), 0, 12);        

            using (var ms = new MemoryStream())
            using (var bw = new BinaryWriter(ms))
            {
                SHA1 sha1 = SHA1.Create();

                bw.Write(newNonce.GetBytes());
                bw.Write(serverNonce.GetBytes());

                byte[] sumSha = sha1.ComputeHash(ms.ToArray());

                ms.SetLength(0);
                bw.Write(serverNonce.GetBytes());
                bw.Write(newNonce.GetBytes());

                byte[] reverseSumSha = sha1.ComputeHash(ms.ToArray());

                ms.SetLength(0);
                bw.Write(sumSha);
                bw.Write(reverseSumSha, 0, 12);

                return ms.ToArray();
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     Формирование соли по схеме substr(new_nonce, 0, 8) XOR substr(server_nonce, 0, 8)
        /// </summary>
        /// <param name="newNonce"></param>
        /// <param name="serverNonce"></param>
        /// <returns></returns>
        private long CalculateInitialSalt(BigInteger newNonce, BigInteger serverNonce)
        {
            var nn = new byte[8];
            var sn = new byte[8];

            Array.Copy(newNonce.GetBytes(), nn, 8);
            Array.Copy(serverNonce.GetBytes(), sn, 8);

            return BitConverter.ToInt32(Aes256IgeManaged.XOR(nn, sn), 0);
        }