Esempio n. 1
0
        public void EncryptPackets(SymmetricAlgorithm keyedAlgorithm, params OpenPgpPacket[] packetsToEncrypt)
        {
            var transform   = new OpenPgpCfbTransform(keyedAlgorithm, encrypt: true);
            var msEncrypted = new MemoryStream();

            EncryptedStream = msEncrypted;
            var encryptor = new CryptoStream(msEncrypted, transform, CryptoStreamMode.Write);

            // HACK: Write the prefix bytes manually rather than do it all in the transform since we need it for the hash
            encryptor.Write(transform.PrefixBytes, 0, transform.PrefixBytes.Length);

            var hasher             = SHA1.Create();
            var hashContentsStream = new MemoryStream();
            var hashStream         = new CryptoStream(hashContentsStream, hasher, CryptoStreamMode.Write);

            hashStream.Write(transform.PrefixBytes, 0, transform.PrefixBytes.Length);

            var currentPacketStream = new MemoryStream();

            foreach (var currentPacketToEncrypt in packetsToEncrypt)
            {
                currentPacketStream.SetLength(0);
                currentPacketToEncrypt.WriteTo(currentPacketStream);
                currentPacketStream.Position = 0;
                currentPacketStream.CopyTo(hashStream);
                currentPacketStream.Position = 0;
                currentPacketStream.CopyTo(encryptor);
            }

            var mdcPacket = new ModificationDetectionCodeOpenPgpPacket();

            mdcPacket.Header.Length = hasher.HashSize / 8;
            mdcPacket.Header.WriteTo(hashStream);

            hashStream.FlushFinalBlock();

            var hashContents = hasher.Hash;

            mdcPacket.HashValue = hashContents;
            mdcPacket.WriteTo(encryptor);

            // SECREVIEW: Emit MDC packet
            encryptor.FlushFinalBlock();
            msEncrypted.Position = 0;
        }
Esempio n. 2
0
        public void EncryptPackets(SymmetricAlgorithm keyedAlgorithm, params OpenPgpPacket[] packetsToEncrypt)
        {
            var transform = new OpenPgpCfbTransform(keyedAlgorithm, encrypt: true);
            var msEncrypted = new MemoryStream();
            EncryptedStream = msEncrypted;
            var encryptor = new CryptoStream(msEncrypted, transform, CryptoStreamMode.Write);

            // HACK: Write the prefix bytes manually rather than do it all in the transform since we need it for the hash
            encryptor.Write(transform.PrefixBytes, 0, transform.PrefixBytes.Length);

            var hasher = SHA1.Create();
            var hashContentsStream = new MemoryStream();
            var hashStream = new CryptoStream(hashContentsStream, hasher, CryptoStreamMode.Write);

            hashStream.Write(transform.PrefixBytes, 0, transform.PrefixBytes.Length);

            var currentPacketStream = new MemoryStream();

            foreach(var currentPacketToEncrypt in packetsToEncrypt) {
                currentPacketStream.SetLength(0);
                currentPacketToEncrypt.WriteTo(currentPacketStream);
                currentPacketStream.Position = 0;
                currentPacketStream.CopyTo(hashStream);
                currentPacketStream.Position = 0;
                currentPacketStream.CopyTo(encryptor);
            }

            var mdcPacket = new ModificationDetectionCodeOpenPgpPacket();
            mdcPacket.Header.Length = hasher.HashSize/8;
            mdcPacket.Header.WriteTo(hashStream);

            hashStream.FlushFinalBlock();

            var hashContents = hasher.Hash;

            mdcPacket.HashValue = hashContents;
            mdcPacket.WriteTo(encryptor);

            // SECREVIEW: Emit MDC packet
            encryptor.FlushFinalBlock();
            msEncrypted.Position = 0;
        }