Added a method to change the password

This commit is contained in:
Thorsten Sommer 2020-01-05 20:12:02 +01:00
parent 8b117c5e6c
commit fa4ce31491
3 changed files with 64 additions and 1 deletions

View File

@ -99,7 +99,7 @@ namespace Encrypter_Tests
} }
[Test] [Test]
public async Task TestChangedPassword() public async Task TestAlteredPassword()
{ {
var message = "This is a test with umlauts äüö."; var message = "This is a test with umlauts äüö.";
var password1 = "password!"; var password1 = "password!";
@ -164,5 +164,41 @@ namespace Encrypter_Tests
Assert.That(true); 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);
}
}
} }
} }

View File

@ -176,5 +176,22 @@ namespace Encrypter
// Encrypt the data with the new settings: // Encrypt the data with the new settings:
return await CryptoProcessor.EncryptString(decryptedData, password, upgradedIterations); 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);
}
} }
} }

View File

@ -45,6 +45,16 @@
<param name="upgradedIterations">The upgraded number of iterations.</param> <param name="upgradedIterations">The upgraded number of iterations.</param>
<returns>The re-encrypted data.</returns> <returns>The re-encrypted data.</returns>
</member> </member>
<member name="M:Encrypter.CryptoProcessor.ChangePassword(System.String,System.String,System.String,System.Int32)">
<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>
</member>
<member name="M:Encrypter.Extensions.Encrypt(System.String,System.String)"> <member name="M:Encrypter.Extensions.Encrypt(System.String,System.String)">
<summary> <summary>
Encrypts this string by means of AES. The result gets base64 encoded. Encrypts this string by means of AES. The result gets base64 encoded.