Beispiel #1
0
        /// <summary>
        /// Шифрует байтовый массив
        /// </summary>
        /// <param name="key">Сессионный ключ</param>
        /// <param name="sourceBytes">Шифруемый массив байтов</param>
        /// <returns>Зашифрованный массив байтов</returns>
        public static byte[] Gost28147Encrypt(Gost28147 key, byte[] sourceBytes)
        {
            int currentPosition = 0;

            byte[] targetBytes      = new byte[1024];
            int    sourceByteLength = sourceBytes.Length;

            // Создаем шифратор для ГОСТ.
            CPCryptoAPITransform cryptoTransform = (CPCryptoAPITransform)key.CreateEncryptor();

            // Размер блока считанных байт.
            int inputBlockSize = cryptoTransform.InputBlockSize;

            try
            {
                // Если возможна обработка нескольких блоков:
                if (cryptoTransform.CanTransformMultipleBlocks)
                {
                    int numBytesRead = 0;
                    while (sourceByteLength - currentPosition >= inputBlockSize)
                    {
                        // Преобразуем байты начиная с currentPosition в массиве
                        // sourceBytes, записывая результат в массив targetBytes.
                        numBytesRead = cryptoTransform.TransformBlock(
                            sourceBytes, currentPosition,
                            inputBlockSize, targetBytes,
                            currentPosition);
                        currentPosition += numBytesRead;
                    }
                    // Преобразуем последний блок.
                    byte[] finalBytes = cryptoTransform.TransformFinalBlock(
                        sourceBytes, currentPosition,
                        sourceByteLength - currentPosition);

                    // Записываем последний зашифрованный блок
                    // в массив targetBytes.
                    finalBytes.CopyTo(targetBytes, currentPosition);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Caught unexpected exception:" + ex.ToString());
            }
            // Определяем, может ли CPCryptoAPITransform использоваться повторно.
            if (!cryptoTransform.CanReuseTransform)
            {
                // Освобождаем занятые ресурсы.
                cryptoTransform.Clear();
            }
            // Убираем неиспользуемые байты из массива.
            return(Helper.TrimArray(targetBytes, sourceByteLength));
        }
Beispiel #2
0
        public byte[] Encrypt(byte[] data)
        {
            int currentPosition = 0;

            byte[] targetBytes      = new byte[1024];
            int    sourceByteLength = data.Length;
            CPCryptoAPITransform cryptoTransform = (CPCryptoAPITransform)symKey.CreateEncryptor();
            int inputBlockSize  = cryptoTransform.InputBlockSize;
            int outputBlockSize = cryptoTransform.OutputBlockSize;

            try
            {
                if (cryptoTransform.CanTransformMultipleBlocks)
                {
                    int numBytesRead = 0;
                    while (sourceByteLength - currentPosition >= inputBlockSize)
                    {
                        numBytesRead = cryptoTransform.TransformBlock(
                            data, currentPosition,
                            inputBlockSize, targetBytes,
                            currentPosition);
                        currentPosition += numBytesRead;
                    }
                    // Преобразуем последний блок.
                    byte[] finalBytes = cryptoTransform.TransformFinalBlock(
                        data, currentPosition,
                        sourceByteLength - currentPosition);

                    // Записываем последний зашифрованный блок
                    // в массив targetBytes.
                    finalBytes.CopyTo(targetBytes, currentPosition);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Caught unexpected exception:" + ex.ToString());
            }
            // Определяем, может ли CPCryptoAPITransform использоваться повторно.
            if (!cryptoTransform.CanReuseTransform)
            {
                // Освобождаем занятые ресурсы.
                cryptoTransform.Clear();
            }
            // Убираем неиспользуемые байты из массива.
            return(TrimArray(targetBytes));
        }