Skip to content

Commit

Permalink
Allow us to pass an optional IV when encrypting
Browse files Browse the repository at this point in the history
  • Loading branch information
LiamMorrow committed Dec 30, 2023
1 parent 1d4722d commit 0067c51
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
15 changes: 13 additions & 2 deletions LiftLog.Lib/Services/AesEncryptionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,22 @@ public ValueTask<byte[]> DecryptAsync(byte[] data, byte[] IV, byte[] key)
return ValueTask.FromResult(decryptor.TransformFinalBlock(data, 0, data.Length));
}

public ValueTask<(byte[] EncryptedPayload, byte[] IV)> EncryptAsync(byte[] data, byte[] key)
public ValueTask<(byte[] EncryptedPayload, byte[] IV)> EncryptAsync(
byte[] data,
byte[] key,
byte[]? iv = null
)
{
var aes = Aes.Create();

aes.GenerateIV();
if (iv is not null)
{
aes.IV = iv;
}
else
{
aes.GenerateIV();
}
aes.Key = key;

using var encryptor = aes.CreateEncryptor();
Expand Down
6 changes: 5 additions & 1 deletion LiftLog.Lib/Services/IEncryptionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ public interface IEncryptionService
{
ValueTask<byte[]> DecryptAsync(byte[] data, byte[] IV, byte[] key);

public ValueTask<(byte[] EncryptedPayload, byte[] IV)> EncryptAsync(byte[] data, byte[] key);
public ValueTask<(byte[] EncryptedPayload, byte[] IV)> EncryptAsync(
byte[] data,
byte[] key,
byte[]? iv = null
);
ValueTask<byte[]> GenerateKeyAsync();
}
20 changes: 20 additions & 0 deletions LiftLog.Tests/Encryption/AesEncryptionServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,24 @@ public async Task EncryptAndDecrypt_EncryptsAndDecryptsData()
// Assert
Assert.Equal(data, decryptedData);
}

[Fact]
public async Task EncryptAndDecrypt_EncryptsAndDecryptsDataGivenSameIV()
{
// Arrange
var key = await _encryptionService.GenerateKeyAsync();
var data1 = Encoding.UTF8.GetBytes("Hello, world!");
var data2 = Encoding.UTF8.GetBytes("Goodbye, world!");

// Act
var (encryptedData1, iv) = await _encryptionService.EncryptAsync(data1, key);
var decryptedData1 = await _encryptionService.DecryptAsync(encryptedData1, iv, key);

var (encryptedData2, _) = await _encryptionService.EncryptAsync(data2, key, iv);
var decryptedData2 = await _encryptionService.DecryptAsync(encryptedData2, iv, key);

// Assert
Assert.Equal(data1, decryptedData1);
Assert.Equal(data2, decryptedData2);
}
}
6 changes: 5 additions & 1 deletion LiftLog.Web/Services/JsAesEncryptionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ public ValueTask<byte[]> DecryptAsync(byte[] data, byte[] IV, byte[] key)
throw new NotImplementedException();
}

public ValueTask<(byte[] EncryptedPayload, byte[] IV)> EncryptAsync(byte[] data, byte[] key)
public ValueTask<(byte[] EncryptedPayload, byte[] IV)> EncryptAsync(
byte[] data,
byte[] key,
byte[]? iv = null
)
{
throw new NotImplementedException();
}
Expand Down

0 comments on commit 0067c51

Please sign in to comment.