Beispiel #1
0
        private static byte[] CheckHmac(byte[] dataToDecrypt, byte[] salt, AesCryptoServiceProvider aes)
        {
            var hmac          = ByteHelpers.CreateSpecialByteArray(32);
            var encryptedData = ByteHelpers.CreateSpecialByteArray(dataToDecrypt.Length - 32);

            Buffer.BlockCopy(dataToDecrypt, 0, hmac, 0, 32);
            Buffer.BlockCopy(dataToDecrypt, 32, encryptedData, 0, dataToDecrypt.Length - 32);

            var newHmac = CreateHmac(salt, aes, encryptedData);

            if (!ByteHelpers.ByteArrayCompare(hmac, newHmac))
            {
                throw new CryptographicException("The authenticated message code doesn't match. \n\nThe message may have been corrupted or tampered with.");
            }

            return(encryptedData);
        }
Beispiel #2
0
        public static byte[] DecryptByteBlock(byte[] dataToDecrypt, byte[] password)
        {
            if (dataToDecrypt == null)
            {
                throw new ArgumentNullException("dataToDecrypt");
            }

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

            var aes = new Aes();

            var salt    = ByteHelpers.CreateSpecialByteArray(32);
            var message = ByteHelpers.CreateSpecialByteArray(dataToDecrypt.Length - 32);

            Buffer.BlockCopy(dataToDecrypt, 0, salt, 0, 32);
            Buffer.BlockCopy(dataToDecrypt, 32, message, 0, dataToDecrypt.Length - 32);

            var deCompressed = Compressor.Decompress(aes.Decrypt(message, password, salt, PBKDF2_ITERATIONS));

            return(deCompressed);
        }