using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Collections.Generic;
private static void DoEncryption2(string original)
using (var myRijndael = new RijndaelManaged())
myRijndael.GenerateKey();
var encrypted = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV);
Console.WriteLine("Original: {0}\r\nEncrypted {1}", original, string.Join(" ", encrypted.Select(b => b.ToString("X2"))));
Console.WriteLine("Error: {0}", e.Message);
private static void DoEncryption()
string original = "Here is some data to encrypt!";
using (RijndaelManaged myRijndael = new RijndaelManaged())
myRijndael.GenerateKey();
byte[] encrypted = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV);
Console.WriteLine("Original: {0}", original);
Console.WriteLine("Error: {0}", e.Message);
static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
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");
using (RijndaelManaged rijAlg = new RijndaelManaged())
ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
swEncrypt.Write(plainText);
encrypted = msEncrypt.ToArray();
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));
var options = new ParallelOptions{MaxDegreeOfParallelism = 4};
Parallel.For(0, 10, options, i => DoEncryption());
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);