public void Encrypt_Decrypt_Cycle(string text, string keyText, string textFileName, string expectedCipher) { var encrypted = SymmetricKeyCryptography.Encrypt(text, keyText, textFileName); Assert.AreEqual(expectedCipher, encrypted); var decrypted = SymmetricKeyCryptography.Decrypt(encrypted, keyText, textFileName); Assert.AreEqual(text, decrypted); }
/// <summary> /// Requires a file with an ending with ".encrypted" for example: "secrets.zip.encrypted". /// Creates a file ending with ".decrypted-2017-12-19" for example: "secrets.zip.decrypted-2017-12-19T08-08-11Z". /// If the file "secrets.zip.decrypted-2017-12-19T08-08-11Z" had already existed, it would be overwritten. /// </summary> /// <returns>Returns the path to the encrypted file.</returns> private static string Decrypt(string path, string password) { var originalPath = path.Substring(0, path.Length - ENCRYPTED_EXT.Length); var originalFileName = originalPath.Split('\\').Last(); var encryptedData = File.ReadAllBytes(path); var encrypted = SymmetricKeyCryptography.Decrypt( encrypted: Convert.ToBase64String(encryptedData), keyText: password, initializationVectorText: originalFileName); var today = DateTime.Now.ToString("yyyy-MM-ddTHH-mm-ssZ"); var safeWritePath = originalPath + $".decrypted-{today}"; File.WriteAllBytes(safeWritePath, Convert.FromBase64String(encrypted)); return(safeWritePath); }