private async Task Then_data_is_correctly_encrypted() { var encryptTask = EncryptAsync(_decryptedData); var controlTask = _algorithm.EncryptAsync(_decryptedData); var encrypted = await encryptTask; var control = await controlTask; Assert.NotNull(encrypted); // NOTE: Cannot use xunit's Assert.Equal here because for some reason it is glacially slow with large data sets. Assert.True(SequencesAreEqual(control, encrypted), "Encrypted sequence does not match the control sequence"); }
/// <summary> /// Encrypts a string that can also be decrypted using the same password and a salt and <see cref="SymmetricAlgorithm"/> /// </summary> /// <param name="algorithm">The SymmetricAlgorithm to encrypt with</param> /// <param name="value">The text that you want to encypt</param> /// <param name="encryptionKey">The password that is used with the salt to encrypt/decrypt the text</param> /// <param name="salt">A value to make the text being encrypted unique to a same text being encrypted elsewhere (basically a prefix added to the text prior to encryption). A salt should be unique to each stored encrypted value and saved elsewhere.</param> /// <param name="iterations">Is the number of times the encryption is performed</param> public static Task <string> EncryptAsync(this SymmetricAlgorithm algorithm, string value, string encryptionKey, string salt, int iterations = DEFAULT_ITERATIONS) { Assert.NotNullOrEmpty(salt, nameof(salt)); return(algorithm.EncryptAsync(value, encryptionKey, Encoding.Unicode.GetBytes(salt))); }
private static Task <byte[]> GeneratedEncryptedDataAsync(SymmetricAlgorithm algorithm, int len) { var decrypted = GenerateDecryptedData(len); return(algorithm.EncryptAsync(decrypted)); }