GetBytes() публичный статический Метод

public static GetBytes ( Byte bytes, Int32 count, Int32 offset ) : Byte[]
bytes Byte
count System.Int32
offset System.Int32
Результат Byte[]
Пример #1
0
        private Byte[] Decrypt(Type symmetricAlgorithm, Byte[] symmetricKey, Byte[] cipherText)
        {
            Contract.Requires(symmetricAlgorithm != null);
            Contract.Requires(symmetricKey != null && symmetricKey.Length > 0);
            Contract.Requires(cipherText != null && cipherText.Length > 0);

            var algorithm = (SymmetricAlgorithm)Activator.CreateInstance(symmetricAlgorithm);
            var ivSize    = algorithm.BlockSize / 8;

            if (cipherText.Length < ivSize)
            {
                throw new ArgumentException("The cipherText specified is invalid and does not contain the initialization vector.");
            }

            algorithm.Key = symmetricKey;
            algorithm.IV  = CryptographyUtility.GetBytes(cipherText, ivSize);
            var cipher = CryptographyUtility.GetBytes(cipherText, cipherText.Length - ivSize, ivSize);

            using (var encryptedStream = new MemoryStream(cipher))
                using (var plainTextStream = new MemoryStream())
                    using (var decryptionStream = new CryptoStream(encryptedStream, algorithm.CreateDecryptor(algorithm.Key, algorithm.IV), CryptoStreamMode.Read))
                    {
                        decryptionStream.CopyTo(plainTextStream);

                        return(plainTextStream.ToArray());
                    }
        }
Пример #2
0
        private async Task DecryptAsync(Type symmetricAlgorithm, Byte[] symmetricKey, Stream cipherText, Stream plainText)
        {
            Contract.Requires(symmetricAlgorithm != null);
            Contract.Requires(symmetricKey != null && symmetricKey.Length > 0);
            Contract.Requires(cipherText != null);
            Contract.Requires(plainText != null);

            var algorithm = (SymmetricAlgorithm)Activator.CreateInstance(symmetricAlgorithm);
            var ivSize    = algorithm.BlockSize / 8;

            if (cipherText.Length < ivSize)
            {
                throw new ArgumentException("The cipherText specified is invalid and does not contain the initialization vector.");
            }

            cipherText.Position = 0;
            plainText.Position  = 0;

            algorithm.Key = symmetricKey;
            algorithm.IV  = CryptographyUtility.GetBytes(cipherText, ivSize);

            cipherText.Position = ivSize;

            var decryptionStream = new CryptoStream(cipherText, algorithm.CreateDecryptor(algorithm.Key, algorithm.IV), CryptoStreamMode.Read);

            await decryptionStream.CopyToAsync(plainText);

            cipherText.Position = 0;
            plainText.Position  = 0;
        }
Пример #3
0
        private Boolean CompareHash(Type hashAlgorithmType, Byte[] plainText, Byte[] hashedBytes, Int32 saltLength = 16)
        {
            var hashAlgorithm = Activator.CreateInstance(hashAlgorithmType, true) as HashAlgorithm;

            if (hashAlgorithm == null)
            {
                throw new InvalidOperationException(String.Format("Could not create instance of type {0}.", hashAlgorithmType));
            }

            var salt       = CryptographyUtility.GetBytes(hashedBytes, saltLength);
            var targetHash = CryptographyUtility.CombineBytes(salt, hashAlgorithm.ComputeHash(CryptographyUtility.CombineBytes(salt, plainText)));

            return(CryptographyUtility.CompareBytes(hashedBytes, targetHash));
        }