using System.Security.Cryptography;
using System.IO;
static string EncrypData;
static byte[] bytes =System.Text.ASCIIEncoding.ASCII.GetBytes("AbCdEfGh");
private void buttonEncrypt_Click(object sender, EventArgs e)
{
try
{
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(cryptoStream);
writer.Write(textBox1.Text);
writer.Flush();
cryptoStream.FlushFinalBlock();
writer.Flush();
EncrypData = Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
StreamWriter sw = new StreamWriter(@"C:\MyEncryption.DAT");
sw.Write(EncrypData);
sw.Flush();
sw.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void buttonDecrypt_Click(object sender, EventArgs e)
{
try
{
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(EncrypData));
CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);
StreamReader reader = new StreamReader(cryptoStream);
StreamWriter sw = new StreamWriter(@"C:\MyDecrypt.DAT");
sw.Write(reader.ReadToEnd());
sw.Flush();
sw.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
No comments:
Post a Comment