Пример #1
0
        public byte[] DecryptData(byte[] data)
        {
            var packet = new EncryptedPacket()
            {
                Iv            = data.Take(16).ToArray(),
                Hmac          = data.Skip(16).Take(32).ToArray(),
                EncryptedData = data.Skip(48).ToArray()
            };

            return(AES.DecryptPacket(packet));
        }
Пример #2
0
        internal byte[] DecryptPacket(EncryptedPacket packet)
        {
            using (var hmac = new HMACSHA256(_sessionKey))
            {
                var hmacToCheck = hmac.ComputeHash(packet.EncryptedData);

                if (!Compare(packet.Hmac, hmacToCheck))
                {
                    throw new CryptographicException("HMAC for decryption does not match encrypted packet, data has been tamperd with");
                }
            }
            return(Decrypt(packet.EncryptedData, _sessionKey, packet.Iv));
        }
Пример #3
0
        internal EncryptedPacket EncryptData(byte[] data)
        {
            //Create new encrypted packet with random Iv
            var encryptedpacket = new EncryptedPacket()
            {
                Iv = GenerateRanomNumber(16)
            };

            //Encrypt data with AES
            encryptedpacket.EncryptedData = Encrypt(data, _sessionKey, encryptedpacket.Iv);

            //Generate HMAC using session key for data integrety check
            using (var hmac = new HMACSHA256(_sessionKey))
            {
                encryptedpacket.Hmac = hmac.ComputeHash(encryptedpacket.EncryptedData);
            }
            return(encryptedpacket);
        }