Skip to content

Instantly share code, notes, and snippets.

@ginxx009
Created June 21, 2018 16:22
Show Gist options
  • Save ginxx009/116d124e899e3c248c80c1d69ab90410 to your computer and use it in GitHub Desktop.
Save ginxx009/116d124e899e3c248c80c1d69ab90410 to your computer and use it in GitHub Desktop.
Encryption & Decryption MD5 C#
private void btnDecrypt_Click(object sender, EventArgs e)
{
byte[] data = Convert.FromBase64String(textBox4.Text); // decrypt the incrypted text
using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
{
byte[] keys = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(hash));
using (TripleDESCryptoServiceProvider tripDes = new TripleDESCryptoServiceProvider() { Key = keys, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 })
{
ICryptoTransform transform = tripDes.CreateDecryptor();
byte[] results = transform.TransformFinalBlock(data, 0, data.Length);
textBox4.Text = UTF8Encoding.UTF8.GetString(results);
}
}
}
private void btnEncrypt_Click(object sender, EventArgs e)
{
byte[] data = UTF8Encoding.UTF8.GetBytes(textBox3.Text);
using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
{
byte[] keys = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(hash));
using (TripleDESCryptoServiceProvider tripDes = new TripleDESCryptoServiceProvider() { Key = keys, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 })
{
ICryptoTransform transform = tripDes.CreateEncryptor();
byte[] results = transform.TransformFinalBlock(data, 0, data.Length);
textBox4.Text = Convert.ToBase64String(results, 0, results.Length);
}
}
}
@Rojuvaxan
Copy link

static string hash { get; set; } = "A!9HHhi%XjjYY4YP2@Nob009X";

@leokamal
Copy link

in file Encrypt.cs
line 2: use textBox4.Text instead of textBox3.Text

@Leopcampos
Copy link

Leopcampos commented Mar 15, 2023

public static string Encrypt(string value)
{
var md5 = new MD5CryptoServiceProvider();
var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(value));

        //converting the contents of the hash variable from byte[] to string
        var result = string.Empty;
        foreach (var item in hash)
        {
            result += item.ToString("X2"); //X2 -> HEXADECIMAL
        }
        return result.ToUpper();
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment