Add project files.

This commit is contained in:
n@work 2023-09-20 12:51:05 +01:00
parent 4aac867255
commit 4f78b3ece7
3 changed files with 107 additions and 0 deletions

10
EncryptionTest.csproj Normal file
View File

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

25
EncryptionTest.sln Normal file
View File

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.7.34031.279
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EncryptionTest", "EncryptionTest.csproj", "{A549CEB8-0D68-441D-8C82-85E96B255731}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A549CEB8-0D68-441D-8C82-85E96B255731}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A549CEB8-0D68-441D-8C82-85E96B255731}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A549CEB8-0D68-441D-8C82-85E96B255731}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A549CEB8-0D68-441D-8C82-85E96B255731}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A55FDA6C-4374-42C6-B539-86D24B42672B}
EndGlobalSection
EndGlobal

72
Program.cs Normal file
View File

@ -0,0 +1,72 @@
using System.Security.Cryptography;
using System.Text;
static string Encrypt(string plainText, byte[] secretKey) {
using Aes aes = Aes.Create();
aes.Key = secretKey;
// Convert the plain text string into a byte array
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
// Create an encryptor using the AES service and its initialization vector (IV)
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
// Perform the encryption
byte[] encrypted = encryptor.TransformFinalBlock(plainTextBytes, 0, plainTextBytes.Length);
// Create a new byte array to hold the IV and the encrypted data
byte[] result = new byte[aes.IV.Length + encrypted.Length];
// Copy the IV and encrypted data into the result byte array
Buffer.BlockCopy(aes.IV, 0, result, 0, aes.IV.Length);
Buffer.BlockCopy(encrypted, 0, result, aes.IV.Length, encrypted.Length);
return Convert.ToBase64String(result);
}
static string Decrypt(string encryptedText, byte[] secretKey) {
using Aes aes = Aes.Create();
aes.Key = secretKey;
// Decode the base64 string into a byte array
byte[] fullCipher = Convert.FromBase64String(encryptedText);
// Create byte arrays to hold the IV and the encrypted data
byte[] iv = new byte[aes.BlockSize / 8];
byte[] cipher = new byte[fullCipher.Length - iv.Length];
// Copy the IV and encrypted data from the full cipher byte array
Buffer.BlockCopy(fullCipher, 0, iv, 0, iv.Length);
Buffer.BlockCopy(fullCipher, iv.Length, cipher, 0, cipher.Length);
// Assign the IV to the AES service
aes.IV = iv;
// Create a decryptor using the AES service
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
// Perform the decryption
byte[] decrypted = decryptor.TransformFinalBlock(cipher, 0, cipher.Length);
// Convert the decrypted byte array back into a string
return Encoding.UTF8.GetString(decrypted);
}
Console.WriteLine("Hello, World!");
string key = "REDACTED_BASE64_STRING";
byte[] secretKey = Convert.FromBase64String(key);
try {
string input = "Hello, world!";
Console.WriteLine("Original text: " + input);
string encryptedText = Encrypt(input, secretKey);
Console.WriteLine("Encrypted text: " + encryptedText);
string decrypted = Decrypt(encryptedText, secretKey);
Console.WriteLine("Decrypted text: " + decrypted);
} catch (Exception e) {
Console.WriteLine(e);
}