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);
        }
예제 #2
0
파일: TPUtil.cs 프로젝트: JKAnderson/TexPup
        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);
        }
예제 #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);
        }