forked from ManifestHub/ManifestHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AccountInfoCallback.cs
89 lines (76 loc) · 3.31 KB
/
AccountInfoCallback.cs
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
84
85
86
87
88
89
using System.Security.Cryptography;
using Newtonsoft.Json;
namespace ManifestHub;
[method: JsonConstructor]
public class AccountInfoCallback(
string accountName,
string? accountPassword = null,
string? refreshToken = null,
DateTime? lastRefresh = null,
string? index = null,
bool? aesEncrypted = null,
string? aesIV = null,
List<uint>? appIds = null) {
public string AccountName { get; set; } = accountName;
public string? AccountPassword { get; set; } = accountPassword;
public string? RefreshToken { get; set; } = refreshToken;
public DateTime? LastRefresh { get; set; } = lastRefresh;
public string? Index { get; set; } = index;
public bool? AesEncrypted { get; set; } = aesEncrypted;
public string? AesIV { get; set; } = aesIV;
public List<uint> AppIds { get; set; } = appIds ?? [];
public AccountInfoCallback(AccountInfoCallback other) : this(
other.AccountName,
other.AccountPassword,
other.RefreshToken,
other.LastRefresh,
other.Index,
other.AesEncrypted,
other.AesIV,
[.. other.AppIds]) {
}
// Encrypt data
public void Encrypt(string key) {
if (AesEncrypted is true) return;
AccountPassword = EncryptString(AccountPassword, key);
RefreshToken = EncryptString(RefreshToken, key);
AesEncrypted = true;
}
// Decrypt data
public void Decrypt(string key) {
AesEncrypted ??= false;
if (AesEncrypted is false) return;
AccountPassword = DecryptString(AccountPassword, key);
RefreshToken = DecryptString(RefreshToken, key);
AesEncrypted = false;
}
// Helper method to encrypt a string
private string? EncryptString(string? text, string key) {
if (string.IsNullOrEmpty(text)) return text;
using var aesAlg = Aes.Create();
aesAlg.Key = Convert.FromBase64String(key); // Decode key from Base64 to byte[]
aesAlg.GenerateIV(); // Generate a new IV for each encryption
AesIV ??= Convert.ToBase64String(aesAlg.IV); // Save IV if not already set
aesAlg.IV = Convert.FromBase64String(AesIV); // Decode IV from Base64 to byte[]
var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using var msEncrypt = new MemoryStream();
using var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
using var swEncrypt = new StreamWriter(csEncrypt);
swEncrypt.Write(text);
swEncrypt.Close();
csEncrypt.Close();
return Convert.ToBase64String(msEncrypt.ToArray());
}
// Helper method to decrypt a string
private string? DecryptString(string? cipherText, string key) {
if (string.IsNullOrEmpty(cipherText)) return cipherText;
using var aesAlg = Aes.Create();
aesAlg.Key = Convert.FromBase64String(key); // Decode key from Base64 to byte[]
aesAlg.IV = Convert.FromBase64String(AesIV!); // Decode IV from Base64 to byte[]
var decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using var msDecrypt = new MemoryStream(Convert.FromBase64String(cipherText));
using var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
using var srDecrypt = new StreamReader(csDecrypt);
return srDecrypt.ReadToEnd();
}
}