public void Main_Decrypting_SingleFile_Verbosity_OK() { Setup.Initialize(out var testFolders); Setup.SetEncryptedFiles(testFolders); string originalFilePath = Directory.GetFiles(testFolders["original"])[0]; string outputFilePath = Path.Combine( path1: testFolders["decrypted"], path2: Path.GetFileNameWithoutExtension(originalFilePath) + ".decrypted.txt"); string targetFilePath = Path.Combine( path1: testFolders["encrypted"], path2: Path.GetFileNameWithoutExtension(originalFilePath) + ".encrypted.txt"); string[] args = { "-d", "--verbose", $@"--key={Setup.AbsolutePath}\priv.key.pem", $"--output={testFolders["decrypted"]}", $"--target={targetFilePath}", }; Program.Main(args); Assert.True(File.Exists(outputFilePath)); FileManipulation.OpenFile(outputFilePath, out var outputFile); Assert.NotNull(outputFile); FileManipulation.OpenFile(originalFilePath, out var originalFile); Assert.NotNull(originalFile); Assert.Equal(outputFile.Length, originalFile.Length); Assert.Equal(outputFile, originalFile); }
public void Main_VerifySignature_NotValidSignature_OK() { string hashalg = "SHA256"; Setup.Initialize(out var testFolders); Setup.SetSignatureFile(testFolders, hashalg); string fileName = Path.GetFileNameWithoutExtension(Directory.GetFiles(testFolders["encrypted"], "*encrypt*")[0].Replace(".encrypted", "")); string originalFilePath = Directory.GetFiles(testFolders["original"]).Last(); string signatureFilePath = Directory.GetFiles(testFolders["encrypted"], $"*{fileName}.SHA256*")[0]; string[] args = { "-v", $"--hashalg={hashalg}", "--verbose", $@"--key={Setup.AbsolutePath}\pub.key.pem", $"--target={originalFilePath}", $"--signaturefile={signatureFilePath}" }; Program.Main(args); FileManipulation.OpenFile(originalFilePath, out var originalFile); FileManipulation.OpenFile(signatureFilePath, out var signatureFile); Assert.True(!Setup.PublicKey.VerifySignedData(originalFile, signatureFile, hashalg)); }
/// <summary> /// Import a <see cref="EncryptionKeyPair"/> from a PEM file. /// </summary> /// <param name="path">Where the pem file is located.</param> /// <returns></returns> /// <exception cref="ArgumentNullException">Directory not specified.</exception> /// <exception cref="ArgumentException">Directory not found.</exception> /// <exception cref="InvalidCastException">Wasn't possible to import key.</exception> public static EncryptionKeyPair ImportPEMFile(string path) { if (string.IsNullOrWhiteSpace(path)) { throw new ArgumentNullException( paramName: nameof(path), message: "Directory not specified."); } if (!File.Exists(path)) { throw new ArgumentException( paramName: nameof(path), message: "File not found."); } using (var rsa = new RSACryptoServiceProvider()) { try { FileManipulation.OpenFile(path, out byte[] content); return(rsa.ImportRSAKeyPEM(content.AsEncodedString())); } finally { rsa.PersistKeyInCsp = false; } } }
public static void SetSignatureFile(Dictionary <string, string> testFolders, string hashalg = "SHA256") { SetEncryptedFiles(testFolders); var filePath = Directory.GetFiles(testFolders["original"])[0]; var fileName = Path.GetFileNameWithoutExtension(filePath); var fileExt = Path.GetExtension(filePath); FileManipulation.OpenFile(filePath, out var originalFile); string output = Path.Combine(testFolders["encrypted"], $"{fileName}.{hashalg}{fileExt}"); var signatureData = PrivateKey.SignData(originalFile, hashalg); File.WriteAllBytes(output, signatureData); }
public static void SetEncryptedFiles(Dictionary <string, string> testFolders, bool multiple = false) { int files = Directory.GetFiles(testFolders["original"]).Length; int i = 0; do { string filePath = Directory.GetFiles(testFolders["original"])[i]; FileManipulation.OpenFile(filePath, out var originalFile); string encryptedFileName = Path.GetFileNameWithoutExtension(filePath) + ".encrypted.txt"; string encryptedPathFile = Path.Combine(testFolders["encrypted"], encryptedFileName); byte[] encryptedFile = PrivateKey.EncryptRijndael(originalFile); File.WriteAllBytes(encryptedPathFile, encryptedFile); i++; } while (i < (multiple ? files : 1)); }
public static void SetMergedFile(Dictionary <string, string> testFolders, string hashalg) { SetSignatureFile(testFolders, hashalg); string output = testFolders["encrypted"]; string originalFilePath = Directory.GetFiles(testFolders["original"])[0]; string fileName = Path.GetFileNameWithoutExtension(originalFilePath); string signatureFilePath = Directory.GetFiles(testFolders["encrypted"], $"*{fileName}.SHA256*")[0]; FileManipulation.OpenFile(originalFilePath, out var data); FileManipulation.OpenFile(signatureFilePath, out var signature); byte[] mergedFile = new byte[signature.Length + data.Length]; using (var ms = new MemoryStream(mergedFile)) { ms.Write(signature, 0, signature.Length); ms.Write(data, 0, data.Length); } File.WriteAllBytes($"{output}\\{fileName}.merged.txt", mergedFile); }
public void Main_Decrypting_MultipleFile_Verbosity_OK() { Setup.Initialize(out var testFolders); Setup.SetEncryptedFiles(testFolders, true); string[] originalFilesPath = Directory.GetFiles(testFolders["original"]); string[] targetFilesPath = Directory.GetFiles(testFolders["encrypted"], "*encrypt*"); Array.Sort(targetFilesPath); string[] args = { "-d", "--verbose", $@"--key={Setup.AbsolutePath}\priv.key.pem", $"--output={testFolders["decrypted"]}", $"--target={testFolders["encrypted"]}", }; Program.Main(args); string[] outputFilesPath = Directory.GetFiles(testFolders["decrypted"]); Array.Sort(outputFilesPath); Assert.Equal(outputFilesPath.Length, originalFilesPath.Length); for (int i = 0; i < targetFilesPath.Length; i++) { FileManipulation.OpenFile(outputFilesPath[i], out var outputFile); Assert.NotNull(outputFile); FileManipulation.OpenFile(originalFilesPath[i], out var originalFile); Assert.NotNull(originalFile); Assert.Equal(outputFile.Length, originalFile.Length); Assert.Equal(outputFile, originalFile); } }