forked from BeyondDimension/SteamTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByteArrayTest.cs
More file actions
83 lines (63 loc) · 2.53 KB
/
ByteArrayTest.cs
File metadata and controls
83 lines (63 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using NUnit.Framework;
using System.Security.Cryptography;
namespace System
{
[TestFixture]
public class ByteArrayTest
{
[Test]
public void ByteArray()
{
var values = new byte[] { 0xff };
var sbytes = values.ToSByteArray();
var bytes = sbytes.ToByteArray();
Assert.IsTrue(bytes[0] == values[0]);
}
[Test]
public void MultipleEncrypt()
{
var aes_cbc_1 = AESUtils.Create();
var aes_cfb_1 = AESUtils.Create(mode: CipherMode.CFB);
var aes_cbc_2 = AESUtils.Create();
var value = DateTime.Now.ToString() + " 🔮🎮";
var bytes_1 = AESUtils.EncryptToByteArray(aes_cbc_1, value);
var bytes_2 = AESUtils.Encrypt(aes_cfb_1, bytes_1);
var bytes_3 = AESUtils.Encrypt(aes_cbc_2, bytes_2);
var bytes_4 = bytes_3;
var d_bytes_4 = bytes_4;
#pragma warning disable CA1416 // 验证平台兼容性
#if !ANDROID && !__ANDROID__ && !__MOBILE__
if (DI.Platform == Platform.Windows)
{
bytes_4 = ProtectedData.Protect(bytes_3, null, DataProtectionScope.LocalMachine);
d_bytes_4 = ProtectedData.Unprotect(bytes_4, null, DataProtectionScope.LocalMachine);
}
#endif
#pragma warning restore CA1416 // 验证平台兼容性
var d_bytes_3 = AESUtils.Decrypt(aes_cbc_2, d_bytes_4);
var d_bytes_2 = AESUtils.Decrypt(aes_cfb_1, d_bytes_3);
var d_value = AESUtils.DecryptToString(aes_cbc_1, d_bytes_2);
TestContext.WriteLine(d_value);
}
[Test]
public void LocalEncrypt()
{
var value = DateTime.Now.ToString() + " 🔮🎮";
var key_str = Environment.MachineName;
(var key, var iv) = AESUtils.GetParameters(key_str);
var aes_ofb = AESUtils.Create(mode: CipherMode.CFB);
aes_ofb.Key = key;
aes_ofb.IV = iv;
var data = AESUtils.EncryptToByteArray(aes_ofb, value);
var value1 = AESUtils.DecryptToString(aes_ofb, data);
Assert.IsTrue(value == value1);
var aes_ofb_1 = AESUtils.Create(mode: CipherMode.CFB);
aes_ofb_1.Key = key;
aes_ofb_1.IV = iv;
var value2 = AESUtils.DecryptToString(aes_ofb, data);
var value3 = AESUtils.DecryptToString(aes_ofb_1, data);
Assert.IsTrue(value2 == value3);
Assert.IsTrue(value1 == value3);
}
}
}