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); }