| 1 | using Microsoft.Data.Sqlite; |
| 2 | using System.Security.Cryptography; |
| 3 | using System.Text; |
| 4 | |
| 5 | namespace SimPas2_Windows.Managers |
| 6 | { |
| 7 | internal class CryptoManager |
| 8 | { |
| 9 | private readonly string mConnectionString; |
| 10 | private readonly byte[] mEncryptionKey; |
| 11 | private readonly string mCulture; |
| 12 | |
| 13 | private string mJpLang; |
| 14 | |
| 15 | public CryptoManager(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 currency, string name, 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 Crypto |
| 32 | WHERE UPPER(Currency) = UPPER(@currency) AND UPPER(Name) = UPPER(@name)"; |
| 33 | com.Parameters.AddWithValue("@currency", currency); |
| 34 | com.Parameters.AddWithValue("@name", name); |
| 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 AddCrypto(string currency, string name, string address, string seed, string viewkey, string spendkey, string height, string password, string note) |
| 47 | { |
| 48 | if (string.IsNullOrWhiteSpace(currency) || string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(address)) |
| 49 | { |
| 50 | string err = mCulture == mJpLang |
| 51 | ? "通貨、ウォレット名及び、住所を御入力下さい。" |
| 52 | : "Please fill in the currency, wallet name, and address."; |
| 53 | throw new ArgumentException(err); |
| 54 | } |
| 55 | |
| 56 | string encryptedPassword = !string.IsNullOrWhiteSpace(password) ? EncryptInfo(password) : ""; |
| 57 | string encryptedSeed = !string.IsNullOrWhiteSpace(seed) ? EncryptInfo(seed) : ""; |
| 58 | string encryptedViewkey = !string.IsNullOrWhiteSpace(viewkey) ? EncryptInfo(viewkey) : ""; |
| 59 | string encryptedSpendkey = !string.IsNullOrWhiteSpace(spendkey) ? EncryptInfo(spendkey) : ""; |
| 60 | |
| 61 | if (AlreadyExists(currency, name)) |
| 62 | { |
| 63 | string err = mCulture == mJpLang |
| 64 | ? $"通貨及びウォレット名「{currency}/{name}」エントリは既に存在します。" |
| 65 | : $"An entry with the currency and wallet name for '{currency}/{name}' already exists."; |
| 66 | throw new ArgumentException(err); |
| 67 | } |
| 68 | |
| 69 | using (SqliteConnection conn = new SqliteConnection(mConnectionString)) |
| 70 | { |
| 71 | conn.Open(); |
| 72 | SqliteCommand com = conn.CreateCommand(); |
| 73 | com.CommandText = @" |
| 74 | INSERT INTO Crypto (Currency, Name, Address, Seed, Viewkey, Spendkey, Height, Password, Note) |
| 75 | VALUES ($currency, $name, $address, $seed, $viewkey, $spendkey, $height, $password, $note)"; |
| 76 | com.Parameters.AddWithValue("$currency", currency); |
| 77 | com.Parameters.AddWithValue("$name", name); |
| 78 | com.Parameters.AddWithValue("$address", address); |
| 79 | com.Parameters.AddWithValue("$seed", string.IsNullOrEmpty(seed) ? DBNull.Value : encryptedSeed); |
| 80 | com.Parameters.AddWithValue("$viewkey", string.IsNullOrEmpty(viewkey) ? DBNull.Value : encryptedViewkey); |
| 81 | com.Parameters.AddWithValue("$spendkey", string.IsNullOrEmpty(spendkey) ? DBNull.Value : encryptedSpendkey); |
| 82 | com.Parameters.AddWithValue("$height", string.IsNullOrEmpty(height) ? DBNull.Value : height); |
| 83 | com.Parameters.AddWithValue("$password", string.IsNullOrEmpty(password) ? DBNull.Value : encryptedPassword); |
| 84 | com.Parameters.AddWithValue("$note", string.IsNullOrEmpty(note) ? DBNull.Value : note); |
| 85 | com.ExecuteNonQuery(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | public bool EditCrypto(int id, string currency, string name, string address, string seed, string viewkey, string spendkey, string height, string password, string note) |
| 90 | { |
| 91 | if (string.IsNullOrWhiteSpace(currency) || string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(address)) |
| 92 | { |
| 93 | string err = mCulture == mJpLang |
| 94 | ? "通貨、ウォレット名及び、住所を御入力下さい。" |
| 95 | : "Please fill in the currency, wallet name, and address."; |
| 96 | throw new ArgumentException(err); |
| 97 | } |
| 98 | |
| 99 | string encryptedPassword = string.IsNullOrEmpty(password) ? string.Empty : EncryptInfo(password); |
| 100 | string encryptedSeed = string.IsNullOrEmpty(seed) ? string.Empty : EncryptInfo(seed); |
| 101 | string encryptedViewkey = string.IsNullOrEmpty(viewkey) ? string.Empty : EncryptInfo(viewkey); |
| 102 | string encryptedSpendkey = string.IsNullOrEmpty(spendkey) ? string.Empty : EncryptInfo(spendkey); |
| 103 | |
| 104 | using (SqliteConnection conn = new SqliteConnection(mConnectionString)) |
| 105 | { |
| 106 | conn.Open(); |
| 107 | SqliteCommand com = conn.CreateCommand(); |
| 108 | com.CommandText = @" |
| 109 | UPDATE Crypto |
| 110 | SET Currency = $currency, Name = $name, Address = $address, Seed = $seed, Viewkey = $viewkey, Spendkey = $spendkey, Height = $height, Password = $password, Note = $note |
| 111 | WHERE Id = $id"; |
| 112 | com.Parameters.AddWithValue("$id", id); |
| 113 | com.Parameters.AddWithValue("$currency", currency); |
| 114 | com.Parameters.AddWithValue("$name", name); |
| 115 | com.Parameters.AddWithValue("$address", address); |
| 116 | com.Parameters.AddWithValue("$seed", string.IsNullOrEmpty(seed) ? DBNull.Value : encryptedSeed); |
| 117 | com.Parameters.AddWithValue("$viewkey", string.IsNullOrEmpty(viewkey) ? DBNull.Value : encryptedViewkey); |
| 118 | com.Parameters.AddWithValue("$spendkey", string.IsNullOrEmpty(spendkey) ? DBNull.Value : encryptedSpendkey); |
| 119 | com.Parameters.AddWithValue("$height", string.IsNullOrEmpty(height) ? DBNull.Value : height); |
| 120 | com.Parameters.AddWithValue("$password", string.IsNullOrEmpty(password) ? DBNull.Value : encryptedPassword); |
| 121 | com.Parameters.AddWithValue("$note", string.IsNullOrEmpty(note) ? DBNull.Value : note); |
| 122 | return com.ExecuteNonQuery() > 0; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | public bool DeleteCrypto(int id) |
| 127 | { |
| 128 | using (SqliteConnection conn = new SqliteConnection(mConnectionString)) |
| 129 | { |
| 130 | conn.Open(); |
| 131 | SqliteCommand com = conn.CreateCommand(); |
| 132 | com.CommandText = "DELETE FROM Crypto WHERE Id = $id"; |
| 133 | com.Parameters.AddWithValue("$id", id); |
| 134 | return com.ExecuteNonQuery() > 0; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | public List<(int Id, string Currency, string Name, string Address, string Seed, string Viewkey, string Spendkey, string Height, string Password, string Note)> GetAll(string keyword = "") |
| 139 | { |
| 140 | var cryptos = new List<(int, string, string, string, string, string, string, string, string, string)>(); |
| 141 | using (SqliteConnection conn = new SqliteConnection(mConnectionString)) |
| 142 | { |
| 143 | conn.Open(); |
| 144 | SqliteCommand com = conn.CreateCommand(); |
| 145 | if (string.IsNullOrWhiteSpace(keyword)) |
| 146 | { |
| 147 | com.CommandText = @" |
| 148 | SELECT Id, Currency, Name, Address, Seed, Viewkey, Spendkey, Height, Password, Note FROM Crypto |
| 149 | ORDER BY Currency DESC"; |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | com.CommandText = @" |
| 154 | SELECT Id, Currency, Name, Address, Seed, Viewkey, Spendkey, Height, Password, Note FROM Crypto |
| 155 | WHERE Currency LIKE @keyword OR Name LIKE @keyword |
| 156 | ORDER BY Currency DESC"; |
| 157 | com.Parameters.AddWithValue("@keyword", $"%{keyword}%"); |
| 158 | } |
| 159 | |
| 160 | using (SqliteDataReader reader = com.ExecuteReader()) |
| 161 | { |
| 162 | while (reader.Read()) |
| 163 | { |
| 164 | string decryptedSeed = DecryptInfo(reader.GetString(4)); |
| 165 | string decryptedViewkey = reader.IsDBNull(5) ? string.Empty : DecryptInfo(reader.GetString(5)); |
| 166 | string decryptedSpendkey = reader.IsDBNull(6) ? string.Empty : DecryptInfo(reader.GetString(6)); |
| 167 | string decryptedPassword = reader.IsDBNull(8) ? string.Empty : DecryptInfo(reader.GetString(8)); |
| 168 | |
| 169 | cryptos.Add(( |
| 170 | reader.GetInt32(0), |
| 171 | reader.GetString(1), |
| 172 | reader.GetString(2), |
| 173 | reader.GetString(3), |
| 174 | decryptedSeed, |
| 175 | reader.IsDBNull(5) ? string.Empty : decryptedViewkey, |
| 176 | reader.IsDBNull(6) ? string.Empty : decryptedSpendkey, |
| 177 | reader.IsDBNull(7) ? string.Empty : reader.GetString(7), |
| 178 | reader.IsDBNull(8) ? string.Empty : decryptedPassword, |
| 179 | reader.IsDBNull(9) ? string.Empty : reader.GetString(9) |
| 180 | )); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | return cryptos; |
| 186 | } |
| 187 | |
| 188 | private string EncryptInfo(string answer) |
| 189 | { |
| 190 | using (Aes aes = Aes.Create()) |
| 191 | { |
| 192 | aes.Key = mEncryptionKey; |
| 193 | aes.GenerateIV(); |
| 194 | byte[] iv = aes.IV; |
| 195 | |
| 196 | using (ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, iv)) |
| 197 | { |
| 198 | byte[] plainBytes = Encoding.UTF8.GetBytes(answer); |
| 199 | byte[] encryptedBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length); |
| 200 | byte[] result = new byte[iv.Length + encryptedBytes.Length]; |
| 201 | |
| 202 | Buffer.BlockCopy(iv, 0, result, 0, iv.Length); |
| 203 | Buffer.BlockCopy(encryptedBytes, 0, result, iv.Length, encryptedBytes.Length); |
| 204 | |
| 205 | return Convert.ToBase64String(result); |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | private string DecryptInfo(string encryptedAnswer) |
| 211 | { |
| 212 | byte[] combined = Convert.FromBase64String(encryptedAnswer); |
| 213 | byte[] iv = new byte[16]; |
| 214 | byte[] encryptedBytes = new byte[combined.Length - iv.Length]; |
| 215 | Buffer.BlockCopy(combined, 0, iv, 0, iv.Length); |
| 216 | Buffer.BlockCopy(combined, iv.Length, encryptedBytes, 0, encryptedBytes.Length); |
| 217 | |
| 218 | using (Aes aes = Aes.Create()) |
| 219 | { |
| 220 | aes.Key = mEncryptionKey; |
| 221 | aes.IV = iv; |
| 222 | |
| 223 | using (ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV)) |
| 224 | { |
| 225 | byte[] decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length); |
| 226 | return Encoding.UTF8.GetString(decryptedBytes); |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | } |