public static void SetDatabaseMasterPassword(SecureString password = null, string encryptedTextPassword = "") { if (password != null) { DatabaseInfo.DatabaseMasterPassword = password; } if (!string.IsNullOrWhiteSpace(encryptedTextPassword)) { string unencryptedPassword = CryptoProcessor.Decrypt(encryptedTextPassword); if (string.IsNullOrWhiteSpace(unencryptedPassword)) { throw new ArgumentException("Encrypted Text Failed to decrypt. Please Reset the Password and update storage"); } SecureString secureStringPassword = new SecureString(); foreach (char c in unencryptedPassword) { secureStringPassword.AppendChar(c); } if (password != null) { if (password.GetHashCode() != secureStringPassword.GetHashCode()) { throw new ArgumentException("You can only pass one password type, please remove one password from the method call and try again."); } } else { DatabaseInfo.DatabaseMasterPassword = secureStringPassword; } } }
/// <summary> /// Decrypts an encrypted private key. /// </summary> /// <param name="privateKey">The encrypted private key.</param> /// <param name="password">The matching password.</param> /// <returns>The decrypted private key.</returns> public static async Task <byte[]> DecryptPrivateKeyAsync(this byte[] privateKey, string password) { await using var inputStream = new MemoryStream(privateKey); await using var outputStream = new MemoryStream(); await CryptoProcessor.Decrypt(inputStream, outputStream, password); return(outputStream.ToArray()); }
/// <summary> /// Decrypts an encrypted private key. /// </summary> /// <param name="privateKey">The encrypted private key.</param> /// <param name="password">The matching password.</param> /// <returns>The decrypted private key.</returns> public static ReadOnlySpan <byte> DecryptPrivateKey(this ReadOnlySpan <byte> privateKey, string password) { using var inputStream = new MemoryStream(privateKey.ToArray()); using var outputStream = new MemoryStream(); CryptoProcessor.Decrypt(inputStream, outputStream, password).Wait(); return(outputStream.ToArray()); }
public async Task TestTooShortPassword4Decryption() { var message = "This is a test with umlauts äüö."; var password = "******"; var encryptedData = await CryptoProcessor.Encrypt(message, password); try { var decryptedMessage = await CryptoProcessor.Decrypt(encryptedData, password[..4]);
public async Task TestEmptyMessage() { var message = string.Empty; var password = "******"; var encryptedData = await CryptoProcessor.Encrypt(message, password); var decryptedMessage = await CryptoProcessor.Decrypt(encryptedData, password); Assert.That(decryptedMessage, Is.EqualTo(message)); }
public async Task TestSimpleEnAndDecryption() { var message = "This is a test with umlauts äüö."; var password = "******"; var encryptedData = await CryptoProcessor.Encrypt(message, password); Assert.That(encryptedData.Length, Is.AtLeast(message.Length)); // Note: Encrypted data contains salt as well! var decryptedMessage = await CryptoProcessor.Decrypt(encryptedData, password); Assert.That(decryptedMessage, Is.EqualTo(message)); }
public static void SetDatabaseMasterPassword(string encryptedTextPassword = "") { if (!string.IsNullOrWhiteSpace(encryptedTextPassword)) { string unencryptedPassword = CryptoProcessor.Decrypt(encryptedTextPassword); if (string.IsNullOrWhiteSpace(unencryptedPassword)) { throw new ArgumentException("Encrypted Text Failed to decrypt. Please Reset the Password and update storage"); } SecureString secureStringPassword = new SecureString(); foreach (char c in unencryptedPassword) { secureStringPassword.AppendChar(c); } DatabaseInfo.DatabaseMasterPassword = secureStringPassword; DatabaseInfo.DatabaseMasterPassword.MakeReadOnly(); } }