using System;using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace AesSample
{
class Program
{
static void Main(string[] args)
{
//appsettings üzerinden okunduğunu varsayalım.
var encryptSettings = new EncryptSettings()
{
IV = "LoremIpsum",
Key = "DolorSitAmet",
};
IEncryptTools encryptTools = new EncryptTools(encryptSettings);
var sampleText = "Knowing is not enough; we must apply. Willing is not enough; we must do";
var encrypted = encryptTools.AesEncrypt(sampleText);
Console.WriteLine("Encrypted:");
Console.WriteLine(encrypted);
Console.WriteLine();
var decrypted = encryptTools.AesDecrypt(encrypted);
Console.WriteLine("Decrypted");
Console.WriteLine(decrypted);
}
}
public interface IEncryptTools
{
string AesEncrypt(string plainText);
string AesDecrypt(string encryptedText);
byte[] Sha256Byte(string plainText);
string Sha256String(string plainText);
string Sha256String(byte[] bytes);
}
public class EncryptSettings
{
public string Key { get; set; }
public string IV { get; set; }
}
public class EncryptTools : IEncryptTools
{
private readonly EncryptSettings encryptSettings;
public EncryptTools(EncryptSettings encryptSettings)
{
this.encryptSettings = encryptSettings;
}
private byte[] key => Sha256Byte(encryptSettings.Key);
private byte[] iv => new List<byte>(Sha256Byte(encryptSettings.IV))
.GetRange(0, 16).ToArray();
public string AesEncrypt(string plainText)
{
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (key == null || key.Length <= 0)
throw new ArgumentNullException("Key");
if (iv == null || iv.Length <= 0)
throw new ArgumentNullException("IV");
byte[] encrypted;
using (var aes = Aes.Create())
{
aes.Key = key;
aes.IV = iv;
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
using (var ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(cs))
{
swEncrypt.Write(plainText);
}
encrypted = ms.ToArray();
}
}
}
return Convert.ToBase64String(encrypted);
}
public string AesDecrypt(string encryptedText)
{
var cipherText = Convert.FromBase64String(encryptedText);
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (key == null || key.Length <= 0)
throw new ArgumentNullException("Key");
if (iv == null || iv.Length <= 0)
throw new ArgumentNullException("IV");
string plaintext = null;
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = key;
aesAlg.IV = iv;
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
public byte[] Sha256Byte(string plainText)
{
byte[] bytes = null;
using (SHA256 sha256Hash = SHA256.Create())
{
bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(plainText));
}
return bytes;
}
public string Sha256String(byte[] bytes)
{
var builder = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
builder.Append(bytes[i].ToString("x2"));
}
return builder.ToString();
}
public string Sha256String(string plainText)
{
var bytes = Sha256Byte(plainText);
return Sha256String(bytes);
}
}
}