public void TestRSADecryptFile() { GenerateRSATestFiles(); GenerateRSATestFiles(); var rsaDir = ConfigurationManager.AppSettings.Get("rsa_dir"); var input = rsaDir + '/' + ConfigurationManager.AppSettings.Get("RSA_rsa_in"); var output = rsaDir + '/' + ConfigurationManager.AppSettings.Get("RSA_txt_out"); var key = rsaDir + '/' + ConfigurationManager.AppSettings.Get("RSA_priv_key"); //OK Assert.AreEqual(ClassRsa.Decrypt(input, output, key), "File successfully decrypted"); //Exceptions Assert.AreEqual(ClassRsa.Decrypt("carciofo.rsa", output, key), "Exception on decryption: carciofo.rsa not found"); Assert.AreEqual(ClassRsa.Decrypt(input, output, "carciofo.xml"), "Exception on decryption: carciofo.xml not found"); Assert.AreEqual(ClassRsa.Decrypt(input, "./cartella/out.txt", key), "Exception on decryption: Output directory not found"); var testInput = rsaDir + '/' + Path.GetFileNameWithoutExtension(input) + ".docx"; File.Create(testInput).Close(); Assert.AreEqual(ClassRsa.Decrypt(testInput, output, key), "Exception on decryption: Wrong input file format"); var testOutput = rsaDir + '/' + Path.GetFileNameWithoutExtension(output) + ".docx"; File.Create(testOutput).Close(); Assert.AreEqual(ClassRsa.Decrypt(input, testOutput, key), "Exception on decryption: Wrong output file format"); var testKey = rsaDir + '/' + Path.GetFileNameWithoutExtension(key) + ".docx"; File.Create(testKey).Close(); Assert.AreEqual(ClassRsa.Decrypt(input, output, testKey), "Exception on decryption: Wrong key file format"); testInput = input; File.WriteAllText(testInput, ""); Assert.AreEqual(ClassRsa.Decrypt(testInput, output, key), "Exception on decryption: Empty rsa file"); File.WriteAllText(testInput, "2sZQzP1EYkwxZm66llsuIPEzec4G1GzVpWljDuGfSc20INCO/JyfCfX7wvIE3GtuPjFtn0wKKlO0jxgPJrDchA5XPgCRvhJUzdtfWeW5Nclqs6PCID8hC00hYzncSW6+iPUKPrrXGMqs+mO2t08xFaRpXHmxDCaBCR++PLNjfmw="); testKey = key; File.WriteAllText(testKey, ""); Assert.AreEqual(ClassRsa.Decrypt(input, output, testKey), "Exception on decryption: Empty key file"); }
public void TestRSAEncryptFile() { GenerateRSATestFiles(); var rsaDir = ConfigurationManager.AppSettings.Get("rsa_dir"); var input = rsaDir + '/' + ConfigurationManager.AppSettings.Get("RSA_txt_in"); var output = rsaDir + '/' + ConfigurationManager.AppSettings.Get("RSA_rsa_out"); var key = rsaDir + '/' + ConfigurationManager.AppSettings.Get("RSA_pub_key"); //OK Assert.AreEqual(ClassRsa.Encrypt(input, output, key), "File successfully encrypted"); //Exceptions Assert.AreEqual(ClassRsa.Encrypt("carciofo.txt", output, key), "Exception on encryption: carciofo.txt not found"); Assert.AreEqual(ClassRsa.Encrypt(input, output, "carciofo.xml"), "Exception on encryption: carciofo.xml not found"); Assert.AreEqual(ClassRsa.Encrypt(input, "./cartella/out.rsa", key), "Exception on encryption: Output directory not found"); var testInput = rsaDir + '/' + Path.GetFileNameWithoutExtension(input) + ".docx"; File.Create(testInput).Close(); Assert.AreEqual(ClassRsa.Encrypt(testInput, output, key), "Exception on encryption: Wrong input file format"); var testOutput = rsaDir + '/' + Path.GetFileNameWithoutExtension(output) + ".docx"; File.Create(testOutput).Close(); Assert.AreEqual(ClassRsa.Encrypt(input, testOutput, key), "Exception on encryption: Wrong output file format"); var testKey = rsaDir + '/' + Path.GetFileNameWithoutExtension(key) + ".docx"; File.Create(testKey).Close(); Assert.AreEqual(ClassRsa.Encrypt(input, output, testKey), "Exception on encryption: Wrong key file format"); testInput = input; File.WriteAllText(testInput, ""); Assert.AreEqual(ClassRsa.Encrypt(testInput, output, key), "Exception on encryption: Empty text file"); File.WriteAllText(testInput, "Prova 1"); testKey = key; File.WriteAllText(testKey, ""); Assert.AreEqual(ClassRsa.Encrypt(input, output, testKey), "Exception on encryption: Empty key file"); }
static void Main(string[] args) { try { if (args.Length == 0) { Help(); return; } if (args.Length != 4) { throw new Exception("Syntax error"); } //command var com = args[0].ToUpper(); if (com != "ENC" && com != "DEC") { throw new Exception("Unknown command"); } //XML file containing key var keyFile = args[1]; //input file var inp = args[2]; //output file var outp = args[3]; switch (com) { case "ENC": Console.WriteLine(ClassRsa.Encrypt(inp, outp, keyFile)); break; case "DEC": Console.WriteLine(ClassRsa.Decrypt(inp, outp, keyFile)); break; default: throw new Exception("Unknown command"); } } catch (Exception e) { Console.WriteLine("Exception: " + e.Message); Help(); } }