Skip to content

Instantly share code, notes, and snippets.

@ginxx009
Last active August 20, 2025 05:44
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#
public string Decrypt(string ciphertext)
{
try
{
byte[] data = Convert.FromBase64String(ciphertext);
using var aes = Aes.Create();
aes.Key = _key;
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
// Extract IV from the first 16 bytes
byte[] iv = data.Take(16).ToArray();
aes.IV = iv;
using var ms = new MemoryStream();
using (var cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(data, 16, data.Length - 16); // Skip IV
}
return Encoding.UTF8.GetString(ms.ToArray());
}
catch (Exception ex)
{
throw new CryptographicException("Decryption failed.", ex);
}
}
public string Encrypt(string plaintext)
{
try
{
using var aes = Aes.Create();
aes.Key = _key;
aes.GenerateIV(); // Random IV for each encryption
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
using var ms = new MemoryStream();
ms.Write(aes.IV, 0, aes.IV.Length); // Prepend IV to ciphertext
using (var cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
byte[] data = Encoding.UTF8.GetBytes(plaintext);
cs.Write(data, 0, data.Length);
}
return Convert.ToBase64String(ms.ToArray());
}
catch (Exception ex)
{
throw new CryptographicException("Encryption failed.", ex);
}
}
@engelhardteric
Copy link

byte[] keys = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(hash));

whats the value of hash

@rahul-espranza
Copy link

byte[] keys = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(hash));

whats the value of hash

@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