AES 对称加解密
This commit is contained in:
1 parent
fbc7565d7b
commit
7bf59d7f74
4 files changed
+118
-15
No files matched your search
@@ -0,0 +1,40 @@
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace MeloongCore.Tests;
|
||||
public class CryptographyUtilsTest : TestBase {
|
||||
|
||||
#region 对称加密
|
||||
|
||||
[Test]
|
||||
[Arguments("")]
|
||||
[Arguments("PCL")]
|
||||
[Arguments("中文内容 Test 123 !@#$%^&*()")]
|
||||
public async Task 对称加密_往返(string sourceString) {
|
||||
string encryptedString = CryptographyUtils.AesEncrypt(sourceString, "PCL-Key-中文");
|
||||
await Assert.That(encryptedString != sourceString).IsTrue();
|
||||
await Assert.That(CryptographyUtils.AesDecrypt(encryptedString, "PCL-Key-中文")).IsEqualTo(sourceString);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task 对称加密_随机化()
|
||||
=> await Assert.That(CryptographyUtils.AesEncrypt("same source", "same key") != CryptographyUtils.AesEncrypt("same source", "same key")).IsTrue();
|
||||
|
||||
[Test]
|
||||
public void 对称加密_篡改失败() {
|
||||
byte[] encryptedBytes = Convert.FromBase64String(CryptographyUtils.AesEncrypt("source", "key"));
|
||||
encryptedBytes[encryptedBytes.Length - 1] ^= 1;
|
||||
Assert.Throws<CryptographicException>(() => CryptographyUtils.AesDecrypt(Convert.ToBase64String(encryptedBytes), "key"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void 对称加密_错误密钥失败() {
|
||||
Assert.Throws<CryptographicException>(() => CryptographyUtils.AesDecrypt(CryptographyUtils.AesEncrypt("source", "key"), "bad key"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void 对称加密_格式错误失败()
|
||||
=> Assert.Throws<CryptographicException>(() => CryptographyUtils.AesDecrypt("not base64", "key"));
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user