Skip to content

Commit

Permalink
Added a method to change the password
Browse files Browse the repository at this point in the history
  • Loading branch information
SommerEngineering committed Jan 5, 2020
1 parent 8b117c5 commit fa4ce31
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
38 changes: 37 additions & 1 deletion Encrypter Tests/EncrypterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public async Task TestSimpleEnAndDecryptionWithASCII()
}

[Test]
public async Task TestChangedPassword()
public async Task TestAlteredPassword()
{
var message = "This is a test with umlauts äüö.";
var password1 = "password!";
Expand Down Expand Up @@ -164,5 +164,41 @@ public async Task TestUpgradedIterationsBehaviour()
Assert.That(true);
}
}

[Test]
public async Task TestChangedPasswordBehaviour()
{
var message = "This is a test with umlauts äüö.";
var previousPassword = "test password";
var newPassword = "test password!!!";
var iterations = 1_000;

var previousEncryptedData = await CryptoProcessor.EncryptString(message, previousPassword, iterations);
var reEncryptedData = await CryptoProcessor.ChangePassword(previousEncryptedData, previousPassword, newPassword, iterations);
Assert.That(previousEncryptedData, Is.Not.EqualTo(reEncryptedData));

var decryptedMessage = await CryptoProcessor.DecryptString(reEncryptedData, newPassword, iterations);
Assert.That(decryptedMessage, Is.EqualTo(message));

try
{
var decryptedMessage2 = await CryptoProcessor.DecryptString(reEncryptedData, previousPassword, iterations);
Assert.Fail("Should not be reached!");
}
catch (CryptographicException e)
{
Assert.That(true);
}

try
{
var decryptedMessage2 = await CryptoProcessor.DecryptString(previousEncryptedData, newPassword, iterations);
Assert.Fail("Should not be reached!");
}
catch (CryptographicException e)
{
Assert.That(true);
}
}
}
}
17 changes: 17 additions & 0 deletions Encrypter/CryptoProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,22 @@ public static async Task<string> UpgradeIterations(string encryptedDataBeforeUpg
// Encrypt the data with the new settings:
return await CryptoProcessor.EncryptString(decryptedData, password, upgradedIterations);
}

/// <summary>
/// Changes the password of the encryption.
/// </summary>
/// <param name="encryptedDataBeforeChange">With the previous password encrypted data.</param>
/// <param name="previousPassword">The previous password.</param>
/// <param name="newPassword">The new password.</param>
/// <param name="iterations">The used iterations.</param>
/// <returns>The re-encrypted data.</returns>
public static async Task<string> ChangePassword(string encryptedDataBeforeChange, string previousPassword, string newPassword, int iterations = ITERATIONS_YEAR_2020)
{
// Decrypt the data with the previous settings:
var decryptedData = await CryptoProcessor.DecryptString(encryptedDataBeforeChange, previousPassword, iterations);

// Encrypt the data with the new settings:
return await CryptoProcessor.EncryptString(decryptedData, newPassword, iterations);
}
}
}
10 changes: 10 additions & 0 deletions Encrypter/Encrypter.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fa4ce31

Please sign in to comment.