Exemplo n.º 1
0
        public void Decrypt()
        {
            var encryptedKeyXml = GetEncryptedKey();
            var encryptedKey    = new EncryptedKey();

            encryptedKey.LoadXml(encryptedKeyXml);

            byte[] symmetricKey       = RsaOaepSha256.Decrypt(encryptedKey.CipherData.CipherValue, Certificate);
            var    symmetricAlgorithm = new AesGcm {
                Key = symmetricKey
            };

            foreach (var attachment in Attachments)
            {
                attachment.Stream.Position = 0;
                var nonce = new byte[AesGcm.NonceSize / 8];
                attachment.Stream.Read(nonce, 0, nonce.Length);
                symmetricAlgorithm.Nonce = nonce;

                var decryptedStream = new MemoryStream();
                var cryptoStream    = new CryptoStream(attachment.Stream, symmetricAlgorithm.CreateDecryptor(), CryptoStreamMode.Read);
                cryptoStream.CopyTo(decryptedStream);
                if (!cryptoStream.HasFlushedFinalBlock)
                {
                    cryptoStream.FlushFinalBlock();
                }
                attachment.Stream = decryptedStream;
            }
        }
Exemplo n.º 2
0
        public void Encrypt()
        {
            var encryptionAlgorithm = new AesGcm {
                KeySize = 128
            };

            encryptionAlgorithm.GenerateKey();

            byte[] encryptedSymmetricKey = RsaOaepSha256.Encrypt(encryptionAlgorithm.Key, PublicKeyInAsn1Format);

            var encryptedKey = new EncryptedKey
            {
                Id = "ek-" + Guid.NewGuid(),
                EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncRSAOAEPUrl),
                CipherData       = new CipherData
                {
                    CipherValue = encryptedSymmetricKey
                }
            };

            var encryptedDataList = new List <EncryptedData>();

            foreach (Attachment attachment in Attachments)
            {
                attachment.Stream.Position = 0;
                Stream encryptedStream = new MemoryStream();
                encryptedStream.Write(encryptionAlgorithm.IV, 0, encryptionAlgorithm.IV.Length);

                var cryptoStream = new CryptoStream(encryptedStream, encryptionAlgorithm.CreateEncryptor(), CryptoStreamMode.Write);
                attachment.Stream.CopyTo(cryptoStream);
                cryptoStream.FlushFinalBlock();
                attachment.Stream = encryptedStream;

                var encryptedData = new EncryptedData
                {
                    Id               = "ed-" + Guid.NewGuid(),
                    Type             = "http://docs.oasis-open.org/wss/oasis-wss-SwAProfile-1.1#Attachment-Content-Only",
                    EncryptionMethod = new EncryptionMethod("http://www.w3.org/2009/xmlenc11#aes128-gcm"),
                    CipherData       = new CipherData
                    {
                        CipherReference = new CipherReference("cid:" + attachment.ContentId)
                    }
                };
                encryptedData.KeyInfo.AddClause(new SecurityTokenReference(encryptedKey.Id));
                encryptedData.CipherData.CipherReference.TransformChain.Add(new AttachmentCiphertextTransform());

                encryptedDataList.Add(encryptedData);

                encryptedKey.ReferenceList.Add(new DataReference(encryptedData.Id));
            }

            var securityXml = GetSecurity() ?? CreateSecurity();

            foreach (var encryptedData in encryptedDataList)
            {
                Insert(encryptedData.GetXml(), securityXml);
            }

            Insert(encryptedKey.GetXml(), securityXml);
        }