Initial Commit
This commit is contained in:
11
AESExample/AESExample.csproj
Normal file
11
AESExample/AESExample.csproj
Normal file
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<RootNamespace>RsaExample</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
86
AESExample/Program.cs
Normal file
86
AESExample/Program.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
|
||||
using System;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace xxx
|
||||
{
|
||||
public class AesEncryption
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
string plaintext = "Hello, World!";
|
||||
Console.WriteLine(plaintext);
|
||||
// Generate a random key and IV
|
||||
byte[] key = new byte[32]; // 256-bit key
|
||||
byte[] iv = new byte[16]; // 128-bit IV
|
||||
using (var rng = new RNGCryptoServiceProvider())
|
||||
{
|
||||
rng.GetBytes(key);
|
||||
rng.GetBytes(iv); //IV == initialization vector
|
||||
}
|
||||
|
||||
// Encrypt
|
||||
byte[] ciphertext = Encrypt(plaintext, key, iv);
|
||||
string encryptedText = Convert.ToBase64String(ciphertext);
|
||||
Console.WriteLine("Encrypted Text: " + encryptedText);
|
||||
// Decrypt
|
||||
byte[] bytes = Convert.FromBase64String(encryptedText);
|
||||
string decryptedText = Decrypt(bytes, key, iv);
|
||||
Console.WriteLine("Decrypted Text: " + decryptedText);
|
||||
//point
|
||||
Console.ReadLine();
|
||||
}
|
||||
|
||||
public static byte[] Encrypt(string plaintext, byte[] key, byte[] iv)
|
||||
{
|
||||
using (Aes aesAlg = Aes.Create())
|
||||
{
|
||||
aesAlg.Key = key;
|
||||
aesAlg.IV = iv;
|
||||
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
|
||||
byte[] encryptedBytes;
|
||||
using (var msEncrypt = new System.IO.MemoryStream())
|
||||
{
|
||||
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
|
||||
{
|
||||
byte[] plainBytes = Encoding.UTF8.GetBytes(plaintext);
|
||||
csEncrypt.Write(plainBytes, 0, plainBytes.Length);
|
||||
}
|
||||
encryptedBytes = msEncrypt.ToArray();
|
||||
}
|
||||
return encryptedBytes;
|
||||
}
|
||||
}
|
||||
|
||||
public static string Decrypt(byte[] ciphertext, byte[] key, byte[] iv)
|
||||
{
|
||||
using (Aes aesAlg = Aes.Create())
|
||||
{
|
||||
aesAlg.Key = key;
|
||||
aesAlg.IV = iv;
|
||||
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
|
||||
byte[] decryptedBytes;
|
||||
using (var msDecrypt = new System.IO.MemoryStream(ciphertext))
|
||||
{
|
||||
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
|
||||
{
|
||||
using (var msPlain = new System.IO.MemoryStream())
|
||||
{
|
||||
csDecrypt.CopyTo(msPlain);
|
||||
decryptedBytes = msPlain.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
return Encoding.UTF8.GetString(decryptedBytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user