First create class : encrypt_decrypt.cs
inside:
then 2 buttons encrypt and decrypt
inside:
using System; using System.Security.Cryptography; using System.Text; namespace RestWebinarSample { class Encryptor { public static string IV = "1a1a1a1a1a1a1a1a"; public static string Key = "1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a13"; public static string Encrypt(string decrypted) { byte[] textbytes = ASCIIEncoding.ASCII.GetBytes(decrypted); AesCryptoServiceProvider endec = new AesCryptoServiceProvider(); endec.BlockSize = 128; endec.KeySize = 256; endec.IV = ASCIIEncoding.ASCII.GetBytes(IV); endec.Key = ASCIIEncoding.ASCII.GetBytes(Key); endec.Padding = PaddingMode.PKCS7; endec.Mode = CipherMode.CBC; ICryptoTransform icrypt = endec.CreateEncryptor(endec.Key, endec.IV); byte[] enc = icrypt.TransformFinalBlock(textbytes, 0, textbytes.Length); icrypt.Dispose(); return Convert.ToBase64String(enc); } public static string Decrypted(string encrypted) { byte[] textbytes = Convert.FromBase64String(encrypted); AesCryptoServiceProvider endec = new AesCryptoServiceProvider(); endec.BlockSize = 128; endec.KeySize = 256; endec.IV = ASCIIEncoding.ASCII.GetBytes(IV); endec.Key = ASCIIEncoding.ASCII.GetBytes(Key); endec.Padding = PaddingMode.PKCS7; endec.Mode = CipherMode.CBC; ICryptoTransform icrypt = endec.CreateDecryptor(endec.Key, endec.IV); byte[] enc = icrypt.TransformFinalBlock(textbytes, 0, textbytes.Length); icrypt.Dispose(); return System.Text.ASCIIEncoding.ASCII.GetString(enc); } } }
then 2 buttons encrypt and decrypt
private void encrypt_Click(object sender, EventArgs e) { Directory.CreateDirectory("data\\"); var sw = new StreamWriter("data\\" + "data.ls"); string enctxt = Encryptor.Encrypt("willbeencrypted"); sw.WriteLine(enctxt); sw.Close(); } private void decrypt_Click(object sender, EventArgs e) { StreamReader sr = new StreamReader(Application.StartupPath + "\\data\\" + "data.ls"); string line = sr.ReadLine(); MessageBox.Show(Encryptor.Decrypted(Convert.ToString(line))); }