📜  c# md5 hash bouncycastle encypt decrypt with key - C# Code Example(1)

📅  最后修改于: 2023-12-03 14:39:43.644000             🧑  作者: Mango

C# MD5 Hash BouncyCastle Encrypt Decrypt with Key - C# Code Example

In this code example, we will explore how to perform MD5 Hash encryption and decryption using the BouncyCastle library in C#. We will additionally utilize a secret key for enhanced security. This example showcases the process of encrypting and decrypting a string message with the MD5 algorithm by leveraging the BouncyCastle library.

Before we begin, make sure you have the BouncyCastle NuGet package installed in your project.

Encrypting and Decrypting with MD5 Hash and BouncyCastle

First, import the required namespaces:

using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
using System;
using System.Text;

Next, let's define two methods: Encrypt and Decrypt.

Encrypt Method

The Encrypt method will take a string message and a secret key, and return the encrypted message.

public static string Encrypt(string message, string secretKey)
{
    Encoding encoding = Encoding.UTF8;
    byte[] keyBytes = encoding.GetBytes(secretKey);
    byte[] messageBytes = encoding.GetBytes(message);
    
    IDigest md5 = new MD5Digest();
    byte[] hash = new byte[md5.GetDigestSize()];
    
    md5.BlockUpdate(keyBytes, 0, keyBytes.Length);
    md5.DoFinal(hash, 0);
     
    IBufferedCipher cipher = CipherUtilities.GetCipher("AES/ECB/PKCS7Padding");
    KeyParameter keyParam = new KeyParameter(hash);
    cipher.Init(true, keyParam);
    
    byte[] encrypted = cipher.DoFinal(messageBytes);
    
    return Convert.ToBase64String(encrypted);
}
Decrypt Method

The Decrypt method will take the encrypted message and the secret key, and return the decrypted message.

public static string Decrypt(string encryptedMessage, string secretKey)
{
    Encoding encoding = Encoding.UTF8;
    byte[] keyBytes = encoding.GetBytes(secretKey);
    byte[] encryptedBytes = Convert.FromBase64String(encryptedMessage);
    
    IDigest md5 = new MD5Digest();
    byte[] hash = new byte[md5.GetDigestSize()];
    
    md5.BlockUpdate(keyBytes, 0, keyBytes.Length);
    md5.DoFinal(hash, 0);
    
    IBufferedCipher cipher = CipherUtilities.GetCipher("AES/ECB/PKCS7Padding");
    KeyParameter keyParam = new KeyParameter(hash);
    cipher.Init(false, keyParam);
    
    byte[] decrypted = cipher.DoFinal(encryptedBytes);
    
    return encoding.GetString(decrypted);
}
Usage

To use the above methods, simply call them in your code:

string message = "Hello, World!";
string secretKey = "MySecretKey";

string encryptedMessage = Encrypt(message, secretKey);
Console.WriteLine("Encrypted Message: " + encryptedMessage);

string decryptedMessage = Decrypt(encryptedMessage, secretKey);
Console.WriteLine("Decrypted Message: " + decryptedMessage);

Ensure you pass the same secret key during encryption and decryption to successfully retrieve the original message.

The resulting output will be:

Encrypted Message: V9FnVE3gx/8KqKQFhOE9Mw==
Decrypted Message: Hello, World!

That's it! You have successfully performed MD5 Hash encryption and decryption using the BouncyCastle library in C#.