EncryptTests.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System.IO;
  2. using System.Text;
  3. using Masuit.Tools.Security;
  4. using Xunit;
  5. namespace Masuit.Tools.Abstractions.Test.Security
  6. {
  7. public class EncryptTests
  8. {
  9. private const string TestString = "Hello, World!";
  10. private const string DesKey = "12345678"; // 8位密钥
  11. private static readonly byte[] DesKeyBytes = Encoding.ASCII.GetBytes(DesKey);
  12. private static readonly byte[] DesIVBytes = Encoding.ASCII.GetBytes(DesKey);
  13. [Fact]
  14. public void DesEncrypt_StringKey_ShouldEncryptAndDecrypt()
  15. {
  16. // Arrange
  17. string original = TestString;
  18. // Act
  19. string encrypted = original.DesEncrypt(DesKey);
  20. string decrypted = encrypted.DesDecrypt(DesKey);
  21. // Assert
  22. Assert.Equal(original, decrypted);
  23. }
  24. [Fact]
  25. public void DesEncrypt_ByteKey_ShouldEncryptAndDecrypt()
  26. {
  27. // Arrange
  28. string original = TestString;
  29. // Act
  30. string encrypted = original.DesEncrypt(DesKeyBytes, DesIVBytes);
  31. string decrypted = encrypted.DesDecrypt(DesKeyBytes, DesIVBytes);
  32. // Assert
  33. Assert.Equal(original, decrypted);
  34. }
  35. }
  36. }