This commit is contained in:
Chenx221 2024-03-12 17:00:03 +08:00
parent a04b4cab5a
commit dd976ed10a
2 changed files with 93 additions and 2 deletions

View File

@ -1 +1,83 @@
Console.WriteLine("Hello, World!");
using System.Security.Cryptography;
using System.Text;
class Program
{
static void Main(string[] args)
{
if (args.Length != 5)
{
Console.WriteLine("Usage: .exe <operate[dec,enc]> <inputFilePath> <outputFilePath> <password> <salt>");
return;
}
string operate = args[0];
string inputFilePath = args[1];
string outputFilePath = args[2];
string password = args[3];
string salt = args[4];
if(operate == "dec")
{
byte[] inputData = File.ReadAllBytes(inputFilePath);
byte[] outputData = DecryptFile(inputData, password, salt);
File.WriteAllBytes(outputFilePath, outputData);
}
else if(operate == "enc")
{
byte[] inputData = File.ReadAllBytes(inputFilePath);
byte[] outputData = EncryptFile(inputData, password, salt);
File.WriteAllBytes(outputFilePath, outputData);
}
else
{
Console.WriteLine("Usage: .exe <operate[dec,enc]> <inputFilePath> <outputFilePath> <password> <salt>");
return;
}
}
public static byte[] DecryptFile(byte[] encryptedData, string password, string salt)
{
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
byte[] saltBytes = Encoding.UTF8.GetBytes(salt);
using (var pbkdf2 = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 100000, HashAlgorithmName.SHA256))
{
byte[] key = pbkdf2.GetBytes(32);
byte[] iv = new byte[12];
Array.Copy(encryptedData, 0, iv, 0, 12);
byte[] tag = new byte[16];
Array.Copy(encryptedData, encryptedData.Length - 16, tag, 0, 16);
byte[] ciphertext = new byte[encryptedData.Length - 28];
Array.Copy(encryptedData, 12, ciphertext, 0, ciphertext.Length);
byte[] plaintext = new byte[ciphertext.Length];
using (var aes = new AesGcm(key,16))
{
aes.Decrypt(iv, ciphertext, tag, plaintext);
}
return plaintext;
}
}
public static byte[] EncryptFile(byte[] plaintextData, string password, string salt)
{
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
byte[] saltBytes = Encoding.UTF8.GetBytes(salt);
using (var pbkdf2 = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 100000, HashAlgorithmName.SHA256))
{
byte[] key = pbkdf2.GetBytes(32);
byte[] iv = new byte[12];
using (var rng = RandomNumberGenerator.Create())
{
rng.GetBytes(iv);
}
byte[] ciphertext = new byte[plaintextData.Length];
byte[] tag = new byte[16];
using (var aes = new AesGcm(key,16))
{
aes.Encrypt(iv, plaintextData, ciphertext, tag);
}
return [.. iv, .. ciphertext, .. tag];
}
}
}

View File

@ -2,10 +2,19 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net8.0-windows10.0.22621.0</TargetFramework>
<RootNamespace>vault_decryptFile</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<SupportedOSPlatformVersion>7.0</SupportedOSPlatformVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>embedded</DebugType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DebugType>embedded</DebugType>
</PropertyGroup>
</Project>