Exemplo n.º 1
0
        private void WriteFirstEncryptedBlock(MemoryStream bufferedOutputStream)
        {
            // Create the encryptor that will be used to transform the data. This will use the key and IV
            // that were generated when the aes object was created, and will only be used for this stream.
            this.cryptoTransform = this.aes.CreateEncryptor();

            RSAPKCS1KeyExchangeFormatter keyFormatter = new RSAPKCS1KeyExchangeFormatter(this.rsa);

            byte[] keyEncrypted = keyFormatter.CreateKeyExchange(this.aes.Key, this.aes.GetType());

            EncryptedFileHeader header = new EncryptedFileHeader
            {
                EncryptedKey          = keyEncrypted,
                IV                    = this.aes.IV,
                CertificateThumbprint = AdapterBase.HexToBytes(this.encryptionCertificate.Thumbprint)
            };

            short padding;

            if (this.Mode == EncryptionMode.Encrypt)
            {
                header.OriginalFileLength  = this.sourceFileSize;
                header.EncryptedFileLength = CalculateEncryptedFileSize(this.sourceFileSize, out padding);
                header.PaddingLength       = padding;
            }
            else
            {
                header.EncryptedFileLength = this.sourceFileSize;
                header.OriginalFileLength  = CalculateDecryptedFileSize(this.sourceFileSize, out padding);
                header.PaddingLength       = padding;
            }

            header.WriteToStream(bufferedOutputStream);

            this.firstBlockTransformed = true;
        }