예제 #1
0
 internal static byte[] Encrypt(this SymmetricAlgorithm alg, byte[] plainText, int blockSizeMultipler = 1)
 {
     using (ICryptoTransform encryptor = alg.CreateEncryptor())
     {
         return encryptor.Transform(plainText, blockSizeMultipler);
     }
 }
    /// <summary>
    ///   <para>Encrypts binary data, using specified symmetric algorithm.</para>
    /// </summary>
    /// <param name="self">Symmetric algorithm to be used for encryption.</param>
    /// <param name="source">Binary data to be encrypted.</param>
    /// <returns>Encrypted binary data.</returns>
    /// <exception cref="ArgumentNullException">If either <paramref name="self"/> or <paramref name="source"/> is a <c>null</c> reference.</exception>
    /// <seealso cref="Encrypt(SymmetricAlgorithm, Stream, bool)"/>
    public static byte[] Encrypt(this SymmetricAlgorithm self, byte[] source)
    {
      Assertion.NotNull(self);
      Assertion.NotNull(source);

      return self.CreateEncryptor().TransformFinalBlock(source, 0, source.Length);
    }
 public static byte[] Encrypt(this SymmetricAlgorithm algorithm, byte[] inputBuffer)
 {
     using (var encryptor = algorithm.CreateEncryptor())
     {
         return encryptor.TransformFinalBlock(inputBuffer);
     }
 }
 /// <summary>
 /// Encryptors the specified algorithm.
 /// </summary>
 /// <param name="algorithm">The algorithm.</param>
 /// <param name="data">The data.</param>
 /// <param name="offset">The offset.</param>
 /// <param name="count">The count.</param>
 /// <returns></returns>
 public static byte[] Encryptor(this SymmetricAlgorithm algorithm, byte[] data, int offset, int count)
 {
     MemoryStream ms = new MemoryStream();
     System.Security.Cryptography.CryptoStream cryptoStream = new CryptoStream(ms, algorithm.CreateEncryptor(), CryptoStreamMode.Write);
     cryptoStream.Write(data, offset, count);
     cryptoStream.FlushFinalBlock();
     return ms.ToArray();
 }
예제 #5
0
        public static Byte[] Encrypt(this AesManaged aes, Byte[] source, Byte[] key, Byte[] iv)
        {
            var bytesKey = ResizeBytesArray(key, aes.Key.Length);
            var bytesIv = ResizeBytesArray(iv, aes.IV.Length);

            using (var msOut = new MemoryStream())
            using (var cryptStreem = new CryptoStream(msOut, aes.CreateEncryptor(bytesKey, bytesIv), CryptoStreamMode.Write))
            {
                cryptStreem.Write(source, 0, source.Length);
                cryptStreem.FlushFinalBlock();
                return msOut.ToArray();
            }
        }
예제 #6
0
        public static string EncryptString(this SymmetricAlgorithm algorithm, string plaintext)
        {
            if (string.IsNullOrEmpty(plaintext))
                throw new ArgumentNullException("plaintext");

            var encryptor = algorithm.CreateEncryptor();

            var ms = new MemoryStream();
            var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);

            using (var writer = new StreamWriter(cs))
            {
                writer.Write(plaintext);
            }

            return Convert.ToBase64String(ms.ToArray());
        }
예제 #7
0
        /// <summary>
        /// Encrypts input stream onto output stream for the given parameters.
        /// </summary>
        /// <param name="algorithm"><see cref="SymmetricAlgorithm"/> to use for encryption.</param>
        /// <param name="source">Source stream that contains data to encrypt.</param>
        /// <param name="destination">Destination stream used to hold encrypted data.</param>
        /// <param name="key">The secret key to use for the symmetric algorithm.</param>
        /// <param name="iv">The initialization vector to use for the symmetric algorithm.</param>
        public static void Encrypt(this SymmetricAlgorithm algorithm, Stream source, Stream destination, byte[] key, byte[] iv)
        {
            byte[] buffer = new byte[Standard.BufferSize];
            CryptoStream encodeStream = new CryptoStream(destination, algorithm.CreateEncryptor(key, iv), CryptoStreamMode.Write);
            int read;

            // Encrypts data onto output stream.
            read = source.Read(buffer, 0, Standard.BufferSize);

            while (read > 0)
            {
                encodeStream.Write(buffer, 0, read);
                read = source.Read(buffer, 0, Standard.BufferSize);
            }

            encodeStream.FlushFinalBlock();
        }
        public static byte[] Encrypt(this SymmetricAlgorithm algorithm, byte[] input)
        {
            // Create memory stream to which to write encrypted data.
            using (var memoryStream = new MemoryStream())
            {
                //// Ensure that crypto operations are thread-safe.
                //lock (algorithm)
                //{
                using (var transform = algorithm.CreateEncryptor())
                {
                    var cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);

                    // Write input data to crypto stream.
                    cryptoStream.Write(input, 0, input.Length);
                    cryptoStream.FlushFinalBlock();

                    // Return encrypted data.
                    return memoryStream.ToArray();
                }
                //}
            }
        }
예제 #9
0
파일: Cipher.cs 프로젝트: avs009/gsf
        /// <summary>Encrypts input stream onto output stream for the given parameters.</summary>
        public static void Encrypt(this SymmetricAlgorithm algorithm, Stream inStream, Stream outStream, byte[] key, byte[] IV)
        {
            // This is the root encryption function. Eventually, all the symmetric algorithm based encryption
            // functions perform their actual encryption here.
            byte[] rgbKey = algorithm.GetLegalKey(key);
            byte[] rgbIV = algorithm.GetLegalIV(IV);
            byte[] buffer = new byte[BufferSize];
            CryptoStream encodeStream = new CryptoStream(outStream, algorithm.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
            int read;

            // Encrypts data onto output stream.
            read = inStream.Read(buffer, 0, BufferSize);

            while (read > 0)
            {
                encodeStream.Write(buffer, 0, read);
                read = inStream.Read(buffer, 0, BufferSize);
            }

            encodeStream.FlushFinalBlock();
        }
예제 #10
0
 public static ICryptoTransform CreateEncryptor(this SymmetricAlgorithm symmetricAlgorithm, CryptoKey cryptoKey, CryptoIV cryptoIV)
 {
     return symmetricAlgorithm.CreateEncryptor(cryptoKey.Value, cryptoIV.Value);
 }