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> /// Produces a file ending with ".encrypted". /// WARNING: The file can't not be renmaed or the file can't be decrypted. If the original filename is changed and forgotten the data is lost. /// </summary> /// <param name="path">Full path to file.</param> /// <param name="password">Password to encrpt file.</param> /// <returns>Returns the path to the encrypted file.</returns> private static string Encrypt(string path, string password) { var fileName = path.Split('\\').Last(); var data = File.ReadAllBytes(path); var encrypted = SymmetricKeyCryptography.Encrypt( data: Convert.ToBase64String(data), keyText: password, initializationVectorText: fileName); var encryptedPath = path + ".encrypted"; File.WriteAllBytes(encryptedPath, Convert.FromBase64String(encrypted)); return(encryptedPath); }
/// <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); }