using System.Security.Cryptography;
public static void Main ()
CryptLib _crypt = new CryptLib ();
string plainText = "This is the text to be encrypted";
String iv = CryptLib.GenerateRandomIV (16);
string key = CryptLib.getHashSha256("my secret key", 256);
String cypherText = _crypt.encrypt (plainText, key, iv);
Console.WriteLine ("iv="+iv);
Console.WriteLine ("key=" + key);
Console.WriteLine("Cypher text=" + cypherText);
Console.WriteLine ("Plain text =" + _crypt.decrypt (cypherText, key, iv));
RijndaelManaged _rcipher;
byte[] _key, _pwd, _ivBytes, _iv;
private enum EncryptMode {ENCRYPT, DECRYPT};
static readonly char[] CharacterMatrixForRandomIVStringGeneration = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'
internal static string GenerateRandomIV(int length) {
char[] _iv = new char[length];
byte[] randomBytes = new byte[length];
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider()) {
rng.GetBytes(randomBytes);
for (int i = 0; i < _iv.Length; i++) {
int ptr = randomBytes[i] % CharacterMatrixForRandomIVStringGeneration.Length;
_iv[i] = CharacterMatrixForRandomIVStringGeneration[ptr];
_enc = new UTF8Encoding();
_rcipher = new RijndaelManaged();
_rcipher.Mode = CipherMode.CBC;
_rcipher.Padding = PaddingMode.PKCS7;
_rcipher.BlockSize = 128;
private String encryptDecrypt (string _inputText, string _encryptionKey, EncryptMode _mode, string _initVector)
_pwd = Encoding.UTF8.GetBytes(_encryptionKey);
_ivBytes = Encoding.UTF8.GetBytes (_initVector);
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
var key = new Rfc2898DeriveBytes(_pwd, saltBytes, 1000);
_key = key.GetBytes(_rcipher.KeySize / 8);
_iv = key.GetBytes(_rcipher.BlockSize / 8);
if (_mode.Equals (EncryptMode.ENCRYPT)) {
byte[] plainText = _rcipher.CreateEncryptor().TransformFinalBlock(_enc.GetBytes(_inputText) , 0, _inputText.Length);
_out = Convert.ToBase64String(plainText);
if (_mode.Equals (EncryptMode.DECRYPT)) {
byte[] plainText = _rcipher.CreateDecryptor().TransformFinalBlock(Convert.FromBase64String(_inputText), 0, Convert.FromBase64String(_inputText).Length);
_out = _enc.GetString(plainText);
public string encrypt (string _plainText, string _key, string _initVector)
return encryptDecrypt(_plainText, _key, EncryptMode.ENCRYPT, _initVector);
public string decrypt(string _encryptedText, string _key, string _initVector)
return encryptDecrypt(_encryptedText, _key, EncryptMode.DECRYPT, _initVector);
public static string getHashSha256(string text, int length)
byte[] bytes = Encoding.UTF8.GetBytes(text);
SHA256Managed hashstring = new SHA256Managed();
byte[] hash = hashstring.ComputeHash(bytes);
string hashString = string.Empty;
hashString += String.Format("{0:x2}", x);
if (length > hashString.Length)
return hashString.Substring (0, length);