This commit is contained in:
Executable
+14
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Configuration.ConfigurationManager" Version="10.0.0-rc.2.25502.107" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Executable
+82
@@ -0,0 +1,82 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
using ConfigurationManager = System.Configuration.ConfigurationManager;
|
||||
using System.Collections.Specialized;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace aesencrypt;
|
||||
|
||||
public class Program
|
||||
{
|
||||
static void encrypt(string plainText, string encryptionKey)
|
||||
{
|
||||
byte[] keyBytes = Encoding.UTF8.GetBytes(encryptionKey);
|
||||
|
||||
using (Aes aes = Aes.Create())
|
||||
{
|
||||
aes.Key = keyBytes;
|
||||
aes.GenerateIV(); // Always generate a new IV
|
||||
|
||||
using (var encryptor = aes.CreateEncryptor(aes.Key, aes.IV))
|
||||
{
|
||||
byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
|
||||
byte[] encryptedBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
|
||||
|
||||
// Combine IV and encrypted data
|
||||
byte[] combinedBytes = new byte[aes.IV.Length + encryptedBytes.Length];
|
||||
Buffer.BlockCopy(aes.IV, 0, combinedBytes, 0, aes.IV.Length);
|
||||
Buffer.BlockCopy(encryptedBytes, 0, combinedBytes, aes.IV.Length, encryptedBytes.Length);
|
||||
|
||||
Console.WriteLine(Convert.ToBase64String(combinedBytes));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void decrypt(string plainText,string encryptionKey)
|
||||
{
|
||||
byte[] fullCipher = Convert.FromBase64String(plainText);
|
||||
byte[] iv = new byte[16];
|
||||
byte[] cipher = new byte[fullCipher.Length - iv.Length];
|
||||
|
||||
Buffer.BlockCopy(fullCipher, 0, iv, 0, iv.Length);
|
||||
Buffer.BlockCopy(fullCipher, iv.Length, cipher, 0, cipher.Length);
|
||||
|
||||
byte[] keyBytes = Encoding.UTF8.GetBytes(encryptionKey);
|
||||
|
||||
using (Aes aes = Aes.Create())
|
||||
{
|
||||
aes.Key = keyBytes;
|
||||
aes.IV = iv;
|
||||
|
||||
using (var decryptor = aes.CreateDecryptor(aes.Key, aes.IV))
|
||||
{
|
||||
byte[] decryptedBytes = decryptor.TransformFinalBlock(cipher, 0, cipher.Length);
|
||||
Console.WriteLine(Encoding.UTF8.GetString(decryptedBytes));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var Initialconfiguration = ConfigurationManager.GetSection("settings") as NameValueCollection;
|
||||
string function = Initialconfiguration["function"];
|
||||
string encryptionKeyUsed = Initialconfiguration["key"];
|
||||
if (function == "encrypt")
|
||||
{
|
||||
Console.WriteLine("What do you want to encrypt?");
|
||||
encrypt(Console.ReadLine(),encryptionKeyUsed);
|
||||
}
|
||||
else if (function == "decrypt")
|
||||
{
|
||||
Console.WriteLine("What do you want to decrypt?");
|
||||
decrypt(Console.ReadLine(),encryptionKeyUsed);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Invalid function type");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Executable
+10
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<configuration>
|
||||
<configSections>
|
||||
<section name="settings" type="System.Configuration.NameValueSectionHandler" />
|
||||
</configSections>
|
||||
<settings>
|
||||
<add key="function" value="decrypt" />
|
||||
<add key="key" value="12345678901234567890123456789012" />
|
||||
</settings>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user