SymmetricDecrypt() public static method

Decrypts using AES/CBC/PKCS7 with an input byte array and key, using the random IV prepended using AES/ECB/None
public static SymmetricDecrypt ( byte input, byte key ) : byte[]
input byte
key byte
return byte[]
Esempio n. 1
0
        /// <summary>
        /// Verifies and performs a symmetricdecrypt on the input using the given password as a key
        /// </summary>
        public static byte[] VerifyAndDecryptPassword(byte[] input, string password)
        {
            byte[] key, hash;
            using (SHA256 sha256 = SHA256Managed.Create())
            {
                byte[] password_bytes = Encoding.ASCII.GetBytes(password);
                key = sha256.ComputeHash(password_bytes);
            }
            using (HMACSHA1 hmac = new HMACSHA1(key))
            {
                hash = hmac.ComputeHash(input, 0, 32);
            }

            for (int i = 32; i < input.Length; i++)
            {
                if (input[i] != hash[i % 32])
                {
                    return(null);
                }
            }

            byte[] encrypted = new byte[32];
            Array.Copy(input, 0, encrypted, 0, 32);

            return(CryptoHelper.SymmetricDecrypt(encrypted, key));
        }
Esempio n. 2
0
            /// <summary>
            /// Processes the specified depot key by decrypting the data with the given depot encryption key, and then by decompressing the data.
            /// If the chunk has already been processed, this function does nothing.
            /// </summary>
            /// <param name="depotKey">The depot decryption key.</param>
            /// <exception cref="System.IO.InvalidDataException">Thrown if the processed data does not match the expected checksum given in it's chunk information.</exception>
            public void Process(byte[] depotKey)
            {
                if (depotKey == null)
                {
                    throw new ArgumentNullException(nameof(depotKey));
                }

                if (IsProcessed)
                {
                    return;
                }

                byte[] processedData = CryptoHelper.SymmetricDecrypt(Data, depotKey);

                if (processedData.Length > 1 && processedData[0] == 'V' && processedData[1] == 'Z')
                {
                    processedData = VZipUtil.Decompress(processedData);
                }
                else
                {
                    processedData = ZipUtil.Decompress(processedData);
                }

                byte[] dataCrc = CryptoHelper.AdlerHash(processedData);

                if (!dataCrc.SequenceEqual(ChunkInfo.Checksum))
                {
                    throw new InvalidDataException("Processed data checksum is incorrect! Downloaded depot chunk is corrupt or invalid/wrong depot key?");
                }

                Data        = processedData;
                IsProcessed = true;
            }
Esempio n. 3
0
        /// <summary>
        /// Attempts to decrypts file names with the given encryption key.
        /// </summary>
        /// <param name="encryptionKey">The encryption key.</param>
        /// <returns><c>true</c> if the file names were successfully decrypted; otherwise, <c>false</c>.</returns>
        public bool DecryptFilenames(byte[] encryptionKey)
        {
            if (!FilenamesEncrypted)
            {
                return(true);
            }

            foreach (var file in Files)
            {
                byte[] enc_filename = Convert.FromBase64String(file.FileName);
                byte[] filename;
                try
                {
                    filename = CryptoHelper.SymmetricDecrypt(enc_filename, encryptionKey);
                }
                catch (Exception)
                {
                    return(false);
                }

                file.FileName = Encoding.ASCII.GetString(filename).TrimEnd(new char[] { '\0' }).Replace(altDirChar, Path.DirectorySeparatorChar);
            }

            FilenamesEncrypted = false;
            return(true);
        }
Esempio n. 4
0
        /// <summary>
        /// Attempts to decrypts file names with the given encryption key.
        /// </summary>
        /// <param name="encryptionKey">The encryption key.</param>
        /// <returns><c>true</c> if the file names were successfully decrypted; otherwise, <c>false</c>.</returns>
        public bool DecryptFilenames(byte[] encryptionKey)
        {
            if (!FilenamesEncrypted)
            {
                return(true);
            }

            DebugLog.Assert(Files != null, nameof(DepotManifest), "Files was null when attempting to decrypt filenames.");

            foreach (var file in Files)
            {
                byte[] enc_filename = Convert.FromBase64String(file.FileName);
                byte[] filename;
                try
                {
                    filename = CryptoHelper.SymmetricDecrypt(enc_filename, encryptionKey);
                }
                catch (Exception)
                {
                    return(false);
                }

                file.FileName = Encoding.UTF8.GetString(filename).TrimEnd(new char[] { '\0' }).Replace(altDirChar, Path.DirectorySeparatorChar);
            }

            FilenamesEncrypted = false;
            return(true);
        }
Esempio n. 5
0
        /// <summary>
        /// Processes a chunk by decrypting and decompressing it.
        /// </summary>
        /// <param name="chunk">The chunk to process.</param>
        /// <param name="depotkey">The AES encryption key to use when decrypting the chunk.</param>
        /// <returns>The processed chunk.</returns>
        public static byte[] ProcessChunk(byte[] chunk, byte[] depotkey)
        {
            byte[] decrypted_chunk    = CryptoHelper.SymmetricDecrypt(chunk, depotkey);
            byte[] decompressed_chunk = ZipUtil.Decompress(decrypted_chunk);

            return(decompressed_chunk);
        }
Esempio n. 6
0
        public byte[] ProcessIncoming(byte[] data)
        {
            try
            {
                return(CryptoHelper.SymmetricDecrypt(data, sessionKey));
            }
            catch (CryptographicException ex)
            {
                DebugLog.WriteLine("NetFilterEncryption", "Unable to decrypt incoming packet: " + ex.Message);

                // rethrow as an IO exception so it's handled in the network thread
                throw new IOException("Unable to decrypt incoming packet", ex);
            }
        }
Esempio n. 7
0
            /// <summary>
            /// Processes the specified depot key by decrypting the data with the given depot encryption key, and then by decompressing the data.
            /// If the chunk has already been processed, this function does nothing.
            /// </summary>
            /// <param name="depotKey">The depot decryption key.</param>
            /// <exception cref="System.IO.InvalidDataException">Thrown if the processed data does not match the expected checksum given in it's chunk information.</exception>
            public void Process(byte[] depotKey)
            {
                if (IsProcessed)
                {
                    return;
                }

                byte[] processedData = CryptoHelper.SymmetricDecrypt(Data, depotKey);
                processedData = ZipUtil.Decompress(processedData);

                byte[] dataCrc = CryptoHelper.AdlerHash(processedData);

                if (!dataCrc.SequenceEqual(ChunkInfo.Checksum))
                {
                    throw new InvalidDataException("Processed data checksum is incorrect! Downloaded depot chunk is corrupt or invalid/wrong depot key?");
                }

                Data        = processedData;
                IsProcessed = true;
            }
Esempio n. 8
0
        /// <summary>
        /// Verifies and performs a symmetricdecrypt on the input using the given password as a key
        /// </summary>
        public static byte[]? VerifyAndDecryptPassword(byte[] input, string password)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            if (password == null)
            {
                throw new ArgumentNullException(nameof(password));
            }

            byte[] key, hash;
            using (var sha256 = SHA256.Create())
            {
                byte[] password_bytes = Encoding.UTF8.GetBytes(password);
                key = sha256.ComputeHash(password_bytes);
            }
            using (HMACSHA1 hmac = new HMACSHA1(key))
            {
                hash = hmac.ComputeHash(input, 0, 32);
            }

            for (int i = 32; i < input.Length; i++)
            {
                if (input[i] != hash[i % 32])
                {
                    return(null);
                }
            }

            byte[] encrypted = new byte[32];
            Array.Copy(input, encrypted, encrypted.Length);

            return(CryptoHelper.SymmetricDecrypt(encrypted, key));
        }
Esempio n. 9
0
            /// <summary>
            /// Processes the specified depot key by decrypting the data with the given depot encryption key, and then by decompressing the data.
            /// If the chunk has already been processed, this function does nothing.
            /// </summary>
            /// <param name="depotKey">The depot decryption key.</param>
            /// <exception cref="System.IO.InvalidDataException">Thrown if the processed data does not match the expected checksum given in it's chunk information.</exception>
            public void Process(byte[] depotKey)
            {
                if (depotKey == null)
                {
                    throw new ArgumentNullException(nameof(depotKey));
                }

                if (IsProcessed)
                {
                    return;
                }

                if (Data != null)
                {
                    byte[] processedData = CryptoHelper.SymmetricDecrypt(Data, depotKey);

                    if (processedData.Length > 1 && processedData[0] == 'V' && processedData[1] == 'Z')
                    {
                        processedData = VZipUtil.Decompress(processedData);
                    }
                    else
                    {
                        processedData = ZipUtil.Decompress(processedData);
                    }

                    byte[] dataCrc = CryptoHelper.AdlerHash(processedData);

                    if (!dataCrc.SequenceEqual(ChunkInfo.Checksum))
                    {
                        throw new InvalidDataException("Processed data checksum is incorrect! Downloaded depot chunk is corrupt or invalid/wrong depot key?");
                    }

                    Data        = processedData;
                    IsProcessed = true;
                }
                if (DataStream != null)
                {
                    Stream reProcessedData;
                    using (Stream processedData = CryptoHelper.SymmetricDecrypt(DataStream, depotKey))
                    {
                        int firstByte  = processedData.ReadByte();
                        int secondByte = processedData.ReadByte();
                        processedData.Position = 0;

                        if (firstByte == 86 && secondByte == 90)
                        {
                            reProcessedData = VZipUtil.Decompress(processedData);
                        }
                        else
                        {
                            reProcessedData = ZipUtil.Decompress(processedData);
                        }
                    }

                    byte[] dataCrc = CryptoHelper.AdlerHash(reProcessedData);

                    if (!dataCrc.SequenceEqual(ChunkInfo.Checksum))
                    {
                        throw new InvalidDataException("Processed data checksum is incorrect! Downloaded depot chunk is corrupt or invalid/wrong depot key?");
                    }

                    DataStream  = reProcessedData;
                    IsProcessed = true;
                }
            }
Esempio n. 10
0
 public byte[] ProcessIncoming(byte[] data)
 {
     return(CryptoHelper.SymmetricDecrypt(data, sessionKey));
 }