diff --git a/vault-decryptFile/Program.cs b/vault-decryptFile/Program.cs index 1bc52a6..0933ec8 100644 --- a/vault-decryptFile/Program.cs +++ b/vault-decryptFile/Program.cs @@ -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 "); + 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 "); + 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]; + } + } +} \ No newline at end of file diff --git a/vault-decryptFile/vault-decryptFile.csproj b/vault-decryptFile/vault-decryptFile.csproj index f2f8a68..1c3590d 100644 --- a/vault-decryptFile/vault-decryptFile.csproj +++ b/vault-decryptFile/vault-decryptFile.csproj @@ -2,10 +2,19 @@ Exe - net8.0 + net8.0-windows10.0.22621.0 vault_decryptFile enable enable + 7.0 + + + + embedded + + + + embedded