Ejemplo n.º 1
0
        /// <summary>
        /// Encrypts the specified source stream.
        /// </summary>
        /// <param name="sourceStream">The source stream.</param>
        /// <param name="nodeId"></param>
        /// <returns>Encrypted stream</returns>
        public Stream Encrypt(Stream sourceStream, string nodeId)
        {
            var alg = createAlgorithm();
            // try custom encryption
            var args = new CryptoEventArgs(sourceStream, nodeId, alg);

            Owner.DoBeforeEncrypt(args);
            // if no custom encryption then do default
            if (!args.Done)
            {
                if (string.IsNullOrEmpty(Owner.Password))
                {
                    sourceStream.CopyTo(args.DestStream);
                }
                else
                {
                    using (var ms = new MemoryStream())
                        using (var cryStream = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write))
                        {
                            sourceStream.CopyTo(cryStream);
                            cryStream.Close();
                            var bytes = ms.ToArray();
                            args.DestStream.Write(bytes, 0, bytes.Length);
                        }
                }
            }
            args.DestStream.Seek(0, SeekOrigin.Begin);
            Owner.DoAfterEncrypt(args);
            return(args.DestStream);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Decrypts the specified source stream.
        /// </summary>
        /// <param name="sourceStream">The source stream.</param>
        /// <param name="nodeId"></param>
        /// <returns>Decrypted stream</returns>
        public Stream Decrypt(Stream sourceStream, string nodeId)
        {
            var alg  = createAlgorithm();
            var args = new CryptoEventArgs(sourceStream, nodeId, alg);

            // try custom decryption
            Owner.DoBeforeDecrypt(args);
            // if no custom decryption then do default
            if (!args.Done)
            {
                if (string.IsNullOrEmpty(Owner.Password))
                {
                    sourceStream.CopyTo(args.DestStream);
                }
                else
                {
                    using (var cryStream = new CryptoStream(sourceStream, alg.CreateDecryptor(), CryptoStreamMode.Read))
                    {
                        cryStream.CopyTo(args.DestStream);
                        sourceStream.Close();
                    }
                }
            }
            args.DestStream.Seek(0, SeekOrigin.Begin);
            Owner.DoAfterDecrypt(args);
            return(args.DestStream);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Encrypts the specified source stream.
 /// </summary>
 /// <param name="sourceStream">The source stream.</param>
 /// <param name="nodeId"></param>
 /// <returns>Encrypted stream</returns>
 public Stream Encrypt(Stream sourceStream, string nodeId)
 {
     var alg = createAlgorithm();
     // try custom encryption
     var args = new CryptoEventArgs(sourceStream, nodeId, alg);
     Owner.DoBeforeEncrypt(args);
     // if no custom encryption then do default
     if (!args.Done)
     {
         if (string.IsNullOrEmpty(Owner.Password))
         {
             sourceStream.CopyTo(args.DestStream);
         }
         else
         {
             using (var ms = new MemoryStream())
             using (var cryStream = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write))
             {
                 sourceStream.CopyTo(cryStream);
                 cryStream.Close();
                 var bytes = ms.ToArray();
                 args.DestStream.Write(bytes, 0, bytes.Length);
             }
         }
     }
     args.DestStream.Seek(0, SeekOrigin.Begin);
     Owner.DoAfterEncrypt(args);
     return args.DestStream;
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Decrypts the specified source stream.
 /// </summary>
 /// <param name="sourceStream">The source stream.</param>
 /// <param name="nodeId"></param>
 /// <returns>Decrypted stream</returns>
 public Stream Decrypt(Stream sourceStream, string nodeId)
 {
     var alg = createAlgorithm();
     var args = new CryptoEventArgs(sourceStream, nodeId, alg);
     // try custom decryption
     Owner.DoBeforeDecrypt(args);
     // if no custom decryption then do default
     if (!args.Done)
     {
         if (string.IsNullOrEmpty(Owner.Password))
         {
             sourceStream.CopyTo(args.DestStream);
         }
         else
         {
             using (var cryStream = new CryptoStream(sourceStream, alg.CreateDecryptor(), CryptoStreamMode.Read))
             {
                 cryStream.CopyTo(args.DestStream);
                 sourceStream.Close();
             }
         }
     }
     args.DestStream.Seek(0, SeekOrigin.Begin);
     Owner.DoAfterDecrypt(args);
     return args.DestStream;
 }
Ejemplo n.º 5
0
 internal void DoBeforeEncrypt(CryptoEventArgs e)
 {
     var handler = BeforeEncrypt;
     if (handler != null) handler(this, e);
 }
Ejemplo n.º 6
0
 internal void DoAfterEncrypt(CryptoEventArgs e)
 {
     var handler = AfterEncrypt;
     if (handler != null) handler(this, e);
 }