Files
CBDDC/tests/ZB.MOM.WW.CBDDC.Network.Tests/CryptoHelperTests.cs
Joseph Doherty 7ebc2cb567
All checks were successful
NuGet Package Publish / nuget (push) Successful in 1m10s
Reformat/cleanup
2026-02-21 07:53:53 -05:00

49 lines
1.4 KiB
C#
Executable File

using System.Security.Cryptography;
using ZB.MOM.WW.CBDDC.Network.Security;
namespace ZB.MOM.WW.CBDDC.Network.Tests;
public class CryptoHelperTests
{
/// <summary>
/// Verifies that encrypted data can be decrypted back to the original payload.
/// </summary>
[Fact]
public void EncryptDecrypt_ShouldPreserveData()
{
// Arrange
var key = new byte[32]; // 256 bits
RandomNumberGenerator.Fill(key);
var original = new byte[] { 1, 2, 3, 4, 5, 255, 0, 10 };
// Act
(byte[] ciphertext, byte[] iv, byte[] tag) = CryptoHelper.Encrypt(original, key);
byte[] decrypted = CryptoHelper.Decrypt(ciphertext, iv, tag, key);
// Assert
decrypted.ShouldBe(original);
}
/// <summary>
/// Verifies that decryption fails when ciphertext is tampered with.
/// </summary>
[Fact]
public void Decrypt_ShouldFail_IfTampered()
{
// Arrange
var key = new byte[32];
RandomNumberGenerator.Fill(key);
var original = new byte[] { 1, 2, 3 };
(byte[] ciphertext, byte[] iv, byte[] tag) = CryptoHelper.Encrypt(original, key);
// Tamper ciphertext
ciphertext[0] ^= 0xFF;
// Act
Action act = () => CryptoHelper.Decrypt(ciphertext, iv, tag, key);
// Assert
Should.Throw<CryptographicException>(act);
}
}