| 1 | using Microsoft.Data.Sqlite; |
| 2 | using System.Security.Cryptography; |
| 3 | using System.Text; |
| 4 | |
| 5 | namespace SimPas2_Windows.Managers |
| 6 | { |
| 7 | public class QaManager |
| 8 | { |
| 9 | private readonly string mConnectionString; |
| 10 | private readonly byte[] mEncryptionKey; |
| 11 | private readonly string mCulture; |
| 12 | |
| 13 | private string mJpLang; |
| 14 | |
| 15 | public QaManager(string databasePath, byte[] encryptionKey, string culture) |
| 16 | { |
| 17 | mConnectionString = $"Data Source={databasePath}"; |
| 18 | mEncryptionKey = encryptionKey ?? throw new ArgumentNullException(nameof(encryptionKey)); |
| 19 | mCulture = culture; |
| 20 | |
| 21 | mJpLang = "ja-JP"; |
| 22 | } |
| 23 | |
| 24 | private bool AlreadyExists(string website, string question, int? excludeId = null) |
| 25 | { |
| 26 | using (SqliteConnection conn = new SqliteConnection(mConnectionString)) |
| 27 | { |
| 28 | conn.Open(); |
| 29 | SqliteCommand com = conn.CreateCommand(); |
| 30 | com.CommandText = @" |
| 31 | SELECT COUNT(*) FROM Qa |
| 32 | WHERE UPPER(Website) = UPPER(@website) AND UPPER(Question) = UPPER(@question)"; |
| 33 | com.Parameters.AddWithValue("@website", website); |
| 34 | com.Parameters.AddWithValue("@question", question); |
| 35 | |
| 36 | if (excludeId.HasValue) |
| 37 | { |
| 38 | com.CommandText += " AND Id != @excludeId"; |
| 39 | com.Parameters.AddWithValue("@excludeId", excludeId.Value); |
| 40 | } |
| 41 | |
| 42 | return Convert.ToInt32(com.ExecuteScalar()) > 0; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | public void AddQa(string website, string question, string answer, string note) |
| 47 | { |
| 48 | if (string.IsNullOrWhiteSpace(website) || string.IsNullOrWhiteSpace(question) || string.IsNullOrWhiteSpace(answer)) |
| 49 | { |
| 50 | string err = mCulture == mJpLang |
| 51 | ? "ウェブサイト、質問及び、回答を御入力下さい。" |
| 52 | : "Please fill in the website, question, and answer."; |
| 53 | throw new ArgumentException(err); |
| 54 | } |
| 55 | |
| 56 | string encryptedAnswer = EncryptAnswer(answer); |
| 57 | |
| 58 | if (AlreadyExists(website, question)) |
| 59 | { |
| 60 | string err = mCulture == mJpLang |
| 61 | ? $"ウェブサイト及び質問「{website}/{question}」向け秘密質問は既に存在します。" |
| 62 | : $"An secret question with the website and question for '{website}/{question}' already exists."; |
| 63 | throw new ArgumentException(err); |
| 64 | } |
| 65 | |
| 66 | using (SqliteConnection conn = new SqliteConnection(mConnectionString)) |
| 67 | { |
| 68 | conn.Open(); |
| 69 | SqliteCommand com = conn.CreateCommand(); |
| 70 | com.CommandText = @" |
| 71 | INSERT INTO Qa (Website, Question, Answer, Note) |
| 72 | VALUES ($website, $question, $answer, $note)"; |
| 73 | com.Parameters.AddWithValue("$website", website); |
| 74 | com.Parameters.AddWithValue("$question", question); |
| 75 | com.Parameters.AddWithValue("$answer", encryptedAnswer); |
| 76 | com.Parameters.AddWithValue("$note", string.IsNullOrEmpty(note) ? DBNull.Value : note); |
| 77 | com.ExecuteNonQuery(); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | public bool EditQa(int id, string website, string question, string answer, string note) |
| 82 | { |
| 83 | if (string.IsNullOrWhiteSpace(website) || string.IsNullOrWhiteSpace(question) || string.IsNullOrWhiteSpace(answer)) |
| 84 | { |
| 85 | string err = mCulture == mJpLang |
| 86 | ? "ウェブサイト、質問及び、回答を御入力下さい。" |
| 87 | : "Please fill in the website, question, and answer."; |
| 88 | throw new ArgumentException(err); |
| 89 | } |
| 90 | |
| 91 | string encryptedAnswer = EncryptAnswer(answer); |
| 92 | |
| 93 | if (AlreadyExists(website, question, id)) |
| 94 | { |
| 95 | string err = mCulture == mJpLang |
| 96 | ? $"ウェブサイト及び質問「{website}/{question}」向け秘密質問は既に存在します。" |
| 97 | : $"An secret question with the website and question for '{website}/{question}' already exists."; |
| 98 | throw new ArgumentException(err); |
| 99 | } |
| 100 | |
| 101 | using (SqliteConnection conn = new SqliteConnection(mConnectionString)) |
| 102 | { |
| 103 | conn.Open(); |
| 104 | SqliteCommand com = conn.CreateCommand(); |
| 105 | com.CommandText = @" |
| 106 | UPDATE Qa |
| 107 | SET Website = $website, Question = $question, Answer = $answer, Note = $note |
| 108 | WHERE Id = $id"; |
| 109 | com.Parameters.AddWithValue("$id", id); |
| 110 | com.Parameters.AddWithValue("$website", website); |
| 111 | com.Parameters.AddWithValue("$question", question); |
| 112 | com.Parameters.AddWithValue("$answer", encryptedAnswer); |
| 113 | com.Parameters.AddWithValue("$note", string.IsNullOrEmpty(note) ? DBNull.Value : note); |
| 114 | return com.ExecuteNonQuery() > 0; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | public bool DeleteQa(int id) |
| 119 | { |
| 120 | using (SqliteConnection conn = new SqliteConnection(mConnectionString)) |
| 121 | { |
| 122 | conn.Open(); |
| 123 | SqliteCommand com = conn.CreateCommand(); |
| 124 | com.CommandText = "DELETE FROM Qa WHERE Id = $id"; |
| 125 | com.Parameters.AddWithValue("$id", id); |
| 126 | return com.ExecuteNonQuery() > 0; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | public List<(int Id, string Website, string Question, string Answer, string Note)> GetAll(string keyword = "") |
| 131 | { |
| 132 | var qas = new List<(int, string, string, string, string)>(); |
| 133 | using (SqliteConnection conn = new SqliteConnection(mConnectionString)) |
| 134 | { |
| 135 | conn.Open(); |
| 136 | SqliteCommand com = conn.CreateCommand(); |
| 137 | if (string.IsNullOrWhiteSpace(keyword)) |
| 138 | { |
| 139 | com.CommandText = @" |
| 140 | SELECT Id, Website, Question, Answer, Note FROM Qa |
| 141 | ORDER BY Website ASC"; |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | com.CommandText = @" |
| 146 | SELECT Id, Website, Question, Answer, Note FROM Qa |
| 147 | WHERE Website LIKE @keyword OR Question LIKE @keyword |
| 148 | ORDER BY Website ASC"; |
| 149 | com.Parameters.AddWithValue("@keyword", $"%{keyword}%"); |
| 150 | } |
| 151 | |
| 152 | using (SqliteDataReader reader = com.ExecuteReader()) |
| 153 | { |
| 154 | while (reader.Read()) |
| 155 | { |
| 156 | string encryptedAnswer = reader.GetString(3); |
| 157 | string decryptedAnswer = DecryptAnswer(encryptedAnswer); |
| 158 | qas.Add(( |
| 159 | reader.GetInt32(0), |
| 160 | reader.GetString(1), |
| 161 | reader.GetString(2), |
| 162 | decryptedAnswer, |
| 163 | reader.IsDBNull(4) ? string.Empty : reader.GetString(4) |
| 164 | )); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return qas; |
| 170 | } |
| 171 | |
| 172 | private string EncryptAnswer(string answer) |
| 173 | { |
| 174 | using (Aes aes = Aes.Create()) |
| 175 | { |
| 176 | aes.Key = mEncryptionKey; |
| 177 | aes.GenerateIV(); |
| 178 | byte[] iv = aes.IV; |
| 179 | |
| 180 | using (ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, iv)) |
| 181 | { |
| 182 | byte[] plainBytes = Encoding.UTF8.GetBytes(answer); |
| 183 | byte[] encryptedBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length); |
| 184 | byte[] result = new byte[iv.Length + encryptedBytes.Length]; |
| 185 | |
| 186 | Buffer.BlockCopy(iv, 0, result, 0, iv.Length); |
| 187 | Buffer.BlockCopy(encryptedBytes, 0, result, iv.Length, encryptedBytes.Length); |
| 188 | |
| 189 | return Convert.ToBase64String(result); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | private string DecryptAnswer(string encryptedAnswer) |
| 195 | { |
| 196 | byte[] combined = Convert.FromBase64String(encryptedAnswer); |
| 197 | byte[] iv = new byte[16]; |
| 198 | byte[] encryptedBytes = new byte[combined.Length - iv.Length]; |
| 199 | Buffer.BlockCopy(combined, 0, iv, 0, iv.Length); |
| 200 | Buffer.BlockCopy(combined, iv.Length, encryptedBytes, 0, encryptedBytes.Length); |
| 201 | |
| 202 | using (Aes aes = Aes.Create()) |
| 203 | { |
| 204 | aes.Key = mEncryptionKey; |
| 205 | aes.IV = iv; |
| 206 | |
| 207 | using (ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV)) |
| 208 | { |
| 209 | byte[] decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length); |
| 210 | return Encoding.UTF8.GetString(decryptedBytes); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |