Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save normanlmfung/96d141918a460a63092728ace14394bc to your computer and use it in GitHub Desktop.
Save normanlmfung/96d141918a460a63092728ace14394bc to your computer and use it in GitHub Desktop.
csharp_ase_encrypt
using System;
using System.Security.Cryptography;
using System.Text;
class Program
{
static void Main()
{
// Define a password, salt, and iteration count for key derivation
string password = "ThisIsAStrongPassword";
byte[] salt = Encoding.UTF8.GetBytes("ThisIsASalt");
int iterations = 10000;
// Derive a valid key using the password, salt, and iteration count
byte[] key = DeriveKey(password, salt, iterations);
// Initialization vector (IV) is also needed for AES encryption
byte[] iv = GenerateIV();
string originalText = "Hello, this is a secret message!";
// Encrypt the original text
byte[] encryptedBytes = EncryptStringToBytes(originalText, key, iv);
// Convert the encrypted bytes to a base64 string for easy storage or transmission
string encryptedText = Convert.ToBase64String(encryptedBytes);
Console.WriteLine("Encrypted Text: " + encryptedText);
// Decrypt the encrypted text
string decryptedText = DecryptStringFromBytes(encryptedBytes, key, iv);
Console.WriteLine("Decrypted Text: " + decryptedText);
Console.ReadLine();
}
static byte[] DeriveKey(string password, byte[] salt, int iterations)
{
using (Rfc2898DeriveBytes derivedBytes = new Rfc2898DeriveBytes(password, salt, iterations))
{
return derivedBytes.GetBytes(32); // 32 bytes = 256 bits (valid key size for AES)
}
}
static byte[] GenerateIV()
{
using (AesManaged aes = new AesManaged())
{
aes.GenerateIV();
return aes.IV;
}
}
static byte[] EncryptStringToBytes(string plainText, byte[] key, byte[] iv)
{
// Create a new instance of the AesManaged class
using (AesManaged aesAlg = new AesManaged())
{
// Set the key and IV
aesAlg.Key = key;
aesAlg.IV = iv;
// Create an encryptor to perform the stream transform
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for encryption
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
// Write all data to the stream
swEncrypt.Write(plainText);
}
// Return the encrypted bytes from the memory stream
return msEncrypt.ToArray();
}
}
}
}
static string DecryptStringFromBytes(byte[] cipherText, byte[] key, byte[] iv)
{
// Create a new instance of the AesManaged class
using (AesManaged aesAlg = new AesManaged())
{
// Set the key and IV
aesAlg.Key = key;
aesAlg.IV = iv;
// Create a decryptor to perform the stream transform
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream and place them in a string
return srDecrypt.ReadToEnd();
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment