-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigHelper.cs
175 lines (153 loc) · 6.14 KB
/
ConfigHelper.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
namespace wagtail
{
public class ConfigData
{
public string api_key { get; set; }
public string voice_id { get; set; }
}
public static class ConfigHelper
{
const string configFilePath = "config.json";
static readonly string encryptKey = "3kpPPnBL/mtlarpXkhrHFSl13SwyYeJMuXAcvgD7f2A="; // This is a Base64 encoded 32-byte key
static readonly byte[] keyBytes = Convert.FromBase64String(encryptKey); // Convert to byte array
public static void SaveConfig(string apiKey, string voiceId)
{
Console.WriteLine("SaveConfig method called.");
Log("SaveConfig method called.");
// Encrypt the individual values
string encryptedApiKey = Encrypt(apiKey, keyBytes);
string encryptedVoiceId = Encrypt(voiceId, keyBytes);
// Create JSON object with encrypted values
var config = new { api_key = encryptedApiKey, voice_id = encryptedVoiceId };
string json = JsonSerializer.Serialize(config);
// Save JSON to file
File.WriteAllText(configFilePath, json);
Log("SaveConfig method succeeded.");
}
public static (string apiKey, string voiceId) LoadConfig()
{
try
{
Console.WriteLine("LoadConfig method called.");
Log("LoadConfig method called.");
if (!File.Exists(configFilePath))
{
Console.WriteLine("Config file does not exist.");
Log("Config file does not exist.");
return (null, null);
}
if (new FileInfo(configFilePath).Length == 0)
{
const string message =
"Config file is empty. Please run the app with --config option to create a new config file.";
Console.WriteLine(message);
Log(message);
return (null, null);
}
// Read JSON from file
string json = File.ReadAllText(configFilePath);
Log($"Read JSON from file: {json}");
// Deserialize JSON into the ConfigData class
ConfigData config = JsonSerializer.Deserialize<ConfigData>(json);
// Use the deserialized values
string? decryptedApiKey = Decrypt(config.api_key, keyBytes);
string? decryptedVoiceId = Decrypt(config.voice_id, keyBytes);
Log($"Loaded API Key: {decryptedApiKey}");
Log($"Loaded Voice ID: {decryptedVoiceId}");
return (decryptedApiKey, decryptedVoiceId);
}
catch (Exception e)
{
Console.WriteLine($"An error occurred: {e}");
Log($"An error occurred: {e}");
return (null, null);
}
}
private static string Encrypt(string text, byte[] key)
{
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = key; // Use byte array
aesAlg.IV = new byte[16];
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (
CryptoStream csEncrypt = new CryptoStream(
msEncrypt,
encryptor,
CryptoStreamMode.Write
)
)
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(text);
}
}
return Convert.ToBase64String(msEncrypt.ToArray());
}
}
}
private static string Decrypt(string cipherText, byte[] key)
{
Log($"Decrypting: {cipherText}");
try
{
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = key; // Use byte array
aesAlg.IV = new byte[16];
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (
MemoryStream msDecrypt = new MemoryStream(
Convert.FromBase64String(cipherText)
)
)
{
using (
CryptoStream csDecrypt = new CryptoStream(
msDecrypt,
decryptor,
CryptoStreamMode.Read
)
)
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
string decryptedText = srDecrypt.ReadToEnd();
// Log the decrypted text
Log($"Decrypted Text: {decryptedText}");
return decryptedText;
}
}
}
}
}
catch (Exception ex)
{
// Log decryption errors
Log($"Decryption Error: {ex.Message}");
return string.Empty;
}
}
private const string LogFilePath = "log.txt";
private static void Log(string message)
{
try
{
File.AppendAllText(LogFilePath, $"{DateTime.Now}: {message}\n");
}
catch (Exception ex)
{
// Handle any errors that occur while logging (e.g., disk full, permissions issues)
Console.WriteLine($"An error occurred while logging: {ex.Message}");
}
}
}
}