Skip to content

Instantly share code, notes, and snippets.

@relyky
Last active December 29, 2021 09:28
Show Gist options
  • Save relyky/82de55abb13d1acac7dc6d732ad0f9ce to your computer and use it in GitHub Desktop.
Save relyky/82de55abb13d1acac7dc6d732ad0f9ce to your computer and use it in GitHub Desktop.
.NET6, Aes 範例, System.Security.Cryptography, Cypher, 加密, Encryption
///
/// ref->[Aes 類別](https://docs.microsoft.com/zh-tw/dotnet/api/system.security.cryptography.aes?view=net-6.0)
///
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
class AesExample
{
public static void TestGo()
{
string original = "Here is 我是中文字 some data to encrypt!";
// Create a new instance of the Aes
// class. This generates a new key and initialization
// vector (IV).
using var aesAlg = Aes.Create();
// Encrypt the string to an array of bytes.
//byte[] encrypted = EncryptStringToBytes_Aes(original, myAes.Key, myAes.IV);
byte[] encrypted = DoEncryptData(original, aesAlg);
// Decrypt the bytes to a string.
//string roundtrip = DecryptStringFromBytes_Aes(encrypted, myAes.Key, myAes.IV);
string roundtrip = DoDecryptData(encrypted, aesAlg);
//Display the original data and the decrypted data.
Console.WriteLine("Original: {0}", original);
Console.WriteLine("Round Trip: {0}", roundtrip);
}
public static void Test2Go()
{
TestInfo original = new()
{
foo = "我是中文字",
bar = 9876543210.123456m,
baz = DateTime.Now
};
string cypher = AesSimpleEncrypt(original);
TestInfo roundtrip = AesSimpleDecrypt<TestInfo>(cypher);
//Display the original data and the decrypted data.
Console.WriteLine("Original: {0}", original);
Console.WriteLine("Round Trip: {0}", roundtrip);
Console.WriteLine("Cypher: {0}", cypher);
}
static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
{
// Check arguments.
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;
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
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);
}
encrypted = msEncrypt.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return encrypted;
}
static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
{
// Check arguments.
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");
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
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.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
static string AesSimpleEncrypt<TObject>(TObject payload)
{
return Convert.ToBase64String(DoEncryptData(JsonSerializer.Serialize<TObject>(payload), GenAesKey()));
}
static TObject AesSimpleDecrypt<TObject>(string cipherText)
{
return JsonSerializer.Deserialize<TObject>(DoDecryptData(Convert.FromBase64String(cipherText), GenAesKey()));
}
static byte[] DoEncryptData(string plainText, Aes aesAlg)
{
// Create an encryptor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for encryption.
using var msEncrypt = new MemoryStream();
using var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
using var swEncrypt = new StreamWriter(csEncrypt);
//Write all data to the stream.
swEncrypt.Write(plainText);
swEncrypt.Close();
return msEncrypt.ToArray();
}
static string DoDecryptData(byte[] cipherText, Aes aesAlg)
{
// Create a decryptor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption.
using var msDecrypt = new MemoryStream(cipherText);
using var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
using var srDecrypt = new StreamReader(csDecrypt);
// Read the decrypted bytes from the decrypting stream
return srDecrypt.ReadToEnd();
}
static Aes GenAesKey()
{
using var sha = new HMACSHA384(seed);
string envprops = $"{Environment.MachineName}{Environment.OSVersion}{Environment.ProcessorCount}{DateTime.Today.DayOfYear}9iabcGT%rdX0Ox.y";
var key48 = sha.ComputeHash(Encoding.ASCII.GetBytes(envprops)).AsSpan();
Aes aesAlg = Aes.Create();
aesAlg.Key = key48.Slice(0, 32).ToArray();
aesAlg.IV = key48.Slice(32, 16).ToArray();
return aesAlg;
}
/// <summary>
/// only for [JwtSimpleEncode] & [JwtSimpleDecode]
/// </summary>
static readonly byte[] seed = { 131, 117, 87, 14, 21, 11, 19, 75, 124, 10, 159, 78, ... };
}
record TestInfo
{
public string foo { get; set; }
public decimal bar { get; set; }
public DateTime baz { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment