public byte[] TransformRaw(byte[] buffer, int outputLength)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            IAsymmetricBlockCipher cipher = new RsaEngine();

            cipher.Init(true, Key);
            if (outputLength > cipher.GetOutputBlockSize())
            {
                throw new ArgumentOutOfRangeException(nameof(outputLength), "Too large output length");
            }

            byte[] transformed = TransformInternal(buffer, cipher);
            if (transformed.Length == outputLength)
            {
                return(transformed);
            }

            byte[] truncated = new byte[outputLength];
            Array.Copy(transformed, transformed.Length - outputLength, truncated, 0, outputLength);
            return(truncated);
        }
Ejemplo n.º 2
0
        public static MemoryStream DecryptRsa(Stream inputStream, string pemKey)
        {
            AsymmetricKeyParameter keyParameter = ReadPem(pemKey);
            var engine = new RsaEngine();

            engine.Init(false, keyParameter);

            var outputStream    = new MemoryStream();
            int inputBlockSize  = engine.GetInputBlockSize();
            int outputBlockSize = engine.GetOutputBlockSize();

            byte[] inputBlock = new byte[inputBlockSize];
            while (inputStream.Read(inputBlock, 0, inputBlock.Length) > 0)
            {
                byte[] outputBlock = engine.ProcessBlock(inputBlock, 0, inputBlockSize);

                int requiredPadding = outputBlockSize - outputBlock.Length;
                if (requiredPadding > 0)
                {
                    byte[] paddedOutputBlock = new byte[outputBlockSize];
                    outputBlock.CopyTo(paddedOutputBlock, requiredPadding);
                    outputBlock = paddedOutputBlock;
                }

                outputStream.Write(outputBlock, 0, outputBlock.Length);
            }

            outputStream.Seek(0, SeekOrigin.Begin);
            return(outputStream);
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Decrypts a file with a provided decryption key.
        /// </summary>
        /// <param name="filePath">An encrypted file</param>
        /// <param name="key">The RSA key in PEM format</param>
        /// <exception cref="ArgumentNullException">When the argument filePath is null</exception>
        /// <exception cref="ArgumentNullException">When the argument keyPath is null</exception>
        /// <returns>A memory stream with the decrypted file</returns>
        public static MemoryStream DecryptRsa(string filePath, string key)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException(nameof(filePath));
            }

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

            AsymmetricKeyParameter keyParameter = GetKeyOrDefault(key);
            RsaEngine engine = new RsaEngine();

            engine.Init(false, keyParameter);

            MemoryStream outputStream = new MemoryStream();

            using (FileStream inputStream = File.OpenRead(filePath))
            {
                int    inputBlockSize  = engine.GetInputBlockSize();
                int    outputBlockSize = engine.GetOutputBlockSize();
                byte[] inputBlock      = new byte[inputBlockSize];
                while (inputStream.Read(inputBlock, 0, inputBlock.Length) > 0)
                {
                    byte[] outputBlock = engine.ProcessBlock(inputBlock, 0, inputBlockSize);

                    int requiredPadding = outputBlockSize - outputBlock.Length;
                    if (requiredPadding > 0)
                    {
                        byte[] paddedOutputBlock = new byte[outputBlockSize];
                        outputBlock.CopyTo(paddedOutputBlock, requiredPadding);
                        outputBlock = paddedOutputBlock;
                    }

                    outputStream.Write(outputBlock, 0, outputBlock.Length);
                }
            }

            outputStream.Seek(0, SeekOrigin.Begin);
            return(outputStream);
        }