diff --git a/Encrypter Tests/EncrypterTests.cs b/Encrypter Tests/EncrypterTests.cs index 27e4fbf..6acff19 100644 --- a/Encrypter Tests/EncrypterTests.cs +++ b/Encrypter Tests/EncrypterTests.cs @@ -99,7 +99,7 @@ namespace Encrypter_Tests } [Test] - public async Task TestChangedPassword() + public async Task TestAlteredPassword() { var message = "This is a test with umlauts äüö."; var password1 = "password!"; @@ -164,5 +164,41 @@ namespace Encrypter_Tests 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); + } + } } } diff --git a/Encrypter/CryptoProcessor.cs b/Encrypter/CryptoProcessor.cs index 6f9523e..65ff6f1 100644 --- a/Encrypter/CryptoProcessor.cs +++ b/Encrypter/CryptoProcessor.cs @@ -176,5 +176,22 @@ namespace Encrypter // Encrypt the data with the new settings: return await CryptoProcessor.EncryptString(decryptedData, password, upgradedIterations); } + + /// + /// Changes the password of the encryption. + /// + /// With the previous password encrypted data. + /// The previous password. + /// The new password. + /// The used iterations. + /// The re-encrypted data. + public static async Task 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); + } } } diff --git a/Encrypter/Encrypter.xml b/Encrypter/Encrypter.xml index 6c708f1..c6bdb52 100644 --- a/Encrypter/Encrypter.xml +++ b/Encrypter/Encrypter.xml @@ -45,6 +45,16 @@ The upgraded number of iterations. The re-encrypted data. + + + Changes the password of the encryption. + + With the previous password encrypted data. + The previous password. + The new password. + The used iterations. + The re-encrypted data. + Encrypts this string by means of AES. The result gets base64 encoded.