Last active
August 20, 2025 05:44
-
-
Save ginxx009/116d124e899e3c248c80c1d69ab90410 to your computer and use it in GitHub Desktop.
Encryption & Decryption MD5 C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| } |
byte[] keys = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(hash));
whats the value of hash
static string hash { get; set; } = "A!9HHhi%XjjYY4YP2@Nob009X";
in file Encrypt.cs
line 2: use textBox4.Text instead of textBox3.Text
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
byte[] keys = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(hash));whats the value of hash