public byte[] ComputeAESRound(AESDir aesDir, byte[] iv, byte[] input, byte[] keyBytes)
        {
            Guard.NotNull(new object[] { iv, input, keyBytes });

            switch (aesDir)
            {
            case AESDir.Encrypt:
                using (var aes = CreateAesManaged(iv, keyBytes))
                    using (var stream = new MemoryStream())
                    {
                        using (var encryptor = aes.CreateEncryptor())
                            using (var encrypt = new CryptoStream(stream, encryptor, CryptoStreamMode.Write))
                            {
                                encrypt.Write(input, 0, input.Length);
                                encrypt.FlushFinalBlock();
                            }
                        return(stream.ToArray());
                    }

            case AESDir.Decrpyt:
                using (var aes = CreateAesManaged(iv, keyBytes))
                    using (var stream = new MemoryStream())
                    {
                        using (var decryptor = aes.CreateDecryptor())
                            using (var decrypt = new CryptoStream(stream, decryptor, CryptoStreamMode.Write))
                            {
                                decrypt.Write(input, 0, input.Length);
                                decrypt.FlushFinalBlock();
                            }
                        return(stream.ToArray());
                    }
            }
            throw new InvalidOperationException(string.Format("{0} is not supported", aesDir));
        }
Exemplo n.º 2
0
        byte[] ComputeAESWithRounds(AESDir aesDir, IV16 iv, byte[] dataBytes, byte[] keyBytes, byte roundsExp, IVCache ivCache, LongRunningOperationContext context)
        {
            Guard.NotNull(new object[] { aesDir, iv, dataBytes, keyBytes, roundsExp, ivCache, context });
            var rounds     = 1u << roundsExp;
            var roundsToGo = rounds;

            byte[] inputData = dataBytes;
            byte[] aesResult = null;
            while (roundsToGo > 0)
            {
                byte[] currentIV =
                    aesDir == AESDir.Encrypt
                       ? ivCache.IVTable.Item2[roundsToGo - 1]
                       : ivCache.IVTable.Item2[ivCache.IVTable.Item2.Length - roundsToGo];

                aesResult = this._platform.ComputeAESRound(aesDir, currentIV, inputData, keyBytes);
                inputData = aesResult;

                roundsToGo--;                 // decrement before calculating the percentage or we'll be stuck at 99%

                // START encryptionProgress / Cancellation
                context.CancellationToken.ThrowIfCancellationRequested();
                decimal progressValue = (rounds - roundsToGo) / (decimal)(rounds);

                context.EncryptionProgress.Percent = (int)(progressValue * 100m);
                context.EncryptionProgress.Report(context.EncryptionProgress);
                // END encryptionProgress
            }
            return(aesResult);
        }