.NET Fiddle

CSharp · Console

/Program.cs

using System;
using System.IO;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;

public class Program
{
	private static void DoEncryption2(string original)
	{
		try
		{
			using (var myRijndael = new RijndaelManaged())
			{
				myRijndael.GenerateKey();
				myRijndael.GenerateIV();
				// Encrypt the string to an array of bytes. 
				var encrypted = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV);
				//Display the original data and the decrypted data.
				Console.WriteLine("Original:   {0}\r\nEncrypted {1}", original, string.Join(" ", encrypted.Select(b => b.ToString("X2"))));
			}
		}
		catch (Exception e)
		{
			Console.WriteLine("Error: {0}", e.Message);
		}
	}

	private static void DoEncryption()
	{
		try
		{
			string original = "Here is some data to encrypt!";
			// Create a new instance of the RijndaelManaged 
			// class.  This generates a new key and initialization  
			// vector (IV). 
			using (RijndaelManaged myRijndael = new RijndaelManaged())
			{
				myRijndael.GenerateKey();
				myRijndael.GenerateIV();
				// Encrypt the string to an array of bytes. 
				byte[] encrypted = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV);
				//Display the original data and the decrypted data.
				Console.WriteLine("Original:   {0}", original);
			}
		}
		catch (Exception e)
		{
			Console.WriteLine("Error: {0}", e.Message);
		}
	}

	static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
	{
		// Check arguments. 
		if (plainText == null || plainText.Length <= 0)
			throw new ArgumentNullException("plainText");
		if (Key == null || Key.Length <= 0)
			throw new ArgumentNullException("Key");
		if (IV == null || IV.Length <= 0)
			throw new ArgumentNullException("IV");
		byte[] encrypted;
		// Create an RijndaelManaged object 
		// with the specified key and IV. 
		using (RijndaelManaged rijAlg = new RijndaelManaged())
		{
			rijAlg.Key = Key;
			rijAlg.IV = IV;
			// Create a decryptor to perform the stream transform.
			ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
			// Create the streams used for encryption. 
			using (MemoryStream msEncrypt = new MemoryStream())
			{
				using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
				{
					using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
					{
						//Write all data to the stream.
						swEncrypt.Write(plainText);
					}

					encrypted = msEncrypt.ToArray();
				}
			}
		}

		// Return the encrypted bytes from the memory stream. 
		return encrypted;
	}

	public static void Main()
	{
		var tasks = new List<Task>();
		tasks.Add(new Task(DoEncryption));
		tasks.Add(new Task(DoEncryption));
		tasks.Add(new Task(DoEncryption));
		tasks.Add(new Task(DoEncryption));
		tasks.Add(new Task(DoEncryption));
		Task.WhenAll(tasks);
		// or
		var options = new ParallelOptions{MaxDegreeOfParallelism = 4};
		Parallel.For(0, 10, options, i => DoEncryption());
		// or
		var list = new List<string>
               {
                  "Here is some hjdata to encrypt!",
                  "Here is sghjome data to encrypt!",
                  "Here is somfghe data to encrypt!",
                  "Here is somfghe data to encrypt!",
                  "Here is sohjkme data to encrypt!",
                  "Here is somghje data to encrypt!",
                  "Here is somfgheh data to encrypt!",
                  "Here is somefgh data to encrypt!",
                  "Here is sfghfgome data to encrypt!",
                  "Here is somefghfgh data to encrypt!",
                  "Here is shome data to encrypt!",
                  "Here is some data to encrypt!",
                  "Here is sfghfghfghome data to encrypt!",
                  "Here is some data to encrypt!",
                  "Here is somfghe data to encrypt!"};
					  
		Parallel.ForEach(list, options, DoEncryption2);
	}
}
;