using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Security.Cryptography;
using System.IO;

public partial class Criptografia : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("String criptografada: " + Criptografar("0123456789", "Senha") + "<br/>");
    }

    private string Criptografar(string dados, string senha)
    {
        byte[] b = Encoding.UTF8.GetBytes(dados);
        byte[] pw = Encoding.UTF8.GetBytes(senha);

        RijndaelManaged rm = new RijndaelManaged();

        PasswordDeriveBytes pdb = new PasswordDeriveBytes(senha, new MD5CryptoServiceProvider().ComputeHash(pw));
        rm.Key = pdb.GetBytes(32);
        rm.IV = pdb.GetBytes(16);
        rm.BlockSize = 128;
        rm.Padding = PaddingMode.PKCS7;

        MemoryStream ms = new MemoryStream();

        CryptoStream cryptStream = new CryptoStream(ms, rm.CreateEncryptor(rm.Key, rm.IV),
            CryptoStreamMode.Write);
        cryptStream.Write(b, 0, b.Length);
        cryptStream.FlushFinalBlock();
        return System.Convert.ToBase64String(ms.ToArray());
    }

    void Page_Init(object sender, EventArgs e)
    {
        Page.ViewStateUserKey = Session.SessionID;
    }
}