public async Task CipheredEncryption_EncryptsAndDecrypts_WithCustomKeyAndIv() { string exampleKey = null; string exampleIv = null; using (var aes = Aes.Create()) { exampleKey = Convert.ToBase64String(aes.Key); exampleIv = Convert.ToBase64String(aes.IV); } var pwd = "password"; var encrypted = await pwd.ToEncryptedSecureString(exampleKey, exampleIv); var keyBytes = Convert.FromBase64String(exampleKey); var ivBytes = Convert.FromBase64String(exampleIv); Assert.NotNull(encrypted); Assert.NotNull(encrypted.String); Assert.AreNotEqual(pwd, encrypted.String.ToClearText()); Assert.AreEqual(keyBytes, encrypted.Key); Assert.AreEqual(ivBytes, encrypted.IV); var clearEncryptedCipher = encrypted.String.ToClearText(); var encryptedCipher = clearEncryptedCipher.ToSecureString(); var secureStr = new EncryptedSecureString(encryptedCipher, key: keyBytes, iv: ivBytes); var decrypted = await secureStr.ToDecryptedSecureString(); Assert.NotNull(decrypted); Assert.NotNull(decrypted.ToClearText()); Assert.AreEqual(pwd, decrypted.ToClearText()); }
public void CanAssignStringValue() { var encryptedSecString = new EncryptedSecureString(); TestDelegate testDelegate = () => encryptedSecString.SetValue(_clearTextData); Assert.DoesNotThrow(testDelegate); }
public void EncryptedValueIsSameAsOriginalValue() { var encryptedSecString = new EncryptedSecureString(); encryptedSecString.SetValue(_clearTextData); var decryptedData = encryptedSecString.GetClearTextValue(); Assert.That(decryptedData, Is.EqualTo(_clearTextData)); }
/// <summary> /// Decrypts an encrypted SecureString thats been ciphered in base64 /// </summary> /// <param name="encryptedSecureStr">the encrypted SecureString thats been ciphered in base64</param> /// <returns>a decrypted non-ciphered read-only SecureString</returns> public static async Task <SecureString> ToDecryptedSecureString(this EncryptedSecureString encryptedSecureStr) { var ciphered = new NetworkCredential("", encryptedSecureStr.String).Password; var encrypted = Convert.FromBase64String(ciphered); using (var aes = Aes.Create()) { aes.Key = encryptedSecureStr.Key; aes.IV = encryptedSecureStr.IV; return(await Adapter.Decrypt(encrypted, aes.Key, aes.IV)); } }
public void CreatingMultipleEncryptedSecureStrings() { var encString1 = new EncryptedSecureString(); encString1.SetValue(_clearTextData); var encString2 = new EncryptedSecureString(); encString2.SetValue("somevalue"); var decryptedString = encString1.GetClearTextValue(); Assert.That(decryptedString, Is.EqualTo(_clearTextData)); }